Basic Shell (Console) operation for beginners

Booting, installing, newbie
Message
Author
Bruce B

#271 Post by Bruce B »

technosaurus wrote:[ $x == "*" ] && exit
It doesn't seem to work. The left part of the comparison might also
need quotes.

In this case, I think I'll do like the pros. Spend 60 bytes writing up a bug
report, rather than 10 bytes to fix the program.

~

Bruce B

#272 Post by Bruce B »

lspet

With this lstool you can view the contents of your pet package.
Helpful especially, if you have to manually 'unpet'.

Code: Select all

#!/bin/bash
tar -tzf 2>/dev/null "$@"
lstgz

For previewing your tar.gz and .tgz files

The code is identical to lspet, I wonder why?

Code: Select all

#!/bin/bash
tar -tzf 2>/dev/null "$@"
~

Shep
Posts: 878
Joined: Sat 08 Nov 2008, 07:55
Location: Australia

#273 Post by Shep »

Bruce B wrote:
technosaurus wrote:[ $x == "*" ] && exit
It doesn't seem to work. The left part of the comparison might also
need quotes.
Are you are testing for x being equal to an asterisk?

Code: Select all

# x=*; if [ "$x" = '*' ]; then echo xxx; fi
Instead of the old single brackets, you can use the much more-versatile double square brackets of bash's extended test function. I think you can get away without quoting the $x here because within [[.......]] there is no word splitting:

Code: Select all

x=*; if [[ $x == '*' ]]; then echo xxx; fi
An alternative: save any positional parameters, then you are free to use the super-efficient set to determine whether your directory is empty:

Code: Select all

 set -- *; if [[ $1 == '*' ]]; then echo directory is empty; fi

Bruce B

#274 Post by Bruce B »

I wrote a little script which will not work. It will work and produce the output shown in the comments after certain characters have been escaped. The escape character is used to change the meaning of the character sent to the 'for loop' is the \ (backslash). The backslash is used to give a literal meaning to the character rather then how bash uses it.

For example: echo ~ displays /root and echo \~ displays ~

Code: Select all

#!/bin/bash

for i in  ! " # $ % & ' ( ) * + , - . / 0 1 2 3 4 5 6 7 8 9 \
: ; < = > ? @ A B C  D E F G H I J K L M N O P Q R S T U V W \
X Y Z [ ] ^ _ ` a b c d e f g h i j k l m n o p q r s t u v \
w x y z { | } ~
do
echo -n "$i "
done
echo

# ! " # $ % & ' ( ) * + , - . / 0 1 2 3 4 5 6 7 8 9
# : ; < = > b @ A B C D E F G H I J K L M N O P Q R
# S T U V W X Y Z [ ] ^ _ ` a b c d e f g h i j k l
# m n o p q r s t u v w x y z { | } ~
The idea here is to self teach exactly how characters are interpreted and which need escaped.

~
Attachments
practice.zip
(372 Bytes) Downloaded 619 times

Bruce B

#275 Post by Bruce B »

Bash errors

Bash gives an error "command not found"

Sometimes you run into some though ones. This one about takes the cake.

Cross my heart - I was confident I did everything right. I looked at the code over and over and could not find the reason for the command not found.

I didn't find it with the mp editor. I can't find anything wrong with cat.

My dog didn't eat my homework but I think she messed up my script. It happens when I leave tasty stuff on the table, she sometimes jumps up on the keyboard uses it as a launch pad to the table to steal my food.

Here is the problem: She pressed the space bar to move the text to column 154, then I guess entered these characters ~V~R

I don't know this for a fact, but I confronted her and she didn't deny it.

As stated bash finds them and says command not found.

Cat don't display them. With persistence I debugged it and even wrote my own poor man's cat utility called kitty. Kitty won't display this character string on the screen either.

Echo will.

How many times I thought bash was the culprit only to find it wasn't.

Anyone else had tough times trying to figure why things just will not work right sometimes?

Anyone else started thinking the problem is Bash, because you couldn't find the problem in your source?

~~~~

Change subject

Hoping Shep or another sed expert is still around, I have a question.

I want to reduce lines empty lines more than two to double lines.

The obvious approach is to me


sed -e 's/^$^$^$^$/^$^$/' -e 's/^$^$^$/^$^$/'


It works, but I've learned enough to know that a sed expert knows the best way to do it.

Shep, anyone?

~

Shep
Posts: 878
Joined: Sat 08 Nov 2008, 07:55
Location: Australia

#276 Post by Shep »

Bruce B wrote:I want to reduce lines empty lines more than two to double lines.
See whether this is going to do what you want:

sed '/^\n*$/{N; /^\n$/N; /\n$/D;}'

reading from left to right:
if the pattern space is empty or contains only newlines then do everything inside the curly brackets, viz.,
append the next line of input to the pattern space
if the pattern space contains exactly two empty lines then append to the pattern space the next line of input
if the last line was empty, delete the first part of the pattern space and repeat until a non-blank line is encountered

Bruce B

#277 Post by Bruce B »

Introducing the file command

In its most simple form we use this external command to help figure what kind of file we are working with with.

In my example, mplayer.dsl has an unknown extension. The Linux distro Damn Small Linux uses it. I want to know what it is. I use file to look at it and report back.

I say file mplayer.dsl

file says: mplayer.dsl: gzip compressed data, from Unix

To learn more I say: gzip -d mplayer.dsl

gzip says: mplayer.dsl: unknown suffix -- ignored

I then say: mv mplayer.dsl mplayer.dsl.gz

Then I say: gzip -d mplayer.dsl.gz

Gzip decompresses it and leaves me with mplayer.dsl

Then I say: file mplayer.dsl

file then says: mplayer.dsl: POSIX tar archive (GNU)

Now I know it is a tar archive

Then I say: tar xvf mplayer.ds

Sure enough it extracts.

To learn more say file --help, for more details consult the man page.

~

Bruce B

#278 Post by Bruce B »

How to make a RAM Disk - step by step

1) Define RAM Disk size
  • Edit menu.lst and set the size of the disk on the kernel line

    ramdisk_size=192000

    This would configure to make a disk size of about 192 MB

    The 192000 is 192000k but the k is not entered
2) Format the RAM Disk in the file /etc/rc.d/rc.local
  • mkfs.ext2 -m 0 /dev/ram6

    the -m 0 says not to allocate space for the super user. It gives more space and the super user allocation is not needed.
3) Fstab
  • tell /etc/fstab about the RAM disk

    Code: Select all

    /dev/ram6 /mnt/ram ext2 rw,noatime,noauto,shortname=mixed,errors=continue 0 0
    
4) mount RAM disk
  • Use ROX-Filer mouse options on /mnt/ram

    or use the command line

    mount /dev/ram6

    or

    mount /mnt/ram

    You can unmount either on the command line or with ROX-Filer
Comments

You can of course use a different disk size, mount point and ram device.

In Puppy we have three basic places to put apps or commands for automatic run.

/etc/rc.d/rc.local
/etc/profile.local
/root/Startup


Here is a guide of what which to use in a variety of circumstances. Any graphics app needs to be put in /root/Startup

Non graphics apps or instructions you only want to run one time per session, put in /ect/rc.d/rc.local

/etc/profile.local is susceptible to being run more than once, something to consider when placing commands in this file

Many readers knew this already. But for those who didn't, now you know.

It is possible that /etc/profile.local doesn't exist. If not make it as a regular text file. It is not a shell script

I use the RAM disk when I have big files or need to batch process files. These files might get modified, converted, deleted, remodified, decoded or encoded, renamed and whatever. This could make for a lot of disk I/O for files that only live a few minutes. The RAM disk speeds things up and saves any disk wear and tear. I also compile in a RAM disk for the same benefits.

The RAM disk I present in this post is what I call - "A true formatted RAM disk"

There are other kinds of RAM disks which can be set up easier. My reason for the so-called true formatted RAM disk is ffmpeg won't write to the other formats.

Thanks

~

Indy'spup
Posts: 50
Joined: Wed 11 May 2011, 15:32
Location: SoCal

#279 Post by Indy'spup »

Ok I got to ask because this is killing me...

Does the Prefix -- used often with a command/function have any specific meaning in bash? As I understand it after the function has run the -- will disable other options pertaining to that function to run and terminate the command cleanly.

ie: --version, or --help etc. etc.

Bruce B

#280 Post by Bruce B »

Indy'spup wrote:ie: --version, or --help etc. etc.
Bash is a command interpreter. I posted the full Bash help on the internal commands and -- is not listed as an internal command.

By which I conclude only that it is not listed.

Set is an internal command and I've seen Shep post examples of set --. I would think -- is an argument the internal command set uses.

Normally we use the -- like this:

cp --help
grep --help


In this case the --help is an argument (parameter?) to the cp and grep commands.

In other words: --help is simply passed on by bash to the external command as an argument.

Bash will pass a bad argument to the cp command. In this example,
I said cp /?

cp replied

[~] cp /?
cp: missing destination file operand after `/?'
Try `cp --help' for more information.
[~]


Issue -- on the CLI and bash reports

[~] --
bash: --: command not found
[~]


This means by itself -- is neither an internal or external command.

~

Shep
Posts: 878
Joined: Sat 08 Nov 2008, 07:55
Location: Australia

#281 Post by Shep »

Indy'spup wrote:Does the Prefix -- used often with a command/function have any specific meaning in bash?
Not exactly. To the shell that combination of characters means nothing special.

"--" is an argument which is meaningful to most commands. It signals the end of options on that command line, thereby indicating that what follows is to be treated as something other than an option.

For example, suppose you (either intentionally or accidently) believe you had created a file named with the two characters "-u". You might think that the command "ls -u" will indicate whether or not that particular file exists. But you will be in for a surprise. :shock:

The "ls" command can take many optional arguments. One such argument is "-u" which tells it to list filenames along with the last time each was accessed. So your command "ls -u" will list all files in the current directory and show their most recent time of access; and it won't list just the one filename you were hoping to see. (Though somewhere in the long listing you will find that file, if it exists.)

To make it clear to "ls" that you want the "-u" string to be seen as a file name and not interpreted as an argument, you can use "ls -- -u"

Had you not known about the "--" argument, or if you were using an older operating system where "--" was found to not be meaningful to "ls", then you could achieve the same outcome by using "ls ./-u" where "." is a universal abbreviation for your current directory.

HTH

User avatar
01micko
Posts: 8741
Joined: Sat 11 Oct 2008, 13:39
Location: qld
Contact:

#282 Post by 01micko »

Hi scripters

I knocked up a script the other day mainly so I could make sure i could list the correct dependencies of a package that I was building. Note the -f argument that checks the ro filesystem. Suggestions for improvement are most welcome.

Code: Select all

#!/bin/sh
#dependency check script
# '-f' param to check if dep is in ro filesystem (frugal)
Usage(){ #basic usage called by '-h' or no args
	echo '
usage: -h : display help and exit
       -f <app> : check read only filesystem (frugal types only)
       <app> : the app you want to check' && exit 1
}
export -f Usage
#no args?
[ ! $1 ] && Usage
case $1 in
*-h*)
Usage
;;
-f) #arg only works with frugals, errors if not
[ ! -d /initrd/pup_ro2 ] && echo "not a frugal install, failure" && exit 1
APP=`which $2`
ROPATH="/initrd/pup_ro2"
;;
*)
APP=`which $1`
ROPATH=""
;;
esac
#meat and spuds
[ ! $APP ] && echo "no such application exists on your system" && exit 1 #does it exist?
FAIL=`ldd $APP|grep -i -E "not|no"`
[[ $FAIL ]] &&  echo "ldd error, not a dynamic executable" && exit 1 #it may be a script
LIST=`ldd $APP|sed 's/ /%/g'`
for DEP in $LIST #deps loop
 do
 BASEDEP=`echo $DEP|cut -d '%' -f1`
 PATHDEP=`echo $DEP|cut -d '%' -f3`
 FOUND=`find $ROPATH/usr/lib -name $BASEDEP`
 [ ! $FOUND ] && FOUND=`find $ROPATH/lib -name $BASEDEP`
 [ $FOUND ] && continue
 [ ! $FOUND ] && echo $PATHDEP >> /tmp/deplist && echo $BASEDEP  >> /tmp/finallist
 done
 NEWLIST=`cat /tmp/deplist`
 for SUBDEP in $NEWLIST #deps of deps loop
  do 
     SUBDEPLIST=`ldd $SUBDEP|cut -d ' ' -f1`
     for FOUNDSUBDEP in $SUBDEPLIST
      do 
       BASEFOUNDSUBDEP=`echo $FOUNDSUBDEP|cut -d '%' -f1`
       FOUNDSUB=`find $ROPATH/usr/lib -name $BASEFOUNDSUBDEP`
       [ ! $FOUNDSUB ] && FOUNDSUB=`find $ROPATH/lib -name $BASEFOUNDSUBDEP`
       [ $FOUNDSUB ] && continue
       [ ! $FOUNDSUB ] && echo "$BASEFOUNDSUBDEP" >> /tmp/finallist
      done
  done
cat /tmp/finallist|sort -u #display results
#cleanup temp
rm /tmp/finallist 2>/dev/null
rm /tmp/deplist 2>/dev/null
exit 0
#end
It checks (or is supposed to) check deps of deps 1 deep.
Attachments
find_deps.gz
(809 Bytes) Downloaded 521 times
Puppy Linux Blog - contact me for access

Bruce B

#283 Post by Bruce B »

01micko

Trivial comment, but potentially frustrating, if one doesn't know.

Sometimes executable files are packed with UPX, when they are, the file is a valid executable file with dependencies, problem is, ldd fails, producing a false negative.

(I think an English teacher would fail me on the sentence above)

You check with the file command and get an accurate output.

Not a criticism or suggestion. Just something to put in your toolkit of ever expanding knowledge, in the event you didn't already know about the behavior.

Bruce

~

User avatar
01micko
Posts: 8741
Joined: Sat 11 Oct 2008, 13:39
Location: qld
Contact:

#284 Post by 01micko »

yeah, I did actually know about that Bruce.. from you I think in one of these threads. Just didn't think of it.. :oops:

I wonder if there is a way to decompress the upx'd binaries and be able to check them with ldd?
Puppy Linux Blog - contact me for access

amigo
Posts: 2629
Joined: Mon 02 Apr 2007, 06:52

#285 Post by amigo »

upx -d decompresses bins, but there is no guarantee that the md5sum of the file will be the same after decompressing and then re-compressing -unless it is done more than once. After doing it once, repeating the operation will not change the md5sum anymore.

eriksatie
Posts: 41
Joined: Tue 07 Jun 2011, 03:27

#286 Post by eriksatie »

Bruce B wrote:
Here is a very simple script that won't seem to work. Please try it.

Scriptname r

Code: Select all

#!/bin/bash
cd /
After you've made it executable, run it.
How do I make a script and make it executable?

Bruce B

#287 Post by Bruce B »

eriksatie wrote:How do I make a script and make it executable?
I guess that wasn't mentioned. I'll come back and make a thorough reply.

In the meantime chmod 755 scriptname will suffice

~
Last edited by Bruce B on Thu 09 Jun 2011, 02:13, edited 1 time in total.

User avatar
rcrsn51
Posts: 13096
Joined: Tue 05 Sep 2006, 13:50
Location: Stratford, Ontario

#288 Post by rcrsn51 »

In the meantime chown 755 scriptname will suffice
I believe you mean

Code: Select all

chmod 755 scriptname

Bruce B

#289 Post by Bruce B »

rcrsn51 wrote:
In the meantime chown 755 scriptname will suffice
I believe you mean

Code: Select all

chmod 755 scriptname
I did. Thank you very much. I'll edit the original.

This is one reason why the open source model works, peer review.

~

SimpleWater
Posts: 94
Joined: Tue 19 Apr 2011, 11:53

#290 Post by SimpleWater »

Code: Select all

chmod +x scriptname
that works fine for me, also easier to remember than numbers and fewer characters too

Post Reply