script with command line arguments

Hi,

whats wrong with the following script. when i run this script, no out put has been generated

This script will be executed with one argument.
I have saved this script as test_script [no extension] and chmod 755 test-script.
Also, in the script how to echo variable value. i tried echo $<variable name> and noting comes up

#!/bin/bash
PROCNAME="$1"
if $# -ne 1 ]; then
echo “usage: $0 PROCNAME missing”
exit 1
fi
cd local/home/userid/process/bin
./util EXPORTLIB /"$PROCNAME" -ax +s

Thanks

Nothing probably but then I’m not sure what you’re doing. Your testing $# not $1 which iirc is how many commandline parameters which = 4 bug shoot it with some echo’s. http://tldp.org/LDP/abs/html/internalvariables.html

#!/bin/bash
echo "This is all (" $* ") This is para 1 (" $1 ") This is how many para (" $# ")"
PROCNAME="$1"
if  $# -ne 1 ]; then
echo "usage: $0 PROCNAME missing"
exit 1
fi

Any way having been playing with getopts myself recently…

Perhaps you’re after getopt/s one to get you started Getopt and getopts after that google.

thanks for your input.

this is what i want to do.

1-run script with one argument <procname>

2-assign that argument to variable PROCNAME

3-if number of argument less than 1 then show error

4-cd to particular folder

5-execute util part with PROCNAME

6-exit

Why are you using a wrapper why don’t you just fix the util?

Any way what you’re doing will work you just need to write the test better. $# not equal 1 matches every time except on one para/arg. So $# <(-lt) 1 will(or do if else on a equal match i.e 0), but your example has $PROCNAME as the second arg(with a slash not sure what that is about, beyond guessing you’re assigning a var).

You could continue the test and then construct the command i.e 2nd para = do.

Though I think this is one for getopts myself.

create a flag that takes an argument then do but I also would be doing it to the util.

Part of the problem seems to stem from you say you want one arg but are using 4.

Please, this is unreadble. Can you repost the script between CODE tags (select the text and use the # sign above).
Then also post in the same way how you call the script.
While I understand and believe you did the chmod, it is much better to simply post the call and output of

ls -l test-script

All these things then show what the computer sees and not what you think what you are doing. That may sound a bit harsh, but a lot of programming problems are there because after looking and looking at the text one still does not see what is wrong and another can only se what is wrong with the real text and not with what the OP thinks it is.

#!/bin/bash
#PROCNAME="$1"
#if $# -ne 1 ]; then
#echo “usage: $0 PROCNAME missing”
#exit 1
#fi
#cd local/home/userid/process/bin
#./util EXPORTLIB /"$PROCNAME" -ax +s

ls -l test.sh
-rwxr-x— 1 [userid] [groupid] 227 2010-04-15 17:04 test.sh

–in above, purposely i have removed the actually userid and gorupid

Apologies for some of the above, I missed the bottom lines I’m still not really sure what you’re saying isn’t working though.

It checks it fine and returns the message. Unless you mean calling the util my suggestion would be to place it in ~/bin as it is in the users path(Though you’re escaping /" one not sure why you want the var to expand (Bash quoting)](http://www.gnu.org/software/bash/manual/bashref.html#Quoting)). Also with any script the whole path is better than relative.

In regards to what hcvv suggested If you highlight the code then click the # in the message box it’ll wrap the code in a code box. Or place it between the tags it creates. code ] and closing /code ] I’ve had to break it show you(Remove spaces).

As feathermonkey explains you, and as I thought I asked you, please use CODE tags around code. Programming has much to do with lay-out and white space. When using a proportional font it is very difficult to see white space on unexpected places. Also when all the lines in your script start with a # there will not be very much left to run as they are all comment line only. PLEASE CUT/PASTE from a terminal window and put between CODE tags.

I made a small example how this would look like:

henk@boven:~> ls -l show
-rwxr--r-- 1 henk wij 73 apr 17 11:27 show
henk@boven:~> cat show
#!/bin/ksh
echo "My name is ${0} and I am called with ${#*} parameters."
henk@boven:~> ./show a b c
My name is ./show and I am called with 3 parameters.
henk@boven:~>

We hope you understand what we mean. We are voluntary spending time on your problem and want to use this time as efficient as possible.

Why not try this and see if it works.

#!/bin/bash
if  "$1" != "" ] ; then
   cd local/home/userid/process/bin
   ./util EXPORTLIB /$1 -ax +s
else
   echo "usage: "$0" "$1" missing"
fi
#End of Script

Thank You,

thanks folks. so the following script is working which is taking only one argument.

for same script,instead of one argument, i like to take two arguments and this script sudo to admin id.

ls -l test.sh
-rwxr-x— 1 186 2010-04-21 13:45 test.sh
cat test.sh
#!/bin/bash
if “$1” != “” ] ; then
cd local/home/userid/process/bin
./util EXPORTLIB /$1 -ax +s
else
echo “usage: “$0” PROCNAME missing”

fi

I don’t know what you want to achieve … but the following code snippet will do what you want


#!/bin/bash

if  "$#" == "2" ] ; then
   command="ls"
   user="admin"
# do some stuff
# ...
# now execute command with sudo
   sudo -u $user $command
else
   echo "usage: "$0" PROCNAME xyy missing"
fi

I do not know where you want the other command line options, but here are two possible choices.

#!/bin/bash
if  "$1" != "" ] ; then
cd local/home/userid/process/bin
./util EXPORTLIB /$1 -ax +s $2 $3 $4
else
echo "usage: "$0" PROCNAME missing"
fi 

or 

#!/bin/bash
if  "$1" != "" ] ; then
cd local/home/userid/process/bin
./util EXPORTLIB /$1 $2 $3 $4 -ax +s 
else
echo "usage: "$0" PROCNAME missing"
fi 

Thank You,