$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:
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..
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
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.
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..
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.
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..
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.
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..
Cool..nice deBugging..
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.
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 /.
Cool..nice deBugging..
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.
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)