Link to home
Start Free TrialLog in
Avatar of Simon336697
Simon336697Flag for Australia

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_videogalleries_*.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_videogalleries_*.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_*.php
or
sed_replace.sh *menu2_it_backup_highslides_*.php

Any help appreciated.
ASKER CERTIFIED SOLUTION
Avatar of omarfarid
omarfarid
Flag of United Arab Emirates image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Simon336697

ASKER

Thanks omar!

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.."
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
Youre the best...
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)
You are brilliant.

Much appreciated mate that makes perfect sense to me now :>)

S
Welcome !