$0 not giving script name


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting $0 not giving script name
# 1  
Old 03-15-2011
$0 not giving script name

Why does $0 return the word usage rather than the script name when used in a function?

Baffeled on this one, any help appreciated.


Code:
usage()
{
  echo "$0 -cs <number of batches>\n"
  echo "$0 -c 4"
  echo "$0 -s 4"
# echo "-c = Create"
# echo "-s = Submit\n"

  exit 1
}



$ Create_batch.ksh
usage -cs <number of batches>

usage -c 4
usage -s 4

# 2  
Old 03-15-2011
Because $0 inside a function is not the same as outside the function. Outside it is the name of the script, as you noticed and inside the function it is the function's positional parameters.
Besides that I can't see where you call this function at all so you might want to write something like:
Code:
#!/bin/bash

usage()
{
  scriptname=$1
  # $1 is the 1st parameter handed over by calling the function (down there the $0 in this case), which is the name of the script
  echo "$scriptname -cs <number of batches>\n"

  exit 1
}

usage $0

exit 0

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Script giving error-Unable to identfiy

#!/bin/sh #!/bin/prel #set -x while getopts ":n:t:" opt; do case "$opt" in n) host=$OPTARG ;; t) targ=$OPTARG ;; *) break ;; esac done if ping -c 2 $host >/dev/null 2>&1 then echo Host... (17 Replies)
Discussion started by: Vishal_dba
17 Replies

2. Shell Programming and Scripting

Script not giving o/p

Hi Below is snippet from script which is not giving the o/p.script name is alarm.sh #!/bin/sh out=`awk '(NR>1) {print $9;exit}' alarm` echo $a however when i simply run the above command i an getting the o/p $ out=`awk '(NR>1) {print $9;exit}' alarm` $ echo $a 6... (2 Replies)
Discussion started by: scriptor
2 Replies

3. Shell Programming and Scripting

Giving Input in a script

Hi, I am a newbie to scripting. I want to know something..Is there any way that I can do this? Here is the thing.. there are so many printer queues in which i need to change a certain option.. am using the hppi utility and i need to modify the printer configuration individually going to each... (3 Replies)
Discussion started by: aksijain
3 Replies

4. Shell Programming and Scripting

My script is not giving result for 2 or more arguments

Hi, I am new to shell scripting and my below script is not giving result for 2 or more arguments. Can anyone help pls. #!/bin/sh sname=$(basename $(readlink -nf $0)) echo "This is $sname, running at $(date)" echo "It is running on $(hostname)" echo "Script being run by" echo " User... (3 Replies)
Discussion started by: baigmd
3 Replies

5. Shell Programming and Scripting

how to run the script without giving the password.

Hi all, I need some help in my script... I need to login into multiple servers from one Unix box and need to login as a superuser account and then I need to perform some actions on the server. For this, i'll use for loop to provide the server names to the script, but for every login as a... (8 Replies)
Discussion started by: raghu.iv85
8 Replies

6. Shell Programming and Scripting

awk script giving unstable results

Hi all Here I came accross a situation which i am unable to reason out... snippet 1 psg ServTest | grep -v "grep" | grep -v "vi" | awk '{ pgm_name=$8 cmd_name="ServTest" gsub(/]*/,"",pgm_name) if(pgm_name==cmd_name) { print "ServTest Present =" cmd_name} }'... (10 Replies)
Discussion started by: Anteus
10 Replies

7. UNIX for Advanced & Expert Users

script giving error

Hi All, i have an small issue... echo " " eval x=$@ export x=`echo $x` echo $x ssh user@ipadrss; cd /mbbv/home/; cd /mbbv/home/orange/orange/ echo pwd bash samplescript.sh $x above is my script which will triger from server A and will connect to server B for some... (2 Replies)
Discussion started by: Shahul
2 Replies

8. Shell Programming and Scripting

script not giving the desired output

Hi, I have a script in which an entry like this ..... FILENAME_B="PIC_${DATE}0732*.JPG" The script connects to an ATM and pull a pic file from it.The format for the file is like PIC_2008061400000001.JPG in the ATM. Means 1st 8 digit is the date(YYYYMMDD) field 2nd 8 digit means hrs... (2 Replies)
Discussion started by: Renjesh
2 Replies

9. Shell Programming and Scripting

Script giving wrong results....

hi, I have this script which gives me the result... #! /usr/bin/sh set -x cd /home/managar a=1 while true do if then echo " File log.txt exists in this directory " exit 0 fi echo " File has not arrived yes..." sleep 3 let a=a+1 if then (1 Reply)
Discussion started by: mgirinath
1 Replies

10. Shell Programming and Scripting

Giving input through script

Script 1.ksh ========= /home/adw/a.ksh << ** a b Script 1.ksh is working fine even without ending "**" Script 2.ksh ========= if then /home/adw/a.ksh << ** a b fi But the script 2.ksh is giving error "syntax error : `<<' unmatched". Is it bcoz of fi. (1 Reply)
Discussion started by: radhika03
1 Replies
Login or Register to Ask a Question