Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

get shell script name within a script 2

Status
Not open for further replies.

philipose

Programmer
Dec 24, 2003
137
US
Hi,
Does anyone know how could I find the name of a shell script from itself ?

I was trying
PGM_NAM=`basename ${0} .sh`

The only issue with that I think is when the shell script is scheduled via the at command or crontab. In such situations the value of PGM_NAM will be ksh or other shell.

Any thoughts

Thanks
philipose
 
Try this:

DIRNAME=${0%/*}
PROGNAME=${0##*/}

Regards,
Khalid
 
Khalid,
I tried your suggestion of
PROGNAME=${0##*/}

But when I ran my shell script using the at command, I got "ksh" instead of my shell script name.
 
echo $0| awk -F"/" '{print $NF}'

For cronjobs I usually don't use just the name but the absolute path just to be sure and not to rely on some environment/paths being set.

laters
zaxxon
 
If you stick
Code:
echo $0
towards the top of your script exactly what output do you get? From your response to khalidaaa's post it looks like you'll get something like 'ksh myscript' but, until we know, it's difficult to see how to process it.

On the internet no one knows you're a dog

Columb Healy
 
Just tried it out on my Debian Linux box and an AIX box:

Debian Linux, bash:
Code:
echo $0| sed 's/.*\/\([^\/].*$\)/\1/g'
root@isau02:/data/tmp> crontab -l
* * * * * /data/tmp/mach.ksh >> /data/tmp/mach.out 2>&1
root@isau02:/data/tmp> cat mach.out
mach.ksh

Cronjob on AIX 5.3 with ksh shows same output.

laters
zaxxon
 

Below is the script I am scheduling on the AIX 5.3 server

hbuhydpf@usvh2euap2a:/pfdirect2/programs/pre_etl>ls -l check.sh
-rwxr-x--- 1 hbuhydpf pfsusr 11359 Jan 22 09:43 check.sh

I am scheduling the script with the below command
at -t 200801220947 <check.sh

I direct echo $0 and echo ${0##*/} to a log file and get only ksh.

What I need is check.sh or check.

Thanks
Philipose
 
well, if you schedule it like this:

[tt]echo "/path/to/your/dir/with/check.sh"|at -t 200801221200[/tt]

you'll be fine.

With your current setup, you're scheduling [red]the contents of[/red] check.sh, not check.sh itself.


HTH,

p5wizard
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top