将2个参数发送到脚本 [英] Send 2 parameters to a script

查看:70
本文介绍了将2个参数发送到脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚开始使用bash,所以希望答案不明显.我有一个名为playlists.txt的文件,如下所示:

I just started using bash so I hope the answer is not obvious. I have a file called playlists.txt that looks like this:

  • ChilledDubstep PLYNrAD9Jn2WDwRrS_Uw6UR55K-c_s6EH4
  • 经典PLYNrAD9Jn2WDWZFpIG3a2tkOBtdeNAbc6
  • ChilledOut PLYNrAD9Jn2WCfrUCoS22kn3pBX1XUFinE
  • SlickSlickSound PLYNrAD9Jn2WDmpu3gNVxIVO8bAiOcQkx7
  • 专辑,音乐会PLYNrAD9Jn2WD3BI8o5VjmSbhZgfYh5zaX

我想将每行的第一个字符串分配给$ name,将第二个字符串分配给$ hash,然后将其发送到script.sh并针对每一行递归执行此操作,因为知道将来行数可能会增加

I want to assign the first string of each line to $name and the second string to $hash, then send it to script.sh and do this recursively for every line, knowing that the number of lines might increase in the future.

我写了这个,但是不起作用:使用此-> 如何将一个字符串拆分为bash shell中有多个变量?

I wrote this but it does not work: using this ->How to split one string into multiple variables in bash shell?

while read name hash
do

        sh script.sh "$name" "$hash"

done < playlists.txt

我应该怎么做?预先谢谢你.

Whats should I be doing instead? Thank you in advance.

谢谢大家,所以我将其更改为此,它更易于阅读.我以为,这将解决我的错误,但仍然存在……该死.因此,基本上,在script.sh中,这是第一部分:

Thanks guys, so I changed it to this, it is easier to read. I thought, it was going to resolve my errors, but there are still there...damn it. So basically, in script.sh, here is the first part:

cd ~/Music/Youtube\ Playlist/Playlists/$name

mv $HOME/Music/Youtube\ Playlist/Playlists/$name/TextRecords/lastoutput.txt $HOME/Music/Youtube\ Playlist/Playlists/lastoutput.txt

但是,shell返回错误:

However, the shell returns the error:

mv: cannot stat `/home/kabaka/Music/Youtube Playlist/Playlists//TextRecords/lastoutput.txt': No such file or directory

这意味着播放列表的名称应该没有任何内容.你知道为什么吗是因为当前脚本中的$ name与上面脚本中的$ name不同吗?请注意,$ hash发生了同样的事情,它应该出现在url中但只是空白

That means that where there should the name of a playlist, there is nothing. Do you have any idea why? Is it because $name in my current script and $name in the script above are not the same? Notice that I have the same thing happening for $hash, which should appear in an url but is just blank

推荐答案

  1. 您可以使用管道将echo命令的输出用作输入文件
  2. 标志'-d'为'cut'命令定义了定界符.

代码如下:

while read line
do
    name=`echo $line|cut -f1 -d' '`
    hash=`echo $line|cut -f2 -d' '`
    sh script.sh $name $hash

done < playlists.txt

第二个问题:在将文件移动到目录之前,请确保该文件存在.您可以使用'mkdir -p'创建它,如下所示:

To your 2nd question: Before moving a file to a directory make sure it exists. You can create it with 'mkdir -p' as follows:

mkdir -p $HOME/Music/Youtube\ Playlist/Playlists/lastoutput.txt

这篇关于将2个参数发送到脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆