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
What's the output of scram runtime -sh ? Is it different in the script or from the command line?
# 5  
Old 09-25-2012
Quote:
Originally Posted by RudiC
What's the output of scram runtime -sh ? Is it different in the script or from the command line?

Hi RudiC,
I have to execute it to set the correct working environment. A simple test to check it its working is following
Code:
SOURCE=/uscms_data/d3/pooja04/CMSSW_5_3_3_patch2/src/ElectroWeakAnalysis/MultiBosons/test/
[cmslpc@test]$ cd $SOURCEPATH
[cmslpc@test]$ eval `scram runtime -sh`
[cmslpc@test]$ root -l 
root [0]

So after execution of that command, it recognize the ROOT.

While with my script, it give the following message:

script.sh
Code:
#!/bin/bash                                                                                                                                           
date

SOURCE=/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
}

if  [ "$1" = "setenv" ]; then
    SetEnv
    exit 0
fi

Execution:
Code:
[cmslpc@test]$ ./script.sh  setenv
Tue Sep 25 15:21:48 CDT 2012
/uscms_data/d3/pooja04/CMSSW_5_3_3_patch2/src/ElectroWeakAnalysis/MultiBosons/test/
[cmslpc@test]$ root -l
-bash: root: command not found
[cmslpc@test]$


Thanks,
# 6  
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.
# 7  
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]$

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