我使用以下命令打开、替换、查看更改并保存文件:
sed 's/old string/new string/g' filename > filename1; diff filename1 filename; mv filename1 filename
是否可以在执行mv
命令之前请求确认,如下所示?
sed 's/old string/new string/g' filename > filename1
diff filename1 filename
<Some-Command here for user to provide Yes or NO confirmation>
mv filename1 filename
目的是验证更改,然后再保存。
我在bash:上使用这个简单的一行
CCD_ 2。
这样,我就不依赖于另一个实用程序,它只会在输入为单个y
时运行。
是的,您可以通过read
将用户输入读取到变量中,然后与一些可接受的值进行比较,如"yes"
。您还可以将命令存储在变量中,并将其打印给用户,然后再执行。
#!/bin/bash
COMMAND='mv filename1 filename'
echo "Perform the following command:"
echo
echo " $ $COMMAND"
echo
echo -n "Answer 'yes' or 'no': "
read REPLY
if [[ $REPLY == "yes" ]]; then
$COMMAND
else
echo Aborted
exit 0
fi
我的脚本是
#!/bin/bash
sed 's/This/It/g' test.xml > "test.xml1"
diff test.xml test.xml1
COMMAND ='mv test.xml1 test.xml'
echo "Perform the following command:"
echo
echo " $ $COMMAND"
echo
echo -n "Answer 'yes' or 'no':"
read REPLY
if[[$REPLY=="yes"]]; then
$COMMAND
else
echo Aborted
exit 0
fi
执行时的错误是,
2c2
< This is a Test file
---
> It is a Test file
./test.sh[9]: COMMAND: not found
Perform the following command:
$
-n Answer 'yes' or 'no':
yes
./test.sh[19]: syntax error at line 20 : `then' unexpected
mv
命令不起作用。