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
# 1  
Old 09-25-2012
comamnds running interactively, failed in script wrigint

Hi all,
I am trying to run these commands in shell script, but there seem to be some mistake. Somehow it is not accepting the last important comamnd "eval `scram runtime -sh`". However, when I try them interactively, it works all fine.
Code:
[pooja04@cmslpc07 v6]$ SOURCEPATH=/uscms_data/d3/pooja04/CMSSW_5_3_3_patch2/src/ElectroWeakAnalysis/MultiBosons/test/
[pooja04@cmslpc07 v6]$ cd $SOURCEPATH
[pooja04@cmslpc07 test]$  eval `scram runtime -sh`

SCRIPT
====
Code:
SOURCEPATH=/uscms_data/d3/pooja04/CMSSW_5_3_3_patch2/src/ElectroWeakAnalysis/MultiBosons/test/

SetEnv () {
    SCRIPT=`readlink -f $0`

    SCRIPTPATH=`dirname $SCRIPT`
    cd $SOURCEPATH
    echo  $SOURCEPATH
    eval `scram runtime -sh`
    cd $SCRIPTPATH
}

any piece of hint is appreciable.

Thanks
# 2  
Old 09-25-2012
You cannot depend on $0. It may be an absolute path, relative path, or no path depending on how the program is run.
# 3  
Old 09-25-2012
hi corona,
I did not understand. I have tested that piece of code. It just cd in the current working directory.
# 4  
Old 09-25-2012
Quote:
Originally Posted by nrjrasaxena
hi corona,
I did not understand. I have tested that piece of code. It just cd in the current working directory.
You've tested it in your home dir, by running it in a manner where $0 can be depended on.

Except, you can't depend on $0. When something that's not you runs it, especially automatic things like cron and so forth, you have no idea what your current dir, and the contents of $0, are going to be. It might start in / with a useless $0 like -bash or something. Or it might give an absolute path you weren't expecting. etc. I keep telling people not to do this but until it breaks down like this, they never understand why.

Try typing echo $0 into your shell for example. I get -bash. That's not any sort of directory.
# 5  
Old 09-25-2012
Quote:
Originally Posted by Corona688
You've tested it in your home dir, by running it in a manner where $0 can be depended on.

Except, you can't depend on $0. When something that's not you runs it, especially automatic things like cron and so forth, you have no idea what your current dir, and the contents of $0, are going to be. It might start in / with a useless $0 like -bash or something. Or it might give an absolute path you weren't expecting. etc. I keep telling people not to do this but until it breaks down like this, they never understand why.

Try typing echo $0 into your shell for example. I get -bash. That's not any sort of directory.

I just tested that piece of code as script like this [1] and the output is [2] just as I want it to be.
It take me back to my working directory.

[1]
Code:
SetEnv () {
    SCRIPT=`readlink -f $0`
    echo $SCRIPT
    SCRIPTPATH=`dirname $SCRIPT`
    cd $SOURCEPATH
    echo  $SOURCEPATH
    eval `scram runtime -sh`
    cd $SCRIPTPATH
    echo $SCRIPTPATH
}

[2]
Code:
[pooja04@cmslpc17 v6]$ ./script.sh  setenv
Tue Sep 25 15:33:30 CDT 2012
/uscms/home/pooja04/script/AnalysisCode/Code42012/v6/Script4JobSubmit.sh
/uscms_data/d3/pooja04/CMSSW_5_3_3_patch2/src/ElectroWeakAnalysis/MultiBosons/test/
/uscms/home/pooja04/script/AnalysisCode/Code42012/v6
[pooja04@cmslpc17 v6]$

# 6  
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..
# 7  
Old 09-25-2012
What's the output of scram runtime -sh ? Is it different in the script or from the command line?
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