Page 15 of 18

Posted: Tue 31 May 2011, 03:39
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

Posted: Tue 07 Jun 2011, 08:12
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.

Posted: Wed 08 Jun 2011, 05:40
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

~

Posted: Wed 08 Jun 2011, 10:00
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?

Posted: Wed 08 Jun 2011, 16:23
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.

Posted: Wed 08 Jun 2011, 18:27
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?

Posted: Wed 08 Jun 2011, 22:35
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

~

Posted: Thu 09 Jun 2011, 00:32
by rcrsn51
In the meantime chown 755 scriptname will suffice
I believe you mean

Code: Select all

chmod 755 scriptname

Posted: Thu 09 Jun 2011, 02:13
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.

~

Posted: Sat 11 Jun 2011, 21:07
by SimpleWater

Code: Select all

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

Posted: Wed 22 Jun 2011, 16:23
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

Posted: Wed 22 Jun 2011, 16:34
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

Posted: Wed 22 Jun 2011, 17:23
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

Posted: Wed 22 Jun 2011, 17:34
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

~

Posted: Wed 22 Jun 2011, 19:50
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.

Posted: Thu 23 Jun 2011, 12:02
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

~

Posted: Thu 23 Jun 2011, 15:10
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

Posted: Thu 23 Jun 2011, 18:57
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


~

Posted: Fri 24 Jun 2011, 07:48
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:

Posted: Fri 24 Jun 2011, 08:51
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