Linux Bash错误:找不到此命令
我刚刚开始学习bash,我正在尝试编写一个脚本,该脚本接受两个单词作为参数,并打印出给定文件中第一个单词所在行和第二个单词所在行之间的部分,例如,如果我的Linux Bash错误:找不到此命令,linux,bash,Linux,Bash,我刚刚开始学习bash,我正在尝试编写一个脚本,该脚本接受两个单词作为参数,并打印出给定文件中第一个单词所在行和第二个单词所在行之间的部分,例如,如果我的test.txt文件如下所示: This is a line of text This is another line of text this is the third one Another one right here #!/bin/bash filename=$1 a=$(grep -n -m1 "$2" "$filename" |
test.txt
文件如下所示:
This is a line of text
This is another line of text
this is the third one
Another one right here
#!/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 test.txt运行这个程序,它应该返回第1、2和3行,因为我们在第一行找到单词This,在第3行找到单词one。我当前的脚本如下所示:
This is a line of text
This is another line of text
this is the third one
Another one right here
#!/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行:未找到This:命令
由于该行上唯一的命令是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
第6行应该是
thing=$(head -n"${b}" "$filename" | tail -n"${lines}") #Print out b lines from beginning and then print $(lines) lines from bottom of that
=