[英]How to modify a file name within a shell script?
I am writing a shell script to sync to a github repo, kick off the build, then take the output file, rename it, and move it to a location where it can be seen by Apache. 我正在编写一个shell脚本以同步到github存储库,开始构建,然后获取输出文件,对其进行重命名,然后将其移动到Apache可以看到的位置。
It's the renaming of the file that I've got not the faintest how to do within a shell script (I have virtually no experience with shell scripts - my understanding 这是文件的重命名,我还没有在shell脚本中最隐秘的操作方法(我几乎没有shell脚本的经验-我的理解
Compiler will create /var/espbuild/firstpart_1vXX_secondpart.bin 编译器将创建/var/espbuild/firstpart_1vXX_secondpart.bin
I need to move this file to: 我需要将此文件移至:
/var/www/html/builds/espbuild/firstpart_1vXX_DATE_secondpart_postfix.bin /var/www/html/builds/espbuild/firstpart_1vXX_DATE_secondpart_postfix.bin
1vXX is the version number 1vXX是版本号
DATE is the output of date +%m-%d DATE是日期+%m-%d的输出
postfix is just a string. 后缀只是一个字符串。
I'm not really certain where to start for something like this - I'm sure there's a graceful way, since this is the kind of thing shell scripts are made for, but I know just about nothing about shell scripts. 我不太确定从何处开始这样的事情-我敢肯定有一种优美的方法,因为这是Shell脚本的目的,但我对Shell脚本一无所知。
Thanks in advance 提前致谢
You can get the result of a command into a variable by using $()
: 您可以使用
$()
将命令的结果转换为变量:
DATE=$(date +%m-%d)
Then just use it in the new filename: 然后在新文件名中使用它:
INPUT=/var/espbuild/firstpart_1vXX_secondpart.bin
OUTPUT=/var/www/html/builds/espbuild/firstpart_1vXX_${DATE}_secondpart_postfix.bin
mv ${INPUT} ${OUTPUT}
Edit: To get out the version part, here's a quick example: 编辑:要获取版本部分,这是一个简单的示例:
VERSION=$(grep -o 1v.. <<< ${INPUT})
Then OUTPUT
should be set like: 然后应将
OUTPUT
设置为:
OUTPUT=/var/www/html/builds/espbuild/firstpart_${VERSION}_${DATE}_secondpart_postfix.bin
You can use this in BASH: 您可以在BASH中使用它:
f='/var/espbuild/firstpart_1vXX_secondpart.bin'
s="${f##*/}"
s2=${s##*_}
dest="/var/www/html/builds/espbuild/${s%_*}_$(date '+%m-%d')_${s2%.*}_postfix.bin"
echo "$dest"
/var/www/html/builds/espbuild/firstpart_1vXX_07-14_secondpart_postfix.bin
cp "$f" "$dest"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.