在 bash 别名命令中,如何使 PHP 脚本的结果可用于 bash 脚本的其余部分?

In bash alias command, how to make a result of PHP script available to the rest of the bash script?

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

我 运行 的 PHP 脚本是:

php getValue.php 

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

PHP 将值打印到标准输出:

<?php
echo 32;

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

#!/bin/bash

mv test.txt "test$(php getValue.php).txt"

getValue.php:

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

script.sh:

#!/bin/bash
phpvalue=`php getValue.php`

file=
extension="${file##*.}"
filename="${file%.*}"
mv "$file" "${filename}${phpvalue}.${extension}"

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