删除 bash 脚本中的回车符,或在使用 mv / mkdir 时忽略它
我试图从来自 grep 的变量创建一个目录,但它一直失败,告诉我有一个回车符,而没有回车符。我试过 dos2unix,它告诉我这是一个无效文件。我在 ubuntu 10.10 上使用 gedit 和 unix 行结尾创建了脚本。
我也尝试过将 cat 文件的输出通过管道传输到
tr -d '\r'
该文件,但这不起作用。
这是脚本中失败的部分:
ARTIST=`cat read | grep DTITLE | cut -c8- | cut -d"/" -f 1`
ALBUM=`cat read | grep DTITLE | cut -c8- | cut -d"/" -f 2 | awk '{gsub(/^[ \t]+|[\t]+$/,"")};1'`
mkdir $DEST/"$ARTIST"
cd $DEST/"$ARTIST"
mkdir "$ALBUM"
mv $DEST/temp/*.flac $DEST/"$ARTIST"/"$ALBUM"/
以及我收到的错误:
mv: target `Hard\r' is not a directory
这是它从中提取的“读取”文件:
macmini:~/Dropbox/bin$ cat ~/Desktop/temp/read
210 folk 0d021f02 CD database entry follows (until terminating `.')
# xmcd CD database file
#
# Track frame offsets:
# 150
# 13002
#
# Disc length: 545 seconds
#
# Revision: 0
# Processed by: cddbd v1.5.2PL0 Copyright (c) Steve Scherf et al.
# Submitted via: CDex 1.50Beta7
#
DISCID=0d021f02
DTITLE=Cursive / Art is Hard
DYEAR=2003
DGENRE=Indie
TTITLE0=Art is Hard
TTITLE1=Sinner's Serenade
EXTD= YEAR: 2003
EXTT0=
EXTT1=
PLAYORDER=
.
此时我有点迷失。回车符从哪里来?
I am trying to create a directory from a variable that comes from grep'ing and it keeps failing, telling me there is a carriage return where there isn't. I have tried dos2unix, it tells me it is an invalid file. I created the script on ubuntu 10.10 with gedit and unix line endings.
I have also tried piping the output of the cat'd file to
tr -d '\r'
that doesn't do it.
here is the part of the script which its failing on:
ARTIST=`cat read | grep DTITLE | cut -c8- | cut -d"/" -f 1`
ALBUM=`cat read | grep DTITLE | cut -c8- | cut -d"/" -f 2 | awk '{gsub(/^[ \t]+|[\t]+$/,"")};1'`
mkdir $DEST/"$ARTIST"
cd $DEST/"$ARTIST"
mkdir "$ALBUM"
mv $DEST/temp/*.flac $DEST/"$ARTIST"/"$ALBUM"/
and the error i receive:
mv: target `Hard\r' is not a directory
here is the file "read" that it is pulling from:
macmini:~/Dropbox/bin$ cat ~/Desktop/temp/read
210 folk 0d021f02 CD database entry follows (until terminating `.')
# xmcd CD database file
#
# Track frame offsets:
# 150
# 13002
#
# Disc length: 545 seconds
#
# Revision: 0
# Processed by: cddbd v1.5.2PL0 Copyright (c) Steve Scherf et al.
# Submitted via: CDex 1.50Beta7
#
DISCID=0d021f02
DTITLE=Cursive / Art is Hard
DYEAR=2003
DGENRE=Indie
TTITLE0=Art is Hard
TTITLE1=Sinner's Serenade
EXTD= YEAR: 2003
EXTT0=
EXTT1=
PLAYORDER=
.
i am a little lost at this point. where is the carriage return coming from?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为它来自读取的文件。在该文件上运行
dos2unix
。但是,此错误消息突出了另一个问题:
mv
误解了您提供的内容,因为它认为的目标太短。我猜您没有正确引用包含空格的文件路径。尝试用双引号将整个参数括起来,而不仅仅是其中的组成部分。I think it's coming from the read file. Run
dos2unix
on that file.However, this error message highlights another problem:
mv
is misinterpreting what you're giving it, because what it thinks is the target is too short. I'm guessing you aren't quoting file paths containing spaces correctly. Try surrounding the whole argument in double quotes, not merely components of it.您可以使用一个 awk 脚本来完成此操作。
要运行它,请另存为 shell 脚本(例如 myscript.sh)并在命令行上键入
./myscript.sh
或者您可以使用一种编程语言,它需要处理繁琐的 shell 引用问题
例如 Ruby(1.9+) 脚本
要运行它,请另存为(例如 myscript.rb )并在命令行或 shell 脚本中输入
ruby myscript.rb
。You can do this with one awk script
To run it, save as a shell script (eg myscript.sh) and type
./myscript.sh
on the command lineOr you can use a programming language that takes care of cumbersome shell quoting issues
eg Ruby(1.9+) script
To run it, save as (Eg myscript.rb ) and type
ruby myscript.rb
on the command line or inside a shell script.