LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (http://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Simple shell script (http://www.linuxquestions.org/questions/linux-newbie-8/simple-shell-script-871856/)

vinaytp 03-30-2011 03:08 AM

Simple shell script
 
Hi All,

I have a script called autorun.sh in cdrom which inturn invokes install.sh in a CDROM.

autorun.sh script

Code:

#!/bin/bash
$(dirname $0) install.sh

when I try to manually execute this it gives error as:

Code:

: No such file or directoryll.sh
some characters are getting truncated...

Thanks

ted_chou12 03-30-2011 03:22 AM

what is this variable $(dirname $0)? if it is path to the script, it should be:
Code:

#!/bin/bash
$(dirname $0)install.sh
#if you want to have space in between:
"$(dirname $0) install.sh"

quotation is needed

colucix 03-30-2011 03:33 AM

Most likely you have some hidden control character in the script, like the windows terminator characters. They ususally make the shell to do weird things like this. Please, show us the output of
Code:

od -c autorun.sh
If you see the sequence \r \n you have to change it into \n, for example by means of the dos2unix utility. Anyway, if you want to run install.sh maybe you need a slash instead of a space:
Code:

$(dirname $0)/install.sh

vinaytp 03-30-2011 03:51 AM

Quote:

Originally Posted by ted_chou12 (Post 4308217)
what is this variable $(dirname $0)? if it is path to the script, it should be:
Code:

#!/bin/bash
$(dirname $0)install.sh
#if you want to have space in between:
"$(dirname $0) install.sh"

quotation is needed

Thanks it worked !!

colucix 03-30-2011 04:32 AM

I'm curious why it works. The following:
Code:

$(dirname $0)install.sh
lacks the slash between the directory name and the name of the script, since the dirname command doesn't put a trailing slash. The second option:
Code:

"$(dirname $0) install.sh"
won't work, since a directory name alone at the beginning of a command line would throw an error, unless the directory name is . (dot). In this case the dot is interpreted as the source built-in and the install.sh script is sourced accordingly (not executed). Anyway it strongly depends on how the autorun.sh script is invoked: if you give the full path, the line above won't work. Just my :twocents:.


All times are GMT -5. The time now is 12:30 AM.