Puppy Linux Discussion Forum Forum Index Puppy Linux Discussion Forum
Puppy HOME page : puppylinux.com
"THE" alternative forum : puppylinux.info
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

The time now is Mon 22 Dec 2014, 13:07
All times are UTC - 4
 Forum index » Advanced Topics » Additional Software (PETs, n' stuff) » Educational
Canned Bash lessons - learn to use Bash commands
Post new topic   Reply to topic View previous topic :: View next topic
Page 2 of 2 [29 Posts]   Goto page: Previous 1, 2
Author Message
amigo

Joined: 02 Apr 2007
Posts: 2295

PostPosted: Sun 28 Mar 2010, 11:10    Post subject:  

DMcCunney, I use the is_elf as a function in any script where it is needed, though I see what you mean now about using the sticky bit for external programs.

As I said, I'm unsure about the mechansim whereby file might be less-than-safe -maybe it only has to do with it getting the file-type wrong -there were/are issues with /etc/mailcap since it relys on file to decide the filetype.

As for ldd, it is a script which runs '/lib/ld-linux.so.2 --verify $@' to determine dependencies. Obviously, that could be disastrous when run on a malicious ELF binary... My replacement uses 'objdump $@ |grep NEEDED' to avoid that. It also provides a more accurate list of dependencies -ldd lists everything linked to by an object -even if ti doesn't really use them all- sometimes Makefiles link libraries which are not needed.
Back to top
View user's profile Send private message 
HiDeHo

Joined: 16 Mar 2011
Posts: 289

PostPosted: Wed 06 Jul 2011, 10:18    Post subject:  

if the pet is not an application they y is it a .pet adn not a .tar.gz

seems weird to extract a pet and not install it
Back to top
View user's profile Send private message 
ttuuxxx


Joined: 05 May 2007
Posts: 10847
Location: Ontario Canada,Sydney Australia

PostPosted: Wed 06 Jul 2011, 10:49    Post subject:  

HiDeHo wrote:
if the pet is not an application they y is it a .pet adn not a .tar.gz

seems weird to extract a pet and not install it

ummm yes it has a menu list under /documents
ttuuxxx

_________________
http://audio.online-convert.com/ <-- excellent site
http://samples.mplayerhq.hu/A-codecs/ <-- Codec Test Files
http://html5games.com/ <-- excellent HTML5 games Smile
Back to top
View user's profile Send private message Visit poster's website 
tallboy


Joined: 21 Sep 2010
Posts: 454
Location: Oslo, Norway

PostPosted: Fri 22 Jul 2011, 23:00    Post subject:  

It sometimes surprises me that so many Linux users are unfamiliar with one of the oldest sources for Linux info. Check out http://tldp.org - The Linux Documentation Project. Look under 'guides', there you'll find the latest version of Wendell Cooper's superb 'Advanced Bash-Scripting Guide'.

There are other useful guides there as well, like 'Introduction to Linux - A Hands on Guide' (don't laugh - read it and discover the 'white spots' on your Linux knowledge map!), and the extremely useful 'GNU/Linux Command-Line Tools Summary'; that one will definitely teach you new ways to live your life in the Linux world!

Tallboy.
Back to top
View user's profile Send private message 
technosaurus


Joined: 18 May 2008
Posts: 4427

PostPosted: Wed 21 Sep 2011, 22:28    Post subject:  

I wish someone would modify the advance BASH scripting guide to be the advanced SHELL scripting guide and point out the "bashisms"

perhaps it already exists?

re: file and dd
I have used read <file with a case $REPLY ...statement for this sort of thing it just reads the first line and is a builtin, but I hope it is safe?

you get []ELF for binary, shared libs and their symlinks
png yields PMG
xpm yields /* XPM */
scripts yield the shabang

here is an example - I wanted to find all occurences of dialog in all scripts so I hacked this together

usage
findusage string
output is a list of all files:linenumbers where $1 occurs
Code:
#!/bin/sh
for FILE in ${PATH//://* }/*;  do
   [ ! -f ${FILE} ] && continue
   read HEAD <${FILE} 2>/dev/null
   case ${HEAD} in
   */bin*sh*)
      i=0
      while read LINE; do
         i=$(($i+1))
         case $LINE in
            *${1}*)echo ${FILE}:$i;;
         esac
      done <${FILE}
   ;;
   esac
done

_________________
Web Programming - Pet Packaging 100 & 101
Back to top
View user's profile Send private message 
technosaurus


Joined: 18 May 2008
Posts: 4427

PostPosted: Wed 28 Sep 2011, 16:18    Post subject:  

Code:
#using shell builtins to replace long pipes of external commands
#
#while read A || [ "$A" ]; do A=`echo $A`;echo ${A};done <<<"${VAR}" |
#or
#while read A || [ "$A" ]; do A=`echo $A`;echo ${A};done <$FILE |
#can replace:
# echo "${VAR}" |tr "\t" " "|tr -s " "|cut/sed ... |
#or
# cat $FILE |tr "\t" " "|tr -s " "|cut/sed ... |
#
#both tr commands are replaced with VAR=`echo $VAR`
#cut is replace with the second <and third> echo using substring manipulation
#use:
# echo ${A#* * *} to remove the first 3 fields or ${A%* * *} to remove the last 3
#there is another alternative if you no longer need the input parameters
#use:
# set `echo $A`; echo $1,$5,$7 ... this uses the 1st,5th&7th " " separated word
#you can also use the IFS value if you want "|" to separate words
#ex.  OLDIFS="${IFS}";IFS="|";...commands...;IFS="${OLDIFS}"
#sed can often be replaced with thie ${VAR///} substring manipulations
#use:
# echo ${A//"${OLDSTRING}"/"${NEWSTRING}"} as a sed replacement
#NOTE this method will work for multiple lines while sed will not
#NOTE on IFS and read, set etc...
#you can get creative with read and IFS too, but it can get difficult to follow
#normally read uses a new line (represented by \n) as the delimiter
#_some_ shells have -d parameter for read, but for portability set/reset IFS
#be careful though, setting IFS will affect other commands too, for example
#set `echo "a b c d e f"`; echo $1 .... "a b c d e f" when IFS="|" ...oops
#this is a feature, not a bug

_________________
Web Programming - Pet Packaging 100 & 101
Back to top
View user's profile Send private message 
smokey01


Joined: 30 Dec 2006
Posts: 1857
Location: South Australia

PostPosted: Wed 14 Dec 2011, 04:10    Post subject:  

Is there a more efficient way to do this?

Code:
#!/bin/bash
if [[ $1 == *.png ]]
then
/usr/bin/geany
else
if [[ $1 == *.PNG ]]
then
/usr/bin/geany
else
if [[ $1 == *.jpeg ]]
then
/usr/bin/geany
else
if [[ $1 == *.jpg ]]
then
/usr/bin/geany
else
if [[ $1 == *.pet ]]
then
/usr/bin/viewnior
else
/usr/bin/mtpaint
fi fi fi fi fi


Is there a way to use an OR operator or something similar.

The applications are there specifically to test the code.

When this script is placed on the desktop and a file is dropped on it, an action takes place depending on the type of file that is dropped on the script file.

Something like the below would be nice:

Code:
#!/bin/bash
if [[ $1 == *.png or *.jpg or *.jpeg or *.PNG ]]
then
do something
else
do something else
fi


TIA

_________________
Puppy Software <-> Distros <-> Puppy Linux Tips
Back to top
View user's profile Send private message Visit poster's website 
maxerro


Joined: 10 Oct 2010
Posts: 65

PostPosted: Wed 14 Dec 2011, 04:50    Post subject:  

smokey01 wrote:
Is there a more efficient way to do this?

Code:
case $1 in
 *.png|*.PNG|*.jpg|*.jpeg) process_pic;;
 *.pet|*.deb|*.rpm) process_pkg;;
 *) do_something_else;;
esac

Don't forget ;; at the end of each case.
Back to top
View user's profile Send private message 
technosaurus


Joined: 18 May 2008
Posts: 4427

PostPosted: Wed 14 Dec 2011, 04:58    Post subject:  

maxerro wrote:
smokey01 wrote:
Is there a more efficient way to do this?

Code:
case $1 in
 *.png|*.PNG|*.jpg|*.jpeg) process_pic;;
 *.pet|*.deb|*.rpm) process_pkg;;
 *) do_something_else;;
esac

Don't forget ;; at the end of each case.

if we are still using ROX-Filer...
Code:
rox $@

or if we keep the mime-types directory

Code:
#!/bin/sh
MIME=`file -b --mime-type $1`
/root/Choices/MIME-types/${MIME//\//_} $1

_________________
Web Programming - Pet Packaging 100 & 101
Back to top
View user's profile Send private message 
smokey01


Joined: 30 Dec 2006
Posts: 1857
Location: South Australia

PostPosted: Wed 14 Dec 2011, 08:26    Post subject:  

Thanks guys.

maxerro your works a treat.

Techno I'm not sure how to implement yours so it will do something when a particular file is selected.

The other little problem I've noticed is if you select more than one type of file, eg: *.pet and *.zip at the same time it copies them both to the same directory.

I am using variable $1 to $9, so in theory I should be able to copy 9 files at a time.

Any ideas?

Thanks

_________________
Puppy Software <-> Distros <-> Puppy Linux Tips
Back to top
View user's profile Send private message Visit poster's website 
maxerro


Joined: 10 Oct 2010
Posts: 65

PostPosted: Wed 14 Dec 2011, 10:02    Post subject:  

smokey01 wrote:
...little problem I've noticed is if you select more than one type of file, eg: *.pet and *.zip at the same time it copies them both to the same directory...

Code:
#!/bin/bash
for ((i=1; i<=$#; i++)); do
 case "${!i}" in
  *.pet|*.deb|*.rpm) busybox cp "${!i}" /destination1 &;;
  *.zip|*.rar|*.bz2) busybox cp "${!i}" /destination2 &;;
  *) busybox cp "${!i}" /destination3 &;;
 esac
done


$# is the number of arguments passed. ${!i} is the value of the i-th argument. This is one of the faster ways. If it causes problems, use classic cp, without busybox and drop & in the end. (It will be MUCH slower.) Be careful if the destination contains spaces, you may need to enclose (some part of) it in double quotes.
Back to top
View user's profile Send private message 
technosaurus


Joined: 18 May 2008
Posts: 4427

PostPosted: Wed 14 Dec 2011, 10:29    Post subject:  

or simply
Code:
#!/bin/sh
for i in "$@"; do
 case "${i}" in
  *.pet|*.deb|*.rpm) busybox cp "${i}" /destination1 &;;
  *.zip|*.rar|*.bz2) busybox cp "${i}" /destination2 &;;
  *) busybox cp "${i}" /destination3 &;;
 esac
done


BTW the reason I used file before was to catch things like png images with an xpm extension

_________________
Web Programming - Pet Packaging 100 & 101

Last edited by technosaurus on Thu 15 Dec 2011, 05:34; edited 1 time in total
Back to top
View user's profile Send private message 
maxerro


Joined: 10 Oct 2010
Posts: 65

PostPosted: Wed 14 Dec 2011, 18:26    Post subject:  

We should also mention the 3rd (shifting) way, which preserves escape-codes in arguments, why some would say that it should be #1 choice.
Code:
#!/bin/sh
while [ $# -ne 0 ]; do
 case "$1" in
  *.pet|*.deb|*.rpm) busybox cp "$1" /dst1 &;;
  *.zip|*.rar|*.bz2) busybox cp "$1" /dst2 &;;
  *) busybox cp "$1" /dst3 &;;
 esac
 shift
done

As you see, the loop will work until it runs-out of arguments. Shift command trashes the $1 argument, so $2 becomes $1, $3>$2, and so on... That's why only $1 is used in every new iteration.
Back to top
View user's profile Send private message 
smokey01


Joined: 30 Dec 2006
Posts: 1857
Location: South Australia

PostPosted: Thu 15 Dec 2011, 00:10    Post subject:  

Very informative. Thanks.

The reason I needed to know this was to create a dropbox type application so I could use it with my own web server.

By just placing the script on the desktop or in OpenWith, I can upload software to my server. The script now allows me to mix file types but they are now copied to the appropriate directory on the server.

Using quotes around the variable allows copying of filenames with spaces, another good feature.

I am using a command line FTP utility to send the files to my web server.

All in all this has become a very useful application.

If you like I can post the FTP client I compiled with the final script or even PET it up.

Thanks so much for your help.

_________________
Puppy Software <-> Distros <-> Puppy Linux Tips
Back to top
View user's profile Send private message Visit poster's website 
Display posts from previous:   Sort by:   
Page 2 of 2 [29 Posts]   Goto page: Previous 1, 2
Post new topic   Reply to topic View previous topic :: View next topic
 Forum index » Advanced Topics » Additional Software (PETs, n' stuff) » Educational
Jump to:  

You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Powered by phpBB © 2001, 2005 phpBB Group
[ Time: 0.1000s ][ Queries: 12 (0.0076s) ][ GZIP on ]