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.
LinuxLinux Distributions

Avatar of undefined
Last Comment
omarfarid

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
omarfarid

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
See how we're fighting big data
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
ASKER
Simon336697

Thanks omar!

ASKER
Simon336697

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.."
omarfarid

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
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
ASKER
Simon336697

Youre the best...
Last one :>)
What does the line

if [ $# -eq 0 ]

mean/do?

Thank you so much..

And the exit 1?

omarfarid

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
Simon336697

You are brilliant.

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

S
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
omarfarid

Welcome !