-1
我剛開始學習bash,並試圖編寫一個接受兩個單詞作爲參數的腳本,並打印出找到第一個單詞的行和第第二個字被發現,例如,如果我的test.txt
文件看起來像:Bash error這個:命令未找到
This is a line of text
This is another line of text
this is the third one
Another one right here
而且我運行./prog.bash test.txt This one
程序應該返回線1,2和3,因爲我們發現,在一行單詞本,並在一個字第3行。我當前的腳本如下所示:
#!/bin/bash
filename=$1
a=$(grep -n -m1 "$2" "$filename" | cut -f 1 -d':') #find first occurence of word1 and use that to cut the line number of word
b=$(grep -n -m1 "$3" "$filename" | cut -f 1 -d':') #find first occurence of word2 and use that to cut the line number of word
lines=$(($b-$a)+1)
thing= $(head -n"${b}" "$filename" | tail -n"${lines}") #Print out b lines from beginning and then print $(lines) lines from bottom of that
echo $a
echo $b
echo $lines
echo $thing
exit
現在我得到一個錯誤說
./prog.bash:6號線:此:找不到命令
由於該行是head
和tail
我看着在我的路徑是唯一的命令是和他們在那裏位於,我想出了:
$PATH =
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games
head = /usr/bin/head
tail = /usr/bin/tail