在linux中通过bash shell脚本更新phpMyAdmin-blowfish_-secret

在linux中通过bash shell脚本更新phpMyAdmin-blowfish_-secret,linux,bash,sed,Linux,Bash,Sed,我正在编写一个bash脚本,自动下载phpMyAdmin并提取它。我想在此安装程序脚本中再添加一个步骤 将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/

我正在编写一个bash脚本,自动下载phpMyAdmin并提取它。我想在此安装程序脚本中再添加一个步骤

将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']

有什么想法吗?

有几点:

你不必以;——换行符具有相同的效果。 如果您想重定向stdout和stderr,可以使用&>/dev/null而不是>/dev/null 2>/dev/null,但是在解压的情况下,您可以使用unzip-q来抑制输出,甚至-qq,但是-q对我来说已经是沉默的。 而不是

cd phpMyAdmin-4.5.3.1-english;
mv * ..;
cd ..;
您只需使用mv phpMyAdmin-4.5.3.1-english/*

有两个以“.”开头的文件,除非设置了dotglob shell选项,否则不会随命令移动,因此必须单独移动它们:

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 file | sed s/x/y/g>output用y替换文件中的所有x,save to output相当于sed s/x/y/g file>output,但后者不会产生额外的子shell。 你的正则表达式

s/cfg['blowfish_secret'] = ''/
被解释为cfg,并且列表中的任意一个字符介于[和]之间,但您需要文字[和],因此必须对它们进行转义:\[和]。在替换字符串中,它们不必转义

openssl rand生成的密码可以包含前斜杠,这会混淆。您可以为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
有几点:

你不必以;——换行符具有相同的效果。 如果您想重定向stdout和stderr,可以使用&>/dev/null而不是>/dev/null 2>/dev/null,但是在解压的情况下,您可以使用unzip-q来抑制输出,甚至-qq,但是-q对我来说已经是沉默的。 而不是

cd phpMyAdmin-4.5.3.1-english;
mv * ..;
cd ..;
您只需使用mv phpMyAdmin-4.5.3.1-english/*

有两个以“.”开头的文件,除非设置了dotglob shell选项,否则不会随命令移动,因此必须单独移动它们:

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 file | sed s/x/y/g>output用y替换文件中的所有x,save to output相当于sed s/x/y/g file>output,但后者不会产生额外的子shell。 你的正则表达式

s/cfg['blowfish_secret'] = ''/
被解释为cfg,并且列表中的任意一个字符介于[和]之间,但您需要文字[和],因此必须对它们进行转义:\[和]。在替换字符串中,它们不必转义

openssl rand生成的密码可以包含前斜杠,这会混淆。您可以为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

你可能想在regex:cfg\['blowfish中转义[],告诉sed它们不是用于字符类的。这些是我需要转义的唯一字符吗?我可以这么说:你不需要cat,顺便说一句:cat file | sed s/x/y>输出与sed s/x/y file>输出相同,并且不会生成子shell。@BenjaminW.我尝试了转义[]它没有工作,config.inc.php文件是空的。在您的sed-only示例中,什么是s/x/y参数?您可能希望在regex:cfg中转义[]\['blowfish…告诉sed它们不适合字符类。这些是我需要转义的唯一字符吗?我会这么说:你不需要cat,顺便说一句:cat文件| sed s/x/y>输出与sed s/x/y文件>输出相同,不会产生子shell。@BenjaminW。我尝试转义了[]而且它没有工作,config.inc.php文件是空的 例如,什么是s/x/y参数?