sed command working different in linux environment.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting sed command working different in linux environment.
# 1  
Old 11-25-2011
sed command working different in linux environment.

Hi
I tried running the code
Code:
scrname=`whence $0 | sed -e 's/\.\///g'`

where $0 is substituted by cm_dsjobrun.sh
in unix env then the value it returns me is

SCRNAME=/data/ds/dpr_ebicm_uat/etl/cm3_0/scripts/shell/cm_dsjobrun.sh

whereas i ran the same code on linux env

The value its returning me as in scrname as ''

I am not able to figure out why this is happening, could you all please help me. What changes should i make in the command so that it will return me the same output as it is giving in unix enviornment i.e .
Code:
SCRNAME=/data/ds/dpr_ebicm_uat/etl/cm3_0/scripts/shell/cm_dsjobrun.sh

Please help

Thanks.

---------- Post updated at 07:09 AM ---------- Previous update was at 06:45 AM ----------

I read on the whence command one point i would like to share is it detects it detects commands and aliases, and searches your path.
So my script path is searched properly in unix env... whereas this is not happening in the linux enviornment.
Could you all please help me in modifying the command so that i get the exact path of my script.

---------- Post updated at 11:35 PM ---------- Previous update was at 07:09 AM ----------

HI

To get the path in linux i tried
Code:
scrname={PWD}/$0

Where $0 will be script name.

My question is if i am using PWD instaed of whence in linux what difference it can cause to my script.

Thanks
# 2  
Old 11-25-2011
'unix env' does not mean very much. Which OSs are you actually talking about?

whence is most likely returning nothing because the directory containing the script is not in $PATH - which is nothing to do with differences in the OS, you just have them set up differently.

Whether ${PWD} will work depends on how and where the script is being run.
# 3  
Old 11-25-2011

If you want a script to be portable, you must use standard commands; whence is not standard.
# 4  
Old 11-28-2011
Where can i find the $PATH variable in linux. Is it specific to some file. If yes, what should be the file name.
# 5  
Old 11-28-2011
Do you want to know what is the path to your script?

Try this:
Code:
scriptDir=`dirname $0`
appDir=`cd ${scriptDir}/ && pwd`

# ${appDir} will always point to the directory where your script is!

I hope it helps!
# 6  
Old 11-28-2011

No, it won't. Try this:
Code:
bash script

where script contains
Code:
scriptDir=`dirname $0`
appDir=`cd ${scriptDir}/ && pwd`
printf '%s\n' "$appDir"

# 7  
Old 11-28-2011
It returns the same to me:

Code:
# cat teste2.sh
scriptDir=`dirname $0`
appDir=`cd ${scriptDir}/ && pwd`
echo "${appDir}"

# bash ./scripts/teste2.sh
/<path to>/scripts
# ./scripts/teste2.sh
/<path to>/scripts

I am in a Red Hat Linux box and I also tested it in a SunOS box.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Simple sed command not working; could be a Mac/Linux vs. PC/Linux issue

Hello, I am on a Mac and trying to clean up some monthly files with a very simple SED: sed '3,10d;/<ACROSS>/,$d' input.txt > output.txt (from the input, delete lines 3 - 10; then delete from the line containing <ACROSS> to the end of the file) then output to output.txt Even when I try... (2 Replies)
Discussion started by: verbatim
2 Replies

2. Shell Programming and Scripting

Disk Space Utilization in HTML format working in one environment and not working on the other

Hi Team, I have written the shell script which returns the result of the disk space filesystems which has crossed the threshold limit in HTML Format. Below mentioned is the script which worked perfectly on QA system. df -h | awk -v host=`hostname` ' BEGIN { print "<table border="4"... (13 Replies)
Discussion started by: Harihsun
13 Replies

3. UNIX for Beginners Questions & Answers

sed command not working

Hello There - Iam trying to get this expdp running for oracle backup. And this is the code below: ### Run the export. ### Comment out any LOGFILE parameters in the .par file. if grep -i "Logfile" /<Path>$1_$2_$3.par; then ## Comment out any LOGFILE... (7 Replies)
Discussion started by: bkilaru
7 Replies

4. UNIX for Beginners Questions & Answers

sed command not working properly

This is my sample file cat bipin.txt Unix is an OS Unix has its own commmands Unix is a user friendly OS Unix is platform independent Unix is a time sharing OS the best OS to learn is Unix Abinitio uses Unix in backend When i use sed 's/Unix/Linux/' bipin.txt , only the first... (2 Replies)
Discussion started by: Bipin_1991
2 Replies

5. Shell Programming and Scripting

sed command not working

cat bipin.txt Unix is an OS Unix has its own commmands Unix is a user friendly OS Unix is platform independent Unix is a time sharing OS the best OS to learn is Unix Abinitio uses Unix in backend this is my file when i use sed 's/Unix/Linux/' bipin.txt all the occurences are getting... (0 Replies)
Discussion started by: Bipin_1991
0 Replies

6. Shell Programming and Scripting

sed command not working

Hi All, I am trying to run a sed command to replace a string in a file. sed -i -e "s/$Job_status_old ,$line/Job_status_new ,$line/g" stat.txt The command wen run from the command promt works fine. But the same command does not work when its put in a script. The script is not failing... (3 Replies)
Discussion started by: samyamkrishna
3 Replies

7. Shell Programming and Scripting

Why is this command not working? (sed)

Hi guys, the command is echo "Online Memory : 32768 MB" | sed 's/.*\(+\).*/\1/' I would expect it to print 32768, it cuts off any character to the first digit, then gets all digits in 1, cuts off the rest after the digits, and should print 32768, instead it... (4 Replies)
Discussion started by: funksen
4 Replies

8. Shell Programming and Scripting

sed find and replace command not working

Hi, Am trying to replace a character '-' with 'O' in position 289 in my file but am not success with below command. sed 's/^\(.\{289\}\)-/\1O/' filename sed: 0602-404 Function s/^\(.\{289\}\)-/\1O/ cannot be parsed. Thanks in Advance Sara Video tutorial on how to use code tags in The... (9 Replies)
Discussion started by: Sara183
9 Replies

9. Shell Programming and Scripting

sed command not quite working yet, if anyone can help appreciated

Hi gang, I am trying to create some batch commands for many html pages I need to re-format. I am trying the number 2b in this example to wrap anchor tags around the number that will be referenced in the footnotes. I am trying to use the h/H hold command, but I have never tried using it... (2 Replies)
Discussion started by: naphelge
2 Replies

10. UNIX for Dummies Questions & Answers

Sed command not working

Hi, I have a test file as follows: 1G102119 ^ AA1179291 ^ 06oct2006 09:50:35^ 73.4^ 2^ 13^ 0^ 1493 1G102119 ^ AA1179291 ^ 06oct2006 09:49:45^ 73.4^ 2^ 13^ 0^ 1493 1G102119 ^ AA1179291 ^ 06oct2006 09:48:58^ 73.4^ 2^ 17^ 0^ 2 1G102119 ... (9 Replies)
Discussion started by: shashi_kiran_v
9 Replies
Login or Register to Ask a Question