Interactive shell script
1. Reasons
I wrote a Windows cmd script before to save the report file:
@echo off
cd
cd ./curl/bin
set /p exampleUrlHost=请输入服务的IP地址:
set /p exampleUrlPort=请输入服务的端口:
:start
echo **********************报告保存脚本**********************
echo 根据以下提示执行
echo 0 退出
echo 1 保存年度报告
echo 2 保存月度报告
echo 3 保存当天报告
set /p exampleUrlIndex=请输入要保存的结果序号:
if %exampleUrlIndex% == 1 (
set exampleUrlPath=/example/exampleYear
)
if %exampleUrlIndex% == 2 (
set exampleUrlPath=/example/exampleMonth
)
if %exampleUrlIndex% == 3 (
set exampleUrlPath=/example/exampleDay
)
if %exampleUrlIndex% == 0 (
exit
)
echo 正在保存...
curl http://%exampleUrlHost%:%exampleUrlPort%%exampleUrlPath%
echo 保存完成。
goto start
But sometimes the service is only deployed on the Linux environment, so a shell script needs to be written to save the report.
2.Shell script
2.1 Initial version
For simple parameter judgment, only one branch is given here. The content of the script save.sh is as follows:
#!/bin/bash
if [ $1 == 1 ]; then
echo 正在保存年度报告...
curl http://exampleUrlHost:exampleUrlPort/example/exampleYear
echo 保存完成。
else
echo "脚本请带参数"
echo "1 保存年度报告"
fi
At this time, if no parameters are included when executing the script, an error will be reported:
./save.sh: line 2: [: ==: unary operator expected
Many of the issues with this script exampleUrlHost和exampleUrlPort
are fixed, and sometimes they need to be modified before they can be executed correctly.
2.2 Interactive case script
#!/bin/bash
echo "请输入服务的IP地址:"
read exampleUrlHost
echo "请输入服务的端口号:"
read exampleUrlPort
echo "请选择您的操作:"
echo "1 保存年度报告"
echo "2 保存月度报告"
echo "3 保存当天报告"
echo "4 退出"
read choice
case $choice in
1)
echo 正在保存年度报告...
curl http://$exampleUrlHost:$exampleUrlPort/example/exampleYear
echo 保存完成。
;;
2)
echo 正在保存月度报告...
curl http://$exampleUrlHost:$exampleUrlPort/example/exampleMonth
echo 保存完成。
;;
3)
echo 正在保存当天报告...
curl http://$exampleUrlHost:$exampleUrlPort/example/exampleDay
echo 保存完成。
;;
4)
exit 0
;;
*)
echo "无效的选项!"
;;
esac
This version not only does not report errors, but the experience seems to be OK. However, if you want to export multiple reports, you have to enter the IP and port number multiple times, which is quite troublesome.
2.3 Final while case version
#!/bin/bash
echo "请输入服务的IP地址:"
read exampleUrlHost
echo "请输入服务的端口号:"
read exampleUrlPort
while true; do
echo "请选择您的操作:"
echo "1 保存年度报告"
echo "2 保存月度报告"
echo "3 保存当天报告"
echo "4 退出"
read choice
case $choice in
1)
echo 正在保存年度报告...
curl http://$exampleUrlHost:$exampleUrlPort/example/exampleYear
echo 保存完成。
;;
2)
echo 正在保存月度报告...
curl http://$exampleUrlHost:$exampleUrlPort/example/exampleMonth
echo 保存完成。
;;
3)
echo 正在保存当天报告...
curl http://$exampleUrlHost:$exampleUrlPort/example/exampleDay
echo 保存完成。
;;
4)
break
;;
*)
echo "无效的选项!"
;;
esac
done
3. Summary
A simple version can also implement the function, but the script must be adjusted before execution.