sed command working different in linux environment.

Hi
I tried running the 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 .

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

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

'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.

If you want a script to be portable, you must use standard commands; whence is not standard.

Where can i find the $PATH variable in linux. Is it specific to some file. If yes, what should be the file name.

Do you want to know what is the path to your script?

Try this:

scriptDir=`dirname $0`
appDir=`cd ${scriptDir}/ && pwd`

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

I hope it helps!

No, it won't. Try this:

bash script

where script contains

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

It returns the same to me:

# 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.

Try:

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

Is teste2.sh in your current directory? If so, change to another directory.

Then you should get what I get:

/home/chris/tmp $ type -p xx.sh
/home/chris/scripts/xx.sh
/home/chris/tmp $ cat ~/scripts/xx.sh

scriptDir=`dirname $0`
appDir=`cd ${scriptDir}/ && pwd`
echo $appDir

/home/chris/tmp $ bash xx.sh
/home/chris/tmp
/home/chris/tmp $