2008-12-06 87 views
1

我已將一些有價值的信息重定向到文本文件中。如何在循環中執行該文本文件的每一行?使用循環執行文本文件

我在想什麼是把我的文本文件加空格,然後用循環來單獨執行每一行。我希望,當我加倍空間的文本文件的每一串命令將有自己的路線。

例如,該文本文件:

貓/ etc/passwd文件|頭-101 | tail -3 nl/etc/passwd |頭-15 | cut -d':'-f1 cat/etc/passwd | cut -d':'-f1,5 | tee users.txt nl/etc/passwd |尾-1 | cut -f1 ls〜/ home | nl |尾-1 | cut -f1 ls -lR/2>/dev/null | sort -n -r +4 |頭-1

應該看起來像這樣當我雙倍空間它:

cat/etc/passwd |頭-101 | tail -3
nl/etc/passwd |頭-15 | cut -d':'-f1
cat/etc/passwd | cut -d':'-f1,5 | tee users.txt
nl/etc/passwd |尾-1 | cut -f1
ls〜/ home | nl |尾-1 | cut -f1 ls -lR/2>/dev/null | sort -n -r +4 |頭-1

然後我會用循環來執行每一行。

這裏是我的腳本:

FILE="$1" 

echo "You Entered $FILE" 

if [ -f $FILE ]; then 

tmp=$(cat $FILE | sed '/./!d' | sed -n '/regex/,/regex/{/regex/d;p}'| sed -n '/---/,+2!p' | sed -n '/#/!p' | sed 's/^[ ]*//' | sed -e\ 
s/[^:]*:// | sed -n '/==> /!p' | sed -n '/--> /!p' | sed -n '/"lab3.cmd"/,+1!p'\ 
| sed -n '/======/!p' | sed -n '/regex/!p' | sed -n '/commands are fun/\ 
!p' | sed -n '/regex/!p') 

fi 

MyVar=$(echo $tmp > hi.txt) 
echo "$MyVar"

這是要去了解它的正確方法?

回答

2

爲什麼不直接用行分隔符來分隔命令?除非你的命令必須能夠包含行分隔符,否則你不需要空行來分隔它們 - 然後如果它需要在一行中包含2呢?

0

恐怕我不明白你想解決的問題。如果你寫命令到一個文件名爲commands.txt中,您可以通過

sh commands.txt 

執行該文件的每一行只是執行它們。如果你想這樣做在一個循環,我想你可以嘗試像

for i in *; do FILE="$i" sh commands.txt; done 

也許你可以編輯你的問題,使其多一點具體的?