gpt4 book ai didi

linux - Bash错误这个: command not found

转载 作者:太空宇宙 更新时间:2023-11-04 05:25:29 24 4
gpt4 key购买 nike

我刚刚开始学习 bash,并尝试编写一个脚本,该脚本接受两个单词作为参数,并打印出给定文件中第一个单词所在行和第二个单词所在行之间的部分,例如,如果我的 test.txt 文件如下所示:

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 行,因为我们在第一行找到单词 This,在第 3 行找到单词 one。我当前的脚本如下所示:

#!/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: line 6: This: command not found

由于该行上唯一的命令是 headtail 我查看了我的路径在哪里以及它们所在的位置,然后我想出了:

$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

=后没有空格

关于linux - Bash错误这个: command not found,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32751143/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com