2015-04-14 117 views
-1

我载列如下文件和标题的列表:如何在bash中一次将变量传递给脚本?

Title file1.txt 
Title2 file2.txt 
Title3 file3.txt 

如何传递这个由行脚本线,设置列1和2作为独立变量。例如

发送标题为$ 1,file1.txt为$ 2给我的脚本。 然后将Title2作为$ 1和file2.txt作为$ 2发送给同一个脚本。

我不知道是否有一个更简单的方法来做到这一点,但任何帮助将不胜感激,谢谢。

+1

你在哪里都榜上有名?在一个文件中?在一个变量?通过管道? – choroba

+0

我已经在文件中完成了它。它似乎已经采取了新的线路,我打算把它写在不同的行: 标题FILE1.TXT 标题2 FILE2.TXT TITLE3 file3.txt 我只希望在同一时间发送到脚本一行尽管 –

回答

0

只需逐行读取文件,使用参数扩展来提取标题和文件名称。

while read -r title file ; do 
    echo Title is "$title", file is "$file". 
done < input.lst 

如果标题可以包含空格,它变得有点复杂:

while read -r line ; do 
    title=${line% *}  # Remove everything from the first space. 
    title=${title%%+()} # Remove trailing spaces. 
    file=${line##* }  # Remove everything up to the last space. 
    echo Title is "$title", file is "$file". 
done < input.lst 
+0

谢谢!那很完美。 –

0

尝试:

for i in "Title file1.txt" "Title2 file2.txt" "Title3 file3.txt"; do Title $i; done 

这实际上是喜欢做:

$ for i in "a b" "c d" "e f"; do echo $i; done 
a b 
c d 
e f 
0

你可以尝试做的是运行objetive脚本其他脚本:

#! /bin/bash 

ls /path/where/files/stay >> try.txt 

a=1 
while [ $a -lt 7 ] 
    do 
     ./script $(sed "$a"'q;d' try.txt) $(sed "$(($a+1))q;d" try.txt) 
     a=$(($a+2)) 
    done 

该脚本将运行脚本从文件中获取喜欢的变量。

相关问题