【问题】:
a.out每次只能得出一个结果,且需要根据提示,手动输入不同的参数。a.out的操作如下:
$ ./a.out
Input client IP address string:
1.1.1.105
Input server IP address string:
1.1.1.1
Input worker number:
4
worker id: 5
【需求】:
因此想到使用shell脚本自动输入参数:
#!/bin/bash
server_ip="1.1.1.105"
client_ip="1.1.1.1"
worker_num="4"
gcc ./echo.c -o a.out
# option 1
#echo -e "$client_ip\n$server_ip\n$worker_num\n" | ./a.out
# option 2
#for ((i=2;i<200;i++))
#do
# client_ip="1.1.1.$i"
# echo "client_ip=$client_ip"
# echo -e "$client_ip\n$server_ip\n$worker_num\n" | ./a.out
#done
# option 3
# sudo apt-get install tcl tk expect
for ((i=2;i<240;i++))
do
client_ip="1.1.1.$i"
expect<<__EOF__
set timeout 10
spawn -noecho ./a.out
expect "Input client IP address string:"
send "$client_ip\n"
expect "Input server IP address string:"
send "$server_ip\n"
expect "Input worker number:"
send "$worker_num\n"
expect off
__EOF__
done
注:__EOF__需要顶头编写。
【优化一】
把a.out嵌入到shell脚本tool.sh中。
$ cat ./tool.sh
#!/bin/sh
#追加在shell脚本末尾中的文件可以是可执行文件或者tar压缩包等
file_name="./a.out"
#使用sed命令把文件内容从shell脚本解析到file_name中,$0是本脚本的名字 $1是传递给本脚本的第一个参数
#sed命令参数说明
# 1,/Alex/ 匹配第一行到有Alex的行
# d 删除指定行
sed "1,/^###END OF THE SCRIPT###/d" "$0" > ${file_name}
# sed解析完之后,对该文件进行其它自定义的操作
chmod u+x ${file_name}
# shell脚本需要执行的内容
server_ip="1.1.1.105"
client_ip="1.1.1.1"
worker_num="4"
# sudo apt-get install tcl tk expect
for ((i=2;i<240;i++))
do
client_ip="1.1.1.$i"
expect<<__EOF__
set timeout 10
spawn -noecho ./a.out
expect "Input client IP address string:"
send "$client_ip\n"
expect "Input server IP address string:"
send "$server_ip\n"
expect "Input worker number:"
send "$worker_num\n"
expect off
__EOF__
done
rm -f ${file_name}
exit 0
###END OF THE SCRIPT###
^?ELF^B^A^A^@^@^@^@^@^@^@^@^@^C^@>^@^A^@^@^@°...
注:
1 ###END OF THE SCRIPT### 这一行之后的内容是使用 cat file_name >> test.sh 命令追加到脚本中的。
2 先写好脚本,再使用 cat file_name >> test.sh 脚本中,不能先把文件追加到脚本,再进行修改,file_name可以是任意的文件
3 sed工作原理:sed工作的过程是先从文件中读取一行内容到模式空间里即sed专属的缓存空间,然后判断这行内容是否是需要处理的内容,如果不是就继续从文件中读取下一行,否则对改行内容进行相应处理后输出,然后继续读取下一行进行判断或处理,直到文件最后一行处理完毕整个过程结束。
[优化二]
自动编译及完成注入shell脚本(auto_compile_and_gent_tool.sh)
使用脚本自动完成以上所有工作.
$ cat auto_compile_and_gent_tool.sh
#!/bin/bash
FILE_NAME="./tool.sh"
set -e
#0 compile
gcc ./echo.c -o a.out
#1 check file
if [ ! -f "$FILE_NAME" ]; then
touch $FILE_NAME
cat>$FILE_NAME<<\___EOF
#!/bin/bash
file_name="./a.out"
sed "1,/^###END OF THE SCRIPT###/d" "$0" > ${file_name}
chmod u+x ${file_name}
# shell script
server_ip="1.1.1.105"
client_ip="1.1.1.1"
worker_num="4"
# sudo apt-get install tcl tk expect
for ((i=2;i<240;i++))
do
client_ip="1.1.1.$i"
expect<<__EOF__
set timeout 10
spawn -noecho ./a.out
expect "Input client IP address string:"
send "$client_ip\n"
expect "Input server IP address string:"
send "$server_ip\n"
expect "Input worker number:"
send "$worker_num\n"
expect off
__EOF__
echo ""
done
rm -f ${file_name}
exit 0
###END OF THE SCRIPT###
___EOF
else
#1 find remove line num
START_LINE=`grep -ran '^\#\#\#END' ./$FILE_NAME | awk -F ':' '{print $1}'`
START_LINE=$(($START_LINE+1))
#echo "START_LINE:"$START_LINE
END_LINE=`wc -l ./$FILE_NAME | awk '{print $1}'`
#echo "END_LINE:"$END_LINE
#3 remove binary content
sed -i ''"$START_LINE"','"$END_LINE"'d' ./$FILE_NAME
fi
#4 append new binary content
cat ./a.out >> $FILE_NAME
#5 modify file exec privallage
chmod 755 $FILE_NAME
echo "====finish!===="