【问题标题】:In bash alias command, how to make a result of PHP script available to the rest of the bash script?在 bash 别名命令中,如何使 PHP 脚本的结果可用于 bash 脚本的其余部分?
【发布时间】:2020-04-26 22:51:39
【问题描述】:

所以,PHP 脚本返回(或者可能需要回显该值?)一个值,例如32,我希望其余的别名将文件test.txt 重命名为test32.txt,因此将那个值(32)附加到test,然后附加值.txt,以便重命名它。从 PHP 中获取这个值并在命令mv test.txt test32.txt 中使用它的命令是什么?

我运行的 PHP 脚本是:

php getValue.php 

此命令返回一个数字值(例如 32)。

【问题讨论】:

    标签: php bash alias


    【解决方案1】:

    让 PHP 将值打印到标准输出:

    <?php
    echo 32;
    

    然后使用command substitution 将输出捕获为字符串:

    #!/bin/bash
    
    mv test.txt "test$(php getValue.php).txt"
    

    【讨论】:

      【解决方案2】:

      getValue.php:

      <?php
      echo rand(0,100);
      ?>
      

      script.sh:

      #!/bin/bash
      phpvalue=`php getValue.php`
      
      file=$1
      extension="${file##*.}"
      filename="${file%.*}"
      mv "$file" "${filename}${phpvalue}.${extension}"
      

      用法示例:./script.sh test.txt

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-09-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-02-04
        • 2012-06-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多