Simon336697
asked on
Bash script with a parameter
Hi guys!
If I wanted to change the sed_replace.sh below so I did not have to hardcode the filename, but could issue the filename with a wildard to act on in the for part of the script, would you guys know how i could change my current script?
========================== ========== ======= current sed_replace.sh
1 #! /bin/bash
2 for text in *menu4_it_sware_videogalle ries_*.php ; do
3 mv $text $text.old
4 sed 's/\.\.\/\.\.\///g' $text.old > $text
5 rm -f $text.old
6 done
========================== ========== ========== ==========
So, instead of just running....
sed_replace.sh
which would find only files of type *menu4_it_sware_videogalle ries_*.php ,
The script could "somehow be modified" to let you place a parameter after the script name and it will act on that in the for statement of the script...
For example:
sed_replace.sh *menu2_it_drp_webcasts_*.p hp
or
sed_replace.sh *menu2_it_backup_highslide s_*.php
Any help appreciated.
If I wanted to change the sed_replace.sh below so I did not have to hardcode the filename, but could issue the filename with a wildard to act on in the for part of the script, would you guys know how i could change my current script?
==========================
1 #! /bin/bash
2 for text in *menu4_it_sware_videogalle
3 mv $text $text.old
4 sed 's/\.\.\/\.\.\///g' $text.old > $text
5 rm -f $text.old
6 done
==========================
So, instead of just running....
sed_replace.sh
which would find only files of type *menu4_it_sware_videogalle
The script could "somehow be modified" to let you place a parameter after the script name and it will act on that in the for statement of the script...
For example:
sed_replace.sh *menu2_it_drp_webcasts_*.p
or
sed_replace.sh *menu2_it_backup_highslide
Any help appreciated.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Omar!
If the user doesnt input a paramater at the command line, is there a way to say to the user...
"Script not executed...please enter a filename.."
If the user doesnt input a paramater at the command line, is there a way to say to the user...
"Script not executed...please enter a filename.."
yes:
#! /bin/bash
if [ $# -eq 0 ]
then
echo "usage: $0 string"
exit 1
fi
for text in *$1*.php; do
mv $text $text.old
sed 's/\.\.\/\.\.\///g' $text.old > $text
rm -f $text.old
done
#! /bin/bash
if [ $# -eq 0 ]
then
echo "usage: $0 string"
exit 1
fi
for text in *$1*.php; do
mv $text $text.old
sed 's/\.\.\/\.\.\///g' $text.old > $text
rm -f $text.old
done
ASKER
Youre the best...
Last one :>)
What does the line
if [ $# -eq 0 ]
mean/do?
Thank you so much..
And the exit 1?
Last one :>)
What does the line
if [ $# -eq 0 ]
mean/do?
Thank you so much..
And the exit 1?
It means if the number of arguments ($#) is equal to (-eq) 0 then echo something then exit 1 (like an error code since by convention successful commands / scripts exit with code 0)
ASKER
You are brilliant.
Much appreciated mate that makes perfect sense to me now :>)
S
Much appreciated mate that makes perfect sense to me now :>)
S
Welcome !
ASKER