Bash 脚本抱怨`sed:option需要一个参数--e`和`No这样的文件或目录`
在我的脚本中,我获取一个文本文件,逐行遍历该文件,并将字符串“test”替换为“true”,然后将其重定向到一个新文件。这是我的密码:Bash 脚本抱怨`sed:option需要一个参数--e`和`No这样的文件或目录`,bash,shell,Bash,Shell,在我的脚本中,我获取一个文本文件,逐行遍历该文件,并将字符串“test”替换为“true”,然后将其重定向到一个新文件。这是我的密码: cat $FILENAME | while read LINE do echo "$LINE" | sed -e `s/test/true/g` > $NEWFILE done 但是,当我执行脚本时,会出现以下错误: /home/deploy/KScript/scripts/Stack.sh: line 46: s/test/true/g: No su
cat $FILENAME | while read LINE
do
echo "$LINE" | sed -e `s/test/true/g` > $NEWFILE
done
但是,当我执行脚本时,会出现以下错误:
/home/deploy/KScript/scripts/Stack.sh: line 46: s/test/true/g: No such file or directory
sed: option requires an argument -- e
Usage: sed [OPTION]... {script-only-if-no-other-script} [input-file]...
-n, --quiet, --silent
suppress automatic printing of pattern space
-e script, --expression=script
add the script to the commands to be executed
-f script-file, --file=script-file
add the contents of script-file to the commands to be executed
-i[SUFFIX], --in-place[=SUFFIX]
edit files in place (makes backup if extension supplied)
-c, --copy
use copy instead of rename when shuffling files in -i mode
(avoids change of input file ownership)
-l N, --line-length=N
specify the desired line-wrap length for the `l' command
--posix
disable all GNU extensions.
-r, --regexp-extended
use extended regular expressions in the script.
-s, --separate
consider files as separate rather than as a single continuous
long stream.
-u, --unbuffered
load minimal amounts of data from the input files and flush
the output buffers more often
--help display this help and exit
--version output version information and exit
你能帮我找出我做错了什么吗?当使用sed时,你应该用单引号或双引号引用替换参数 例如,这应该起作用:
echo "$LINE" | sed -e "s/test/true/g" > $NEWFILE
使用sed时,应使用单引号或双引号引用替换参数 例如,这应该起作用:
echo "$LINE" | sed -e "s/test/true/g" > $NEWFILE
对于此类错误,将
set-x
放在echo“$line”| sed-e's/test/true/g'>$NEWFILE
set +x
echo "$LINE" | sed -e `s/test/true/g` > $NEWFILE
Bash将在执行命令行之前打印命令行,并引用参数。这会让你知道为什么它会失败
确保使用正确的引号字符。`(倒勾)和(单引号)是不同的东西。第一个将尝试执行命令
s/test/true/g
,并将此命令的结果传递给sed
对于类似的错误,将set-x
放在echo“$line”| sed-e's/test/true/g'>$NEWFILE
之前的行中
set +x
echo "$LINE" | sed -e `s/test/true/g` > $NEWFILE
Bash将在执行命令行之前打印命令行,并引用参数。这会让你知道为什么它会失败
确保使用正确的引号字符。`(倒勾)和(单引号)是不同的东西。第一个将尝试执行命令
s/test/true/g
,并将此命令的结果传递给sed
sed可以通过读取文件并对每一行应用脚本来工作,这就是您在脚本中所做的。所以你只想:
sed -e 's/test/true/g' "$FILENAME" > "$NEWFILE"
备注:
-e
参数在这里是可选的,因为您只有一个脚本。sed可以通过读取文件并将脚本应用到每一行来工作,这就是您在脚本中执行的操作。所以你只想:
sed -e 's/test/true/g' "$FILENAME" > "$NEWFILE"
备注:
-e
参数在这里是可选的,因为您只有一个脚本。如果$FILENAME
只包含一个文件名,您可以摆脱循环,只需执行以下操作:sed-e's/test/true/g'$FILENAME>$NEWFILE
。整个脚本在我看来是错误的-sed在输入文件上逐行运行,所以您只需要sed's/test/true/g'$FILENAME>$NEWFILE
。您当前的脚本将导致$NEWFILE只有一行,确切地说是最后一行。伙计们,谢谢,但我发现了错误,语法是正确的,并且我正在通过管道输入thorugh Echo也工作得很好@aragaer唯一的错误是我使用了“`”而不是“`”:)如果$FILENAME
只包含一个文件名,您可以摆脱循环,只需执行以下操作:sed-e's/test/true/g'$FILENAME>$NEWFILE
。整个脚本对我来说似乎是错误的-sed自己逐行操作输入文件,所以您只需要sed's/test/true/g'$FILENAME>$NEWFILE
。您当前的脚本将导致$NEWFILE只有一行,确切地说是最后一行。伙计们,谢谢,但我发现了错误,语法是正确的,并且我正在通过管道输入thorugh Echo也工作得很好@阿拉格尔唯一的错误是我用了“`”而不是“`”: