我已经写了一个bash脚本,它包括一个脚本检查文件大小的部分,如果超过2gb,那么它将启动一个屏幕会话并开始导入db转储文件。但是,当脚本执行到特定的部分时,它就不起作用了。意味着,在导入db dump之前,脚本正在完成执行。我猜这是因为screen会话没有等待db dump import命令执行完成。脚本看起来不会那么专业,因为我是shell脚本的初学者,我也不太擅长编码,请不要介意。
注意:我已经创建了一个套接字号为24602的屏幕会话,我正在尝试在该会话中执行db dump import命令
脚本
# !/bin/bash
cd /case_data
echo "Conditions:
1.DB dump file should be there in /case_data/<case number>/ location
2.DB dump file should end with extension .sql"
read -p "Please enter a case number for which you need to update the DB file:" casenum
if [[ -z "$casenum" ]] ; then
echo 'Exiting automation as there is no user INPUT. Hasta La Vista!!'
return 0
elif [[ ${#casenum} -ne 5 ]]; then
echo "Error: Case number should contain 5 characters. Please enter the case number again!!"
return 0
elif [[ $casenum == *['!'@#\$%^\&*()_+]* ]]; then
echo "Error: Special characters are not allowed. Please enter the case number again!!"
return 0
elif [ -e "$casenum" ]; then
echo "Modifying the DB dump file for the case number $casenum"
cd /case_data/$casenum
filesize=`du -h --block-size=G *.sql | head -c 1`
`sed -i '1s/^/SET autocommit=0;\n/' *.sql`
`echo "COMMIT;" >> *.sql`
mv *.sql c$casenum.sql
echo "DB dump file modification completed!!"
echo "Initiating customer DB dump import to the DB c$casenum"
if [ $filesize -ge 2 ]; then
echo "Customer DB greater than 2 GB, started a screen session"
echo "Customer DB import started. Please wait till the import is completed"
screen -S 24602 -X 'mysql -ss -N -uroot -ppassword -e "use c$casenum;source /case_data/$casenum/c$casenum.sql";'
else
echo "Customer DB less than 2 GB so skipping screen session!!!"
echo "Customer DB import started. Please wait till the import is completed"
mysql -ss -N -uroot -ppassword -e "use c$casenum;source /case_data/$casenum/c$casenum.sql;"
echo "Customer DB import completed successfully!!!"
fi
else
echo "Directory does not exist, please check the case number entered"
fi
1条答案
按热度按时间l2osamch1#
你误解了
screen -X
'.-x将指定的命令发送到正在运行的屏幕会话。
这里指定的命令是指屏幕命令,它是在屏幕的命令行模式(c-a:)或screenrc文件中使用的命令。
screen -S 24602 -X 'mysql ...'
不起作用,因为mysql
是bash外部命令,不是screen命令。替换为
screen -S 24602 -X screen mysql ...
应该有用(注意:没有引号。)它将运行mysql ...
在会话24602的新窗口中,您可以点击c-a\u c-a切换窗口。顺便说一句,
screen -S 24602
不是说“套接字号24602”,而是说会话名是24602。