我正在开发一个自动下载phpMyAdmin并将其解压缩的bash脚本。我想在此安装程序脚本中添加一个步骤。
将config.sample.inc.php
复制为config.inc.php
并使用随机的河豚秘密更新此文件的行:
$ cfg ['blowfish_secret'] =''; / *你必须为COOKIE AUTH填写这个! * /
所以,这就是我所尝试过的:
#!/bin/bash
wget -O phpMyAdmin-4.5.3.1-english.zip https://files.phpmyadmin.net/phpMyAdmin/4.5.3.1/phpMyAdmin-4.5.3.1-english.zip;
unzip phpMyAdmin-4.5.3.1-english.zip >/dev/null 2>/dev/null;
cd phpMyAdmin-4.5.3.1-english;
mv * ..;
cd ..;
rm -rf phpMyAdmin-4.5.3.1-english;
rm -rf phpMyAdmin-4.5.3.1-english.zip;
randomBlowfishSecret=`openssl rand -base64 32`;
cat config.sample.inc.php | sed -e "s/cfg['blowfish_secret'] = ''/cfg['blowfish_secret'] = '$randomBlowfishSecret'/" > config.inc.php
当此脚本运行时,将下载并解压缩phpMyAdmin并复制文件,但似乎并未将randomBlowfishSecret
设置为$cfg['blowfish_secret']
。
有什么想法吗?
答案 0 :(得分:4)
几点:
;
结束行 - 换行具有相同的效果。&>/dev/null
而不是>/dev/null 2>/dev/null
,但对于unzip
,您只需使用unzip -q
即可抑制输出(甚至是-qq
,但-q
对我来说已经沉默了。)而不是
cd phpMyAdmin-4.5.3.1-english;
mv * ..;
cd ..;
您可以使用mv phpMyAdmin-4.5.3.1-english/* .
有两个以.
开头的文件,这些文件不会随您的命令移动(除非您设置了dotglob
shell选项),因此您必须单独移动它们:< / p>
mv phpMyAdmin-4.5.3.1-english/.*.yml .
phpMyAdmin-4.5.3.1-english
现在为空,因此您可以使用rmdir
而不是rm -rf
将其删除(这会让您知道它还不是空的。)phpMyAdmin-4.5.3.1-english.zip
只是一个档案;无需递归删除它,rm -f
就足够了。可以通过三种方式改进sed
:
cat
。 cat file | sed "s/x/y/g" > output
(将x
中的所有file
替换为y
,保存为output
)相当于sed "s/x/y/g" file > output
,但后者不会产生一个额外的子壳。您的正则表达式
s/cfg['blowfish_secret'] = ''/
被解释为“cfg
,以及[
和]
之间列表中的任意一个字符”,但您需要文字[
和]
,所以他们必须被转义:\[
和\]
。在替换字符串中,不必转义它们。
openssl rand
生成的密码可以包含正斜杠,这会混淆sed。您可以为sed使用不同的分隔符,例如"s|x|y|"
而不是"s/x/y/"
。所有这些都是装饰性的,除了最后两个sed要点:那些可以打破脚本。好吧,丢失的隐藏文件也可能很烦人。
清理适用于我的版本:
#!/bin/bash
wget -O phpMyAdmin-4.5.3.1-english.zip https://files.phpmyadmin.net/phpMyAdmin/4.5.3.1/phpMyAdmin-4.5.3.1-english.zip
unzip -q phpMyAdmin-4.5.3.1-english.zip
mv phpMyAdmin-4.5.3.1-english/* .
mv phpMyAdmin-4.5.3.1-english/.*.yml .
rmdir phpMyAdmin-4.5.3.1-english
rm -f phpMyAdmin-4.5.3.1-english.zip
randomBlowfishSecret=`openssl rand -base64 32`;
sed -e "s|cfg\['blowfish_secret'\] = ''|cfg['blowfish_secret'] = '$randomBlowfishSecret'|" config.sample.inc.php > config.inc.php