简体   繁体   中英

Directory and string i bash script

I`m new to bash scripting and need some help with a strange problem.

Here is my lines of code:

#!/bin/ash -x  
echo  Variabel \$1='\t'$1  
TARGET_DIR=/volume1/video/Transcoded/  
echo "Variabel\$TARGET_DIR=$TARGET_DIR"  
fbname=$(basename "$1")  
echo  Variabel \$fbname=$fbname  
out="${fbname}""${TARGET_DIR}"  
echo  $out  
read -p "Press [Enter] key to start next Transcode..."  

This outputs:

Variabel $1=\t/volume1/video/Movies/Thor (2011)/Thor (2011).mkv  
Variabel$TARGET_DIR=/volume1/video/Transcoded/  
Variabel $fbname=Thor (2011).mkv  
/volume1/video/Transcoded/  
Press [Enter] key to start next Transcode... 

in the the last echo $out shoulde be path and file name combined.. but it is broken. what could be wrong?

Thanks for any anwer:)

try this:

out="${fbname}${TARGET_DIR}"  
echo  $out  

It looks to me like either $1 or some of the lines of the script end with a carriage return (sometimes written \\r) -- this character is generally invisible, but can cause weird behavior and output. For instance, if we start with TARGET_DIR="/volume1/video/Transcoded/" and fbname=$'Thor (2011).mkv\\r' (note: $'...' is bash notation for a string with escape sequences like \\r interpreted), then you'll wind up with out=$'Thor (2011).mkv\\r/volume1/video/Transcoded/', and when you echo $out , it prints:

Thor (2011).mkv
/volume1/video/Transcoded/

... with the second "line" printed on top of the first, so you never actually see the first part.

Stray carriage returns are usually a result of using DOS/Windows text editors -- don't use them for unix text files (incl. scripts). To remove them, see the previous questions here and here .

BTW, I second @shellter's confusion about why the filename is before the path...

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