使用外部文件中的变量控制bash脚本
发布时间:2020-12-15 18:33:39 所属栏目:安全 来源:网络整理
导读:我想像这样控制一个bash脚本: #!/bin/shUSER1=_parsefromfile_HOST1=_parsefromfile_PW1=_parsefromfile_USER2=_parsefromfile_HOST2=_parsefromfile_PW2=_parsefromfile_imapsync --buffersize 8192000 --nosyncacls --subscribe --syncinternaldates --I
我想像这样控制一个bash脚本:
#!/bin/sh USER1=_parsefromfile_ HOST1=_parsefromfile_ PW1=_parsefromfile_ USER2=_parsefromfile_ HOST2=_parsefromfile_ PW2=_parsefromfile_ imapsync --buffersize 8192000 --nosyncacls --subscribe --syncinternaldates --IgnoreSizeErrors --host1 $HOST1 --user1 $USER1 --password1 $PW1 --ssl1 --port1 993 --noauthmd5 --host2 $HOST2 --user2 $USER2 --password2 $PW2 --ssl2 --port2 993 --noauthmd5 --allowsizemismatch 使用来自控制文件的参数,如下所示: host1 user1 password1 host2 user2 password2 anotherhost1 anotheruser1 anotherpassword1 anotherhost2 anotheruser2 anotherpassword2 其中每一行代表脚本的一次运行,其中参数被提取并变为变量. 这样做最优雅的方式是什么? 拍
使用shell脚本,这通常使用source函数来完成,该函数将文件作为shell脚本执行,就好像它被内联到您正在运行的脚本中一样 – 这意味着您在文件中设置的任何变量都会导出到您的脚本中.
缺点是(a)您的配置文件已执行,因此如果没有特权的用户可以编辑特权配置文件,则存在安全风险. (b)您的配置文件语法仅限于有效的bash语法.不过,这真的很方便. config.conf USER=joe PASS=hello SERVER=127.0.0.2 script.sh #!/bin/bash # Set defaults USER=`whoami` # Load config values source config.conf foobar2000 --user=$USER --pass=$PASS --HOST=$HOST source可以缩写为单个. – 所以以下两个是等价的: source file.sh . file.sh (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |