comamnds running interactively, failed in script wrigint


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting comamnds running interactively, failed in script wrigint
# 8  
Old 09-25-2012
$0 does not mean "path to my program". That's what it traditionally is, and it's what you get whenever a shell runs a program, but when something that's not a shell runs your program, that's not guaranteed by any means. cron will give weird values for $0. CGI gives weird values for $0. And so forth.

So yes... It does when you run it, from a terminal, giving it an absolute path. When things other than you run it, from non-terminals, in ways and from places you never did, you might get something else.

These two C programs show how it works:

Code:
$ cat printzero.c

#include <stdio.h>

int main(int argc, char *argv[])
{
        printf("$0 is %s\n", argv[0]);
}

$ cat zero.c

#include <unistd.h>
#include <stdio.h>

int main(int argc, char *argv[])
{
        if(argc < 2)
        {
                fprintf(stderr, "usage:  ./zero program zeroth-argument\n");
                return(1);
        }

        // First argument is the program name and path.  Second is $0.
        // Note that NOTHING forces them to be the same.
        execl(argv[1], argv[2], NULL);
        return(1);
}

$ gcc zero.c -o zero
$ gcc printzero.c -o printzero
$ ./printzero

$0 is ./printzero

$ ./zero ./printzero slartibartfast

$0 is slartibartfast

$

So, when you run ./printzero by itself, the shell runs it, and $0 is what you expect.

When the zero program runs printzero, it can give whatever weird and wild value it wants to $0, and does.

Some shells will make a halfhearted attempt to correct the value of $0 into something 'useful', but they're not psychic. They can't recover paths they were never given in the first place.

So $0 is only what the program calling it says it is. It's not necessarily the information you want. Do not depend on it if you want your scripts to be run by cron and the like.

Last edited by Corona688; 09-25-2012 at 06:14 PM..
# 9  
Old 09-25-2012
Ohh,
Thanks for the explanation.
just wondering...can I rely this $0 while working with shell script probably?
If not, then what could be the other way to write it in shell script.
BTW, Google helped me in finding that $0 Smilie

Code:
checking the current dir
go to other dir
execute something there
come back to previous directory

Thanks
# 10  
Old 09-25-2012
Quote:
Originally Posted by nrjrasaxena
just wondering...can I rely this $0 while working with shell script probably?
Only if you know for a fact it's going to be launched from a shell and not by cron or anything.
Quote:
If not, then what could be the other way to write it in shell script.
Either code the path into the script, take it as a parameter, or make it something you can grab by sourcing /etc/profile. That's what it's for, really -- storing global values like that.

Most programs don't keep their data in the same location as the executable, you know, especially if they need to write to it. Writing to the same folder your program exists in could be a recipe for disaster if your program goes wrong -- might delete itself, rename itself, truncate itself, overwrite itself, etc, or be manipulated into doing so by clever/malicious people.
# 11  
Old 09-25-2012
Quote:
Originally Posted by Corona688
Either code the path into the script, take it as a parameter,
I used to do it.. But every time I used to make different directory and cp the content..I always used to forget to change this variable..and I really had hard time dealing with the consequences..Smilie

So I prefer taking out some time and do Google about the solution..
Also sombeody was suggesting me that $pwd serve the purpose in shell script. I did not find anything on google related with this.

Thanks,
# 12  
Old 09-25-2012
Quote:
Originally Posted by nrjrasaxena
I used to do it.. But every time I used to make different directory and cp the content..I always used to forget to change this variable..and I really had hard time dealing with the consequences..Smilie
So put more error-checking in your code. Check for the presence of certain files. If they're not there,complain that such-and-such variable hasn't been set and quit.

Code:
#!/bin/bash

die() {
        echo -- "$@" >&2
        exit 1
}

. /etc/profile

[ -z "$MYDIRVAR" ] && die "MYDIRVAR not set in /etc/profile"
[ -e "$MYDIRVAR" ] || die "MYDIRVAR=$MYDIRVAR does not exist"
[ -d "$MYDIRVAR" ] || die "MYDIRVAR=$MYDIRVAR is not a directory"
[ -f "$MYDIRVAR"/myscript.sh ] || die "MYDIRVAR=$MYDIRVAR does not contain myscript.sh"

echo "MYDIRVAR=$MYDIRVAR, folder exists and contains myscript.sh, ready to go" >&2

Quote:
Also sombeody was suggesting me that $pwd serve the purpose in shell script. I did not find anything on google related with this.
pwd is your shell's working directory.

Things like cron or CGI scripts and so forth, which don't actually login as you, are likely to have a $pwd of /.

Last edited by Corona688; 09-25-2012 at 06:36 PM..
This User Gave Thanks to Corona688 For This Post:
# 13  
Old 09-25-2012
Cool..nice deBugging..Smilie
So if I assume this part to be solved. The question remains that the script failed to execute a simple line of command..why..?? No idea !!

Quote:
Originally Posted by Corona688
So put more error-checking in your code. Check for the presence of certain files. If they're not there,complain that such-and-such variable hasn't been set and quit.

Code:
#!/bin/bash

die() {
        echo -- "$@" >&2
        exit 1
}

. /etc/profile

[ -z "$MYDIRVAR" ] && die "MYDIRVAR not set in /etc/profile"
[ -e "$MYDIRVAR" ] || die "MYDIRVAR=$MYDIRVAR does not exist"
[ -d "$MYDIRVAR" ] || die "MYDIRVAR=$MYDIRVAR is not a directory"
[ -f "$MYDIRVAR"/myscript.sh ] || die "MYDIRVAR=$MYDIRVAR does not contain myscript.sh"

echo "$MYDIRVAR exists and contains myscript.sh, ready to go" >&2

pwd is your shell's working directory.

Things like cron or CGI scripts and so forth, which don't actually login as you, are likely to have a $pwd of /.
# 14  
Old 09-25-2012
Quote:
Originally Posted by nrjrasaxena
Cool..nice deBugging..Smilie
So if I assume this part to be solved. The question remains that the script failed to execute a simple line of command..why..?? No idea !!
Things like cron, cgi-scripts, and so forth also have a pretty minimal $PATH. It probably couldn't find scram. In short, everything local to your shell login that you've been depending on when you write your scripts, can't be depended on if you're not doing a shell login.

You can get a more complete PATH by doing . /etc/profile where such global settings are supposed to be kept. That's done automatically for you on login of course, but for cron and such, not unless you ask for it.

You could also do /absolute/path/to/scram instead.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Command works interactively but not in bash script

The below command works in the terminal interactively but not as part of a bash script. I though maybe I needed to escape the "$dir" so it isn't interpreted literally, but that's not it. Thank you :). interactively in terminal dir=/path/to new=$(ls "$dir"/*.csv -tr | tail -n 1) && echo... (6 Replies)
Discussion started by: cmccabe
6 Replies

2. UNIX for Beginners Questions & Answers

Expect scripting issue, works interactively when doing commands in cli, does not work in script

Hi; problem may be obvious, simple but I have to say it is somehow not easy to locate the issue. I am doing some word extracting from multiline text. Interacting in CLI seems to work without issues. First step is to add multiline text to a variable. expect1.1> expect1.1> set... (2 Replies)
Discussion started by: aldowski
2 Replies

3. Solaris

Failed to identify flash rom on Sunfire V240 running Solaris 10

Hi Guys, I have performed OBP & ALOM upgrade on V240 system. One of my system, running Solaris 10, having issue to identify flash rom during ALOM 1.6.10 version upgrade (OBP upgraded to latest one). May I know what the reason of this error and how can I fix it so I can upgrade ALOM using... (0 Replies)
Discussion started by: myrpthidesis
0 Replies

4. Shell Programming and Scripting

Is it possible to create a here document by running a script interactively?

hi, i have script which installs around 20 packages. during installation of each package script will be prompted for user input. i need to run the script in non-interactive by using here document. is it possible to generate here document by running the script in interactive mode on Solaris?... (1 Reply)
Discussion started by: snreddy_gopu
1 Replies

5. Shell Programming and Scripting

How to fetch running/failed process

I am trying to fetch failed process but while doing that unable to do so. like; (1)ps -ef | grep snmpCollect o/p is coming like - root 12423 4393 1 19:44:06 pts/0 0:00 grep snmpCollect (2)ps -ef | grep sttps o/p- root 15517 4393 0 19:53:24 pts/0 0:00 grep sttps... (6 Replies)
Discussion started by: kumarabhi84
6 Replies

6. Shell Programming and Scripting

how can the search and replace can be done interactively

hi, To search and replace a string in multiple files i am using following command: find . -name '*.txt' -print0 |xargs -0 perl -pi -e 's/find_string/replace_string/g' I want to be prompted while replacing the string. how this can be done. thanks for every help. (2 Replies)
Discussion started by: aks__
2 Replies

7. UNIX for Dummies Questions & Answers

Retrieving output interactively

Hi. I have a situation where I need to constantly read a command's output in order to loop through it to determine something that is happening over our system. The command td <filename> interactively prints 'filename' as text is written to it and this is redirected to standard output. I have been... (8 Replies)
Discussion started by: fidodido
8 Replies

8. Filesystems, Disks and Memory

skgpspawn failed running oracle db 9.2.0.5.0 on aix 5.3

Hi, I am running an oracle db 9.2.0.5.0 on ibm p5 550 aix 5.3 with 10g ram, 10G swap space 3 database instances each SGA about 500Meg. I am getting the following error in my alert log file from time to time: skgpspawn failed:category = 27142, depinfo = 11, op = fork, loc = skgpspawn3 ... (0 Replies)
Discussion started by: hawkerpacific
0 Replies

9. Shell Programming and Scripting

Editing file during running ksh in HP failed

Hi falks, I have the following program: #!/bin/ksh ed -s /home/ias/v9.0.3/j2ee/OC4J_RiGHTv_HPCD2/applications/Xbip/Xbip/WEB -INF/config/application.properties <<EOF >/dev/null 1 d . d . a application.filePath.core = /core-HPCD2/Html/ application.filePath.xbip =... (2 Replies)
Discussion started by: nir_s
2 Replies

10. UNIX for Dummies Questions & Answers

UNIX comamnds

Hi everybody! I am new to unix; however I already now a few unix commands. I would like a complete document, pdf or anything, to learn them all. Hope you can help me!:confused: (4 Replies)
Discussion started by: cybergang007
4 Replies
Login or Register to Ask a Question