bash 如何在变量中存储回声 | 砍结果?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21726450/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to store in a variable an echo | cut result?
提问by user3290180
for example
例如
echo "filename.pdf" | cut -d'.' -f 1
This way I get the "filename" string. I'd like to store it in a variable called FILE and then use it like this:
这样我就得到了“文件名”字符串。我想将它存储在一个名为 FILE 的变量中,然后像这样使用它:
DIR=$PATH/$FILE.txt
So, my script wants to create a file.txt with the same name of the pdf (not a copy of the file, just the name) This way I tried to assign the result of echo | cut
所以,我的脚本想要创建一个与 pdf 同名的 file.txt(不是文件的副本,只是名称)这样我试图分配 echo | 的结果。切
FILE=
but I get only "path/.txt" so the filename is missing.
但我只得到“路径/.txt”所以文件名丢失了。
回答by Sean Bright
FILE=$(echo "filename.pdf" | cut -d'.' -f 1)
回答by anubhava
So, my script wants to create a file.txt with the same name of the pdf
所以,我的脚本想创建一个与 pdf 同名的 file.txt
You can use BASH string manipulation:
您可以使用 BASH 字符串操作:
s="filename.pdf"
p="${s/%.pdf/.txt}"
echo "$p"
filename.txt
回答by chepner
POSIX parameter expansion would read
POSIX 参数扩展将读取
file=filename.pdf
filename="${file%%.*}" # Two % will remove multiple extensions, if applicable
dir=$path/$filename.txt