我正在使用以下命令打开,替换,查看更改并保存文件,
sed' s / old string / new string / g'文件名> filename1; diff filename1 filename; mv filename1 filename
是否可以在执行mv命令之前请求确认,如下所示
sed' s / old string / new string / g'文件名>文件名1 diff filename1 filename
来自用户的是或否输入命令
mv filename1 filename
共鸣是验证变化,然后保存......
请帮忙......
答案 0 :(得分:1)
是的,您可以通过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
答案 1 :(得分:0)
我的剧本是,
#!/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
命令无效。
答案 2 :(得分:0)
我在bash上使用了这种简单的衬纸:
echo -n 'Confirm: ' && read 'x' && [ $x == 'y' ] && <command>
。
这样,我不依赖其他实用程序,它仅在输入为单个y
时运行。