Passing Arguments in Shell Scripts

Hello everybody! First time posting here:)

Right, I am trying to pass arguments in my shell scripts using $1, $2 and $3 etc using if else statement........

This is my shell script which is based on serching the google website

#!/bin/sh
wget  -t1  -E -e robots=off - -awGet.log -T 200 -H -Priserless -O mylist1.html -U "Mozilla" "classical+music&ie=utf-8&oe=utf-8&aq=t&rls=org.mozilla:en-GB:official&client=firefox-a"
cat mylist1.html |sed -e 's#<a href#\n<a href#g' >mylist2.html
cat mylist2.html |sed -e 's#</a>#</a>\n#g' >mylist3.html
cat mylist3.html |sed -n -e '/^<a href="http:/p' >mylist4.html
cat mylist4.html |sed -n -e '/\.google/!p' >mylist5.html
cat mylist5.html |sed -n -e '/[0-9]*[0-9]*[0-9]*\.[0-9]*[0-9]*[0-9]*\.[0-9]*[0-9]*[0-9]*\.[0-9]*[0-9]*[0-9]*/!p' >mylist6.html

Could somebody please quide me through!

Thanks

Kev

I don't see any arguments used in your script, nor an if ... else statement.

Thanks "cfajohnson" for replying.....

What I am trying to do is to pass arguments in my script such as
-h, -- help, -v, --version, -V, --Verbose, -o filename, --output=filename
-s searchphrase, --search=searchphrase, -f format, --format=format

Since I am a beginner in shell scripting I am not able to figure it out.....

args=
while [ $# -ge 1 ]
do
  case $1 in
    -h | --help) echo help; exit ;;
    -v | --version) echo Version 0.01; exit ;;
    -V | --Verbose ) versbose=1 ;;
    -o ) filename=$2; shift ;;
    -o*) filename=${1#-o} ;;
    --output=) filename=${1#*=} ;;
    -s) searchphrase=$2; shift ;;
    -s*) ${1#-s} ;;
    --search=) searchphrase=${1#*=} ;;
    -f) format=$2; shift ;;
    -f*) format=${1#-f} ;;
    --format=) format${1#*=} ;;
    -*) echo invalid option >&2; exit 1 ;;
    *) args="$args
$1"
       ;;
  esac
  shift
done
oldIFS=$IFS
IFS='
'
set -f
set -- $args
IFS=$oldIFS
set +f

SIR you are a genius:b:

okay so according to your code:

-s) searchphrase=$2; shift ;;

So my case statement for $2 would look like this?:

case $2
echo 'wget  -t1  -E -e robots=off - -awGet.log -T 200 -H -Priserless -O mylist1.html -U "Mozilla" "classical+music&ie=utf-8&oe=utf-8&aq=t&rls=org.mozilla:en-GB:official&client=firefox-a"'

case $3 ....and so on

Please correct me if I am wrong

Thanks a Mil!

No, you only need the one case statement; it reads all the parameters on the command line. Whatever was passed as the argument to -s will be in the variable $searchphrase.

That line doesn't use anything that has been passed on the command line (it also lacks a URL), so why do you need to parse anything to use it?

Yup you are correct

Sorry sir, could you explain this part please!!!:o

Because say if the user runs "test -s" in the command line will the output go to standard output?

Thanks

The case statement reads the options on the command line and sets variables based on what it finds.

If you entered test -s, you should get an error message (the code I posted isn't that sophiticated, however), because, according to what you posted before, it should have a searchphrase argument.

Sorry again to bother you....

I noticed you changed your code would it be possible to explain it please as the previous one looked less complicated:o

Also see if type in ./test -o mylist4.html it doesn't give any output?

Thanks

There's very little difference.

The revision allows non-option arguments to be included anywhere on the command line. When a non-option argument is encountered, it is added to the args variable. After the options have been parsed, those arguments are placed back into the positional parameters.

You gave a searchphrase argument in your initial post.

What do you have in mind?

Ignore the above as I have been so confused with arguments and etc that I forgot what I was writting.....:smiley:

Actual question was when I pass a argument say "./test -s mylist4.html" or "./test -o mylist4.html"
there is no output???

That depends on what's in the script. It can do anything you want it to.

The file mylist4.html is present inside my script....should it not return a value that is If I passed the searchphrase ./test --search mylist4.html!!

Thanks

Do you have a command in the script that prints a value? If you do, it will. If you don't, it won't.

That depends on what your script does with it.

Putting something on the command line does nothing but make that argument available to the script. It's up to the script to do something (or nothing) with it.

Right following our conversation:

This is a if statement I did which doesn't seem to work?

if [ -s /bin/bash ]
then 
     echo "file /bin/bash exists"
else
     echo "file /bin/bash exists"
fi

If I do a search ./test -s mylist1.html

it displays files exists

but the if I type ./test -s mylist14.html

still says the same

  1. You tell it to print the same thing whether the test succeeds or fails.

  2. You don't use the command-line argument anywhere. You are always testing for /bin/bash.

case $1 in
     -s) searchstring=$2 ;;
esac

if [ -s "$searchstring" ]
then
  echo  "file $searchstring exists"
else
  echo  "file $searchstring does not exist"
fi

Thanks for correction!

Now, if I only run ./test in the terminal to create my .html files it shows the echo message "file does not exists" even though I didn't pass in the '-s' argument!

Did you fix your wget statement? The one you posted has no URL.

Yup!

wget -tl -E -e robots=off - - awGet.log -T 200 -H -Priserless -O mylist1.html -U "Mozilla" "http://www.google.co.uk/search?classical+music&ie=utf-8&oe=utf-8&aq=t&rls=org.mozilla:en-GB:official&client=firefox-a"

Please post:
the full script

the exact output (cut and paste)

what exactly happened

what you expected to happen that didn't