Canned Bash lessons - learn to use Bash commands
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.
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.
- ttuuxxx
- Posts: 11171
- Joined: Sat 05 May 2007, 10:00
- Location: Ontario Canada,Sydney Australia
- Contact:
ummm yes it has a menu list under /documentsHiDeHo 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
ttuuxxx
http://audio.online-convert.com/ <-- excellent site
http://samples.mplayerhq.hu/A-codecs/ <-- Codec Test Files
http://html5games.com/ <-- excellent HTML5 games :)
http://samples.mplayerhq.hu/A-codecs/ <-- Codec Test Files
http://html5games.com/ <-- excellent HTML5 games :)
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.
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.
- technosaurus
- Posts: 4853
- Joined: Mon 19 May 2008, 01:24
- Location: Blue Springs, MO
- Contact:
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
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: Select all
#!/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
Check out my [url=https://github.com/technosaurus]github repositories[/url]. I may eventually get around to updating my [url=http://bashismal.blogspot.com]blogspot[/url].
- technosaurus
- Posts: 4853
- Joined: Mon 19 May 2008, 01:24
- Location: Blue Springs, MO
- Contact:
Code: Select all
#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
Check out my [url=https://github.com/technosaurus]github repositories[/url]. I may eventually get around to updating my [url=http://bashismal.blogspot.com]blogspot[/url].
Is there a more efficient way to do this?
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:
TIA
Code: Select all
#!/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
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: Select all
#!/bin/bash
if [[ $1 == *.png or *.jpg or *.jpeg or *.PNG ]]
then
do something
else
do something else
fi
smokey01 wrote:Is there a more efficient way to do this?
Code: Select all
case $1 in
*.png|*.PNG|*.jpg|*.jpeg) process_pic;;
*.pet|*.deb|*.rpm) process_pkg;;
*) do_something_else;;
esac
- technosaurus
- Posts: 4853
- Joined: Mon 19 May 2008, 01:24
- Location: Blue Springs, MO
- Contact:
if we are still using ROX-Filer...maxerro wrote:smokey01 wrote:Is there a more efficient way to do this?Don't forget ;; at the end of each case.Code: Select all
case $1 in *.png|*.PNG|*.jpg|*.jpeg) process_pic;; *.pet|*.deb|*.rpm) process_pkg;; *) do_something_else;; esac
Code: Select all
rox $@
Code: Select all
#!/bin/sh
MIME=`file -b --mime-type $1`
/root/Choices/MIME-types/${MIME//\//_} $1
Check out my [url=https://github.com/technosaurus]github repositories[/url]. I may eventually get around to updating my [url=http://bashismal.blogspot.com]blogspot[/url].
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
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
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: Select all
#!/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
- technosaurus
- Posts: 4853
- Joined: Mon 19 May 2008, 01:24
- Location: Blue Springs, MO
- Contact:
or simply
BTW the reason I used file before was to catch things like png images with an xpm extension
Code: Select all
#!/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
Last edited by technosaurus on Thu 15 Dec 2011, 09:34, edited 1 time in total.
Check out my [url=https://github.com/technosaurus]github repositories[/url]. I may eventually get around to updating my [url=http://bashismal.blogspot.com]blogspot[/url].
We should also mention the 3rd (shifting) way, which preserves escape-codes in arguments, why some would say that it should be #1 choice.
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.
Code: Select all
#!/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
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.
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.