简体   繁体   中英

Bash run script from script using variable

I am trying to run bash script from bash script using variable.

The variable is assigned before from parsed CSV, and when I echo it the path to another script looks ok. When I try to run the script path from the variable it fails.

Assiging the variable (2nd column from csv):

THE_VARIABLE=`echo ${CHOICE[${WHICH}]} | awk -F";" '{print $2}'`

echo $THE_VARIABLE (ok):

/fake_path/scripts/test.sh

Bash debug:

    + echo $'/fake_path/scripts/test.sh\r'

However after that when trying to run:

bash $THE_VARIABLE
: No such file or directoryripts/test.sh

It seems like taking only part of the string in variable(?).

Thanks for help!

The string ends with ASCII character 13 (\\x0D or \\015 or \\r) and not the sequence '\\' 'r' , with echo the trailing \\r can't be seen because echo adds a \\n , but appending a # character for example will show at begining of line.

echo "${THE_VARIABLE}#"
#fake_path/scripts/test.sh

to remove the trailing \\r :

THE_VARIABLE=${THE_VARIABLE%$'\r'}

otherwise it means that input file has dos line ending \\r\\n , it could be modified with dos2unix .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM