删除 bash 脚本中的回车符,或在使用 mv / mkdir 时忽略它

发布于 2024-11-01 23:19:54 字数 1150 浏览 6 评论 0原文

我试图从来自 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

不交电费瞎发啥光 2024-11-08 23:19:54

我认为它来自读取的文件。在该文件上运行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.

﹏半生如梦愿梦如真 2024-11-08 23:19:54

您可以使用一个 awk 脚本来完成此操作。

#!/bin/bash   

awk -F"[/=]" 'BEGIN{
    qq="\047"
    DEST="/tmp"
}/DTITLE/{
    ARTIST=$2
    ALBUM=$NF
    gsub(/^[ \t]+|[ \t]+$/,"",ALBUM)
    gsub(/^[ \t]+|[ \t]+$/,"",ARTIST)
    cmd="mkdir "qq DEST"/"ARTIST qq
    print cmd
    #system(cmd)
    cmd="mkdir -p " qq DEST"/"ARTIST  "/" ALBUM qq
    print cmd
    #system(cmd)
    cmd="mv "DEST"/temp/*.flac " qq DEST"/"ARTIST  "/" ALBUM qq
    print cmd
    #system(cmd)
} ' file

要运行它,请另存为 shell 脚本(例如 myscript.sh)并在命令行上键入 ./myscript.sh

或者您可以使用一种编程语言,它需要处理繁琐的 shell 引用问题
例如 Ruby(1.9+) 脚本

#!/usr/bin/env ruby  
require 'fileutils'
DEST="/tmp"
File.open("file") do |f|
    while not f.eof?
        line = f.gets.chomp
        if line[/DTITLE/]
            line = line.split(/[\/=]/)
            ARTIST=line[1].strip
            ALBUM=line[2].strip
            Dir.mkdir( File.join(DEST, ARTIST) )
            Dir.mkdir( File.join(DEST, ARTIST, ALBUM) )
            Dir.glob( File.join(DEST, "temp", "*.flac") ).each do |file|
                FileUtils.mv ( file, File.join(DEST, ARTIST, ALBUM) )
            end
        end
    end
end

要运行它,请另存为(例如 myscript.rb )并在命令行或 shell 脚本中输入 ruby myscript.rb

You can do this with one awk script

#!/bin/bash   

awk -F"[/=]" 'BEGIN{
    qq="\047"
    DEST="/tmp"
}/DTITLE/{
    ARTIST=$2
    ALBUM=$NF
    gsub(/^[ \t]+|[ \t]+$/,"",ALBUM)
    gsub(/^[ \t]+|[ \t]+$/,"",ARTIST)
    cmd="mkdir "qq DEST"/"ARTIST qq
    print cmd
    #system(cmd)
    cmd="mkdir -p " qq DEST"/"ARTIST  "/" ALBUM qq
    print cmd
    #system(cmd)
    cmd="mv "DEST"/temp/*.flac " qq DEST"/"ARTIST  "/" ALBUM qq
    print cmd
    #system(cmd)
} ' file

To run it, save as a shell script (eg myscript.sh) and type ./myscript.sh on the command line

Or you can use a programming language that takes care of cumbersome shell quoting issues
eg Ruby(1.9+) script

#!/usr/bin/env ruby  
require 'fileutils'
DEST="/tmp"
File.open("file") do |f|
    while not f.eof?
        line = f.gets.chomp
        if line[/DTITLE/]
            line = line.split(/[\/=]/)
            ARTIST=line[1].strip
            ALBUM=line[2].strip
            Dir.mkdir( File.join(DEST, ARTIST) )
            Dir.mkdir( File.join(DEST, ARTIST, ALBUM) )
            Dir.glob( File.join(DEST, "temp", "*.flac") ).each do |file|
                FileUtils.mv ( file, File.join(DEST, ARTIST, ALBUM) )
            end
        end
    end
end

To run it, save as (Eg myscript.rb ) and type ruby myscript.rb on the command line or inside a shell script.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文