简体   繁体   English

目录和字符串我bash脚本

[英]Directory and string i bash script

I`m new to bash scripting and need some help with a strange problem. 我是bash脚本的新手,需要一些奇怪问题的帮助。

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. 在最后一个echo $ out shoul中,路径和文件名组合在一起..但是它被打破了。 what could be wrong? 什么可能是错的?

Thanks for any anwer:) 谢谢你的任何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. 它看起来像$ 1或脚本的某些行以回车结束(有时写成\\ r) - 这个字符通常是不可见的,但可能导致奇怪的行为和输出。 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: 例如,如果我们从TARGET_DIR =“/ volume1 / video / Transcoded /”和fbname = $'Thor(2011).mkv \\ r'开始(注意:$'...'是带有转义序列的字符串的bash表示法喜欢\\ r \\ n解释),那么你最终将退出= $'Thor(2011).mkv \\ r \\ n \\ r \\ n/volume1 / video / Transcoded /',当你echo $out ,它会打印:

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). 杂散回车通常是使用DOS / Windows文本编辑器的结果 - 不要将它们用于unix文本文件(包括脚本)。 To remove them, see the previous questions here and here . 要删除它们,请在此处此处查看以前的问题。

BTW, I second @shellter's confusion about why the filename is before the path... 顺便说一句,我第二次@ shellter对于为什么文件名在路径之前的混淆...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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