Bash scripting - Get script name


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Bash scripting - Get script name
# 1  
Old 12-04-2009
Bash scripting - Get script name

All,

I am trying to migrate a script written in ksh (AIX) to bash (SuSE Linux). I encountered a number of errors when I am trying to run this script on bash and I was resolving them one-by-one. However, there is one error that remains. Here are the details:
The script has the following command:
Code:
>> script=`basename $0`

and this script variable is used in the code below while doing some error reporting. The error is that $0 in bash is producing
Code:
-bash

and not the usual
Code:
<directory path>/scriptname

output.

How do I achieve this? I am a little rusty in Shell scripting, so any help is appreciated!

Thanks.
# 2  
Old 12-04-2009
works for me
Code:
#!/bin/bash

script=$0

echo ${script}

script=`basename ${script}`

echo ${script}

output
Code:
# ./t.bash
./t.bash
t.bash

Where in your script is it being run?
# 3  
Old 12-04-2009
Thanks for your reply jsmithstl.

This script=`basename $0` is run in the very beginning where all the variable assignments are done. There are other variable assignments (typeset etc.) being done, and this is done in a section there.

Also, I have replaced the #!/usr/bin/ksh with #!/bin/bash and tried in vain.

Let me know if this info is what you asked from me!
# 4  
Old 12-04-2009
can you post your script down to the point where you echo $script?
# 5  
Old 12-09-2009
I apologize for the delay jsmithstl. Here is the script (at the variable declarations level). I am not echo-ing it, but I am using it as part of the USAGE function which is written further below. I did not attach that portion here...

So, here goes:
Code:
set -A mth JAN FEB MAR APR MAY JUN JUL AUG SEP OCT NOV DEC
integer rc=$success
agency_file=
ghost_file=
loss_file=
month_t_process="DEC"
prem_file=
script=`basename $0`
today_ymdHM=`date +%y\%m\%d%H%M`
USAGE="Usage: ${script}\n"

The workaround for now is, since this script was already running find in ksh, I am using "ksh -x" to run the script and this seems to be working fine. But, then, I would like this resolved in "bash" too. So, I would really appreciate your, for that matter anybody else's, input on this Smilie

Thanks.
# 6  
Old 12-09-2009
I ran it under bash and had issues with the SET and the integer, but `basename $0` worked fine.

Can you run it under bash and post the code you set USAGE, the code you are using $USAGE, the output, and any error message?

Code:
#!/bin/bash

set -A mth JAN FEB MAR APR MAY JUN JUL AUG SEP OCT NOV DEC
integer rc=$success
agency_file=
ghost_file=
loss_file=
month_t_process="DEC"
prem_file=
script=`basename $0`
today_ymdHM=`date +%y\%m\%d%H%M`
USAGE="Usage: ${script}\n"

printf "${USAGE}"

$ ./t.bash
./t.bash: line 3: set: -A: invalid option
set: usage: set [--abefhkmnptuvxBCHP] [-o option-name] [arg ...]
./t.bash: line 4: integer: command not found
Usage: t.bash
$
# 7  
Old 12-09-2009
"-bash" usually signifies a login shell. Can it be that you are running these commands straight from the command line instead of calling them from a shell script.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to block first bash script until second bash script script launches web server/site?

I'm new to utilities like socat and netcat and I'm not clear if they will do what I need. I have a "compileDeployStartWebServer.sh" script and a "StartBrowser.sh" script that are started by emacs/elisp at the same time in two different processes. I'm using Cygwin bash on Windows 10. My... (3 Replies)
Discussion started by: siegfried
3 Replies

2. Homework & Coursework Questions

Bash Scripting

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: Try running 'phone4 xyz' and see what happens. Modify your program so that if no matching name is found, an... (1 Reply)
Discussion started by: OmgHaxor
1 Replies

3. Shell Programming and Scripting

bash scripting help

Hi Guys i have a <script?> that spits out the location of each printer using snpget here is the code for i in `sed -n '/Start Printer/,/End Printer/p' /hosts/blah/etc/dhcp/hosts.conf | awk '!/^#/ {print $2}' | egrep -v \... (2 Replies)
Discussion started by: ab52
2 Replies

4. Shell Programming and Scripting

bash scripting help

have this code but when i run it i get this error ./pulse: line 2: and here is the code #!/bin/bash if ; then pulseaudio -k; fi what am i doing wrong thanks Adam (5 Replies)
Discussion started by: ab52
5 Replies

5. Homework & Coursework Questions

bash,scripting

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: i have to do this but i am confused, Create a file containing the bash functions which perform the... (1 Reply)
Discussion started by: CRAZYLITTLELOU
1 Replies

6. Shell Programming and Scripting

please help with Bash Scripting????

Hi, can anyone help me with my scrip please. I wanted do following tasks: 1. List all the directory 2. A STDIN to ask user to enter a directory name from listed directories 3. command to check if the directory exists( or a command to validate if the user entered a valid directory name) ... (2 Replies)
Discussion started by: eminjan
2 Replies

7. Shell Programming and Scripting

bash scripting help

hi all i'm trying to get a script working upon connection with pppd According to docu this happens ina clean environment with a couple of variables set, namely $1,$2,... To be able to execute the statements i included a path statement but i think i'm running into trouble with the variables -... (6 Replies)
Discussion started by: jimjones
6 Replies

8. UNIX for Dummies Questions & Answers

Should I do a bash scripting course?!

Hello, I'm confused (oh, yes). I'm running Linux at work. When I type 'echo $SHELL' I am told that I'm running tcsh. In /bin I note that both tcsh and bash are listed. Question 1: Can I swap to run bash rather than tcsh and, if so, how will this affect my system? Is there any advantage to... (6 Replies)
Discussion started by: macpete
6 Replies

9. Shell Programming and Scripting

Bash Scripting

Hello there peeps: There is a little piece of bash shell scripting problem i have, which i was hoping you could help me with. #!/bin/bash stored_word() { case $(( $$ % 8 )) in 0 ) echo "energy";; 1 ) echo "touch";; 2 ) echo "climbing";; 3 ) echo... (3 Replies)
Discussion started by: keyvan
3 Replies
Login or Register to Ask a Question