shell脚本介绍在开发过程中,不断有项目或产品的升级。在这个过程中,我们可以充分利用shell脚本,实现一些固定步骤的一键部署和升级。配置文件在编写脚本之前,我们可以额外编写一个配置文件作为一键部署的入参文件,脚本可以读取配置文件中的变量值。此时只需要修改配置文件中的配置,不需要修改脚本。如:test.properties[root@linux01~/test_sh]#vimtest.properties#setparametersstart#inttypeid_test=12#stringtypestr_test="testproperties"#arraywithwhitespacetypearray_test=(arr1arr2arr3)[root@linux01~/test_sh]#ll-rw-r-----1rootroot159Mar1621:43test.propertiesshell实战[root@linux01~/test_sh]#vimtest.sh#!/bin/bash#printstarttimestarttime=$(date+%Y-%m-%d\%H:%M:%S)#echotologecho"【开始执行脚本】,开始时间为:"$starttime>>test_sh.log#从其他文件源读取属性./test.propertiesecho"Parameters:cattest.properties">>test_sh.logwhilereadlinedoecho$line>>test_sh.log;done>test_sh.log#==============================================================#createdirmkdir${str_test}${id_test}echo-e"\nmkdirdirsuccess,andname是:“${str_test}${id_test}>>test_sh.log#productfilefor((i=0;我<${#array_test[@]};i++))doecho"nowarray_testiis"${#array_test[@]}>>test_sh.logecho"Thisis${array_test[i]}file">>${array_test[i]}_file.txtdone#printendtimeendtime=`date+"%Y-%m-%d%H:%M:%S"`[root@linux01~/test_sh]#ll-rw-r-----1rootroot159Mar1621:43test.properties-rwxr-x---1rootroot993Mar1621:45test.sh执行shell注意为了简单起见,我们将test.properties和test.sh放在一个目录下,方便阅读配置[root@linux01~/test_sh]#shtest.sh[root@linux01~/test_sh]#lltotal28-rw-r-----1rootroot19Mar1622:11arr1_file.txt-rw-r-----1rootroot19Mar1622:11arr2_file.txt-rw-r-----1rootroot19Mar1622:11arr3_file.txt-rw-r-----1rootroot1593月16日22:08test.propertiesdrwxr-x---2rootroot4096Mar1622:11test_properties12-rwxr-x---1rootroot969Mar1622:11test.sh-rw-r-----1rootroot469Mar1622:11test_sh.log查看日志[root@linux01~/test_sh]#vimtest_sh.log【开始执行脚本】,开始时间为:2020-03-1621:57:47参数:cattest.properties#setparametersstart#inttypeid_test=12#stringtypestr_test="testproperties"#arraywithwhitespacetypearray_test=(arr1arr2arr3)------test.propertiesend------mkdirdirsuccess,andnameis:test_properties_12nowarray_testiis3nowarray_testiis3nowarray_testiis3【执行脚本结束】,结束时间为:2020-03-1621:57:47查看生成的文件[root@linux01~/test_sh]#vimarr1_file.txt这是arr1文件[root@linux01~/test_sh]#vimarr2_file.txt这是arr2文件[root@linux01~/test_sh]#vimarr3_file.txt这是arr3文件phoenix