Basic Shell (Console) operation for beginners

Booting, installing, newbie
Message
Author
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

User avatar
rhadon
Posts: 1292
Joined: Thu 27 Mar 2008, 11:05
Location: Germany

#291 Post by rhadon »

Hi,

inspired by this thread, I tried to solve it by my own.
btw. I think this feature could be worth to be integrated in all new puppies. :wink:

These are the relevant lines (thanks DaveS):

Code: Select all

read -t 5 NUMSAVE 
 [ -z "$NUMSAVE" ] && NUMSAVE=1
I want to substitute both values (5 and 1) by values from a textfile.

I built a textfile named "boot-savefile" with:

Code: Select all

time=10
default=2
From console, I get access to the file with

Code: Select all

#grep "time=" boot-savefile | cut -f 2 -d '='
10
#grep "default=" boot-savefile | cut -f 2 -d '='
2
#
I made a simple script for testing:

Code: Select all

TST1=grep "time=" boot-savefile | cut -f 2 -d '='
echo $TST1
I tried now for 2 days, several hours a day, to get this working. No chance. No matter what I tried, and I really tried a lot.

Maybe I don't see the trees for the wood. Where am I wrong?

Thanks
Rolf
Ich verwende "frugal", und das ist gut so. :wink:
Raspberry Pi without Puppy? No, thanks.

Bruce B

#292 Post by Bruce B »

Rolf,

Please post the names save files on your list in order from top to bottom.

Please tell me if there is a default, such as the one at the top.

Please zip init if you are not using Lupu 5.20

Bruce

~

If you don't know how to open initrd.gz tell me

User avatar
rhadon
Posts: 1292
Joined: Thu 27 Mar 2008, 11:05
Location: Germany

#293 Post by rhadon »

Bruce,

the code from DaveS works fine. I can change the time or the default savefile. But every time I want to change something, like I want another savefile by default, I have to edit the initrd.gz. I know how to do but it's annoying. It is much easier to edit a simple textfile. That's the actual reason for me, digging in bash programming.

I now use Lupu 5.25 and I think it's equal to 5.20
the original part of init ~line 782:

Code: Select all

     read NUMSAVE
    [ $NUMSAVE -ne 0 ] && PUPSAVE="`cat /tmp/PUPSAVE2SFSS | tr '\n' ' ' | cut -f $NUMSAVE -d ' '`"
changed to:

Code: Select all

read -t 5 NUMSAVE 
  [ -z "$NUMSAVE" ] && NUMSAVE=1
    [ $NUMSAVE -ne 0 ] && PUPSAVE="`cat /tmp/PUPSAVE2SFSS | tr '\n' ' ' | cut -f $NUMSAVE -d ' '`"
As I wrote, it works fine but with values from a external textfile it would be great.

For now my savefiles are: lupusave.3fs, lupusave-compile.3fs and lupusave-dummy.3fs. I don't think, it is important.

Rolf
Last edited by rhadon on Thu 23 Jun 2011, 05:48, edited 1 time in total.
Ich verwende "frugal", und das ist gut so. :wink:
Raspberry Pi without Puppy? No, thanks.

Bruce B

#294 Post by Bruce B »

Rolf,

I thought you wanted help. I'll offer a suggesiton that made it more enjoyable for me

-n 1 saves from entering the selection then the enter key - just enter the selection

try it

Bruce

~

User avatar
rhadon
Posts: 1292
Joined: Thu 27 Mar 2008, 11:05
Location: Germany

#295 Post by rhadon »

Bruce,

I don't understand:
-n 1 saves from entering the selection then the enter key - just enter the selection
Yes, I want help, and for 2 times I tried my very best to explain what I want and where I'm struggling.

OK, maybe step by step. Please forget everything before.

I have a textfile named boot-savefile with:

Code: Select all

time=10 
default=2
And a script:

Code: Select all

TST1=20
echo $TST1
gives the expected result: 20. OK.

Typing in console:

Code: Select all

grep "time=" boot-savefile | cut -f 2 -d '='
gives the expected result: 10

In the script:

Code: Select all

TST1=grep "time=" boot-savefile | cut -f 2 -d '='
echo $TST1
doesn't work. Also everthing I tried to get it work, failed.

What is wrong, please?

It seems to be a stupid, basic problem of misunderstanding from me.

Rolf

Edit: OK, I got it. :D

Code: Select all

TST1=$(grep "time=" boot-savefile | cut -f 2 -d '=')
Works.

Thanks for trying to help.
Ich verwende "frugal", und das ist gut so. :wink:
Raspberry Pi without Puppy? No, thanks.

Bruce B

#296 Post by Bruce B »

rhadon wrote:In the script:

Code: Select all

TST1=grep "time=" boot-savefile | cut -f 2 -d '='
Sorry I wasn't on the same page with you.

I'll offer a tip for the future when setting variables such as the one above.


Commonly we use backtics
Compare first line, which won't work, with the second, which will work

TST1=grep "time=" boot-savefile | cut -f 2 -d '='
TST1=`grep "time=" boot-savefile | cut -f 2 -d '='`

I can't define what the backtics do in every scenario, but in this case, I explain it is saying; move the results of my commands from right to left into the variable.

Bruce

~

User avatar
rhadon
Posts: 1292
Joined: Thu 27 Mar 2008, 11:05
Location: Germany

#297 Post by rhadon »

Thanks for clarification, Bruce.

I just learned again that it's always good, thinking twice or more before posting. :lol:

Just before telling you that I tried backtics several times and it doesn't work, I realised that I always used normal tics (I hope that's the right term), as ' instead of `. :oops:

So another mystery is solved for me.

Thanks,

Rolf
Ich verwende "frugal", und das ist gut so. :wink:
Raspberry Pi without Puppy? No, thanks.

Bruce B

#298 Post by Bruce B »

Quotes and apostrophes work also and are frequently used when setting variables. The key is to know what they do.

Here is something for you to copy and paste to your console if you want to see the effects. One command at a time from top to bottom.

myvar='echo $PATH'

$myvar

myvar="echo $PATH"

$myvar

myvar=`echo $PATH | cut -d : -f 1-3`

$myvar

unset myvar


~

User avatar
rhadon
Posts: 1292
Joined: Thu 27 Mar 2008, 11:05
Location: Germany

#299 Post by rhadon »

Thanks again, I got it.

I was playing with the cut command for better understanding. So I changed the delimiter to /.

Now there's a difference between running this command directly or moving the result of the command into myvar and then asking the value of myvar.

Code: Select all

# echo $PATH | cut -d / -f 1-5
/bin:/usr/bin:/sbin:

# myvar=`echo $PATH | cut -d / -f 1-5`
# $myvar
bash: /bin:/usr/bin:/sbin:: No such file or directory
# 

Shouldn't both kinds give identical results? OK, the result is equal, but why no comment above?


Another question. I've built a small script which seems to work fine. But adding it to the init script of initrd doesn't work. I think I know why, but not how to solve. For now I will try to find it out by myself. A new day, new ideas ...and so on.

If I can't solve this for me, is it OK to post such questions here (maybe initrd isn't so interesting for others here) or should I post in a separate thread, maybe in Users ( For the regulars )?

Rolf

Edit: Aaarrrggg... I hate typos but typos love me. edited the third time :oops:
Ich verwende "frugal", und das ist gut so. :wink:
Raspberry Pi without Puppy? No, thanks.

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

#300 Post by 01micko »

Hi Rolf

I saw this before dinner but I thought I'll let it go for a little while..

echo "$myvar"

It works without the double quotes too.

Cheers
Puppy Linux Blog - contact me for access

Post Reply