Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

script full name and path $0 not visible when called

Tags:

bash

shell

argv

I have a script "task.sh" with the following content:

#!/bin/bash

CUR_DIR=`pwd`
SCRIPTPATH="${CUR_DIR}/`dirname $0`"

when I call it with "bash task.sh" it works as expected but when it is called with ". task.sh"

$ . log/task.sh 
dirname: invalid option -- b
Try `dirname --help' for more information.

When the script is being scheduled in crontab it is not working as well. Can someone tell me what am I doing wrong or a different way in order to get the directory of a script that is not the current directory ?

like image 361
Hugo Avatar asked Apr 18 '11 20:04

Hugo


2 Answers

There is no reason to call external binaries such as pwd and dirname when using bash. The functionality of these two binaries can be replicated with pure shell syntax.

Try the following:

#!/bin/bash

CUR_DIR="$PWD"
SCRIPTPATH="${CUR_DIR}/${0#*/}"
like image 26
SiegeX Avatar answered Oct 03 '22 11:10

SiegeX


When you invoke it as bash task.sh, bash assigns "task.sh" to $0 (from the bash manual: "If Bash is invoked with a file of commands [...] $0 is set to the name of that file.").

When you source the file, bash does not alter $0, it just executes the script in the current environment. What's in $0 in your current enviroment?

$ echo "$0"
-bash

The leading dash will be interpreted by dirname as an option.

If it's in a cron job, why are you sourcing it?

If you need to source your script, this will work if your shell is bash:

SCRIPTPATH="${CUR_DIR}/${BASH_ARGV[0]}"

However, cron's shell is, I believe, /bin/sh. Even if /bin/sh is a symlink to bash, when bash is invoked as sh it will try to behave POSIXly: the BASH_ARGV array probably won't be available to you.

like image 77
glenn jackman Avatar answered Oct 03 '22 13:10

glenn jackman