如何删除一个文件名的shell脚本扩展? [英] How can remove the extension of a filename in a shell script?
本文介绍了如何删除一个文件名的shell脚本扩展?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这有什么错如下code?
What's wrong with the following code?
name='$filename | cut -f1 -d'.''
由于是,我得到的文本字符串'$文件名|切-f1 -d。但如果我删除引号我没有得到任何东西。同时键入
As is, I get the literal string '$filename | cut -f1 -d'.' but if I remove the quotes I don't get anything. Meanwhile typing
"test.exe" | cut -f1 -d'.'
在shell给我输出我想要的,测试。我已经知道$文件名已经分配了正确的价值。我想要做的是分配给一个变量名不带扩展名。
in a shell gives me the output I want, "test". I already know $filename has been assigned the right value. What I want to do is assign to a variable the filename without the extension.
推荐答案
您应该使用$(命令)当你想在脚本/命令来执行命令。
You should be using $(command) when you want to execute a command in script/command.
所以,你行应为
name=$(echo $filename | cut -f 1 -d '.')
code解释:
Code explanation:
-
回声
获得变量的值$文件名
,并将其发送到标准输出 - 我们然后抓住输出和管道它到
剪切
命令 - 的
剪切
将使用。作为分隔符(也称为分离器),用于切割串入段,由-f
我们选择,我们希望在输出哪个段 - 然后
$()
包装将得到的输出,并返回其值 - 返回值将被分配到指定变量
名称
echo
get the value of the variable$filename
and send it to standard output- We then grab the output and pipe it to the
cut
command - The
cut
will use the . as delimiter (also known as separator) for cutting the string into segments and by-f
we select which segment we want to have in output - Then the
$()
wrapper will get the output and return it's value - The returned value will be assigned to the variable named
name
这篇关于如何删除一个文件名的shell脚本扩展?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文