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

标签 linux bash sed

我正在开发一个 bash 脚本,它可以自动下载 phpMyAdmin 并将其解压缩。我想在此安装程序脚本中再添加一个步骤。

config.sample.inc.php 复制为 config.inc.php 并使用随机河豚 secret 更新此文件的行:

$cfg['blowfish_secret'] = ''; /* YOU MUST FILL IN THIS FOR 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 的情况下,您可以只使用 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 就够了。
  • 您可以使用更现代的 $() 而不是用于命令替换的已弃用的反引号:

    randomBlowfishSecret=$(openssl rand -base64 32)
    
  • sed 可以通过三种方式进行改进:

    • No need for cat . cat 文件 | sed "s/x/y/g"> output(将file中的所有x替换为y,保存到output) 等同于 sed "s/x/y/g"file > output,但后者不会生成额外的子 shell。
    • 你的正则表达式

      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

关于linux - 在 linux 中通过 bash shell 脚本更新 phpMyAdmin blowfish_secret,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34539132/

相关文章:

linux - 在 vagrant box 上安装 R-Script 和 RMySQL

linux - ping 回复 header

linux - Linux 中的 USB 插件/输出

linux - 如何对不同长度的列使用粘贴命令

linux - 将 flex 的命令行参数作为搜索字符串

arrays - 捕获文件和单独的列并使用数组添加内容

bash - 将命令的输出结果存储到数组中

bash - 在 makefile 中使用大括号进行变量扩展不起作用

linux - Sed/awk - 如何删除开始模式和结束模式之间的换行符。

Linux - 如何根据字段值从文件中删除某些行