下面的代码有什么问题?
name='$filename | cut -f1 -d'.''
就像这样,我得到的字面值字符串$filename | cut -f1 -d'。',但如果我删除引号,我什么也得不到。与此同时,打字
"test.exe" | cut -f1 -d'.'
在shell中给出我想要的输出,test。我已经知道$filename已经被分配了正确的值。我要做的是给一个变量分配没有扩展名的文件名。
下面的代码有什么问题?
name='$filename | cut -f1 -d'.''
就像这样,我得到的字面值字符串$filename | cut -f1 -d'。',但如果我删除引号,我什么也得不到。与此同时,打字
"test.exe" | cut -f1 -d'.'
在shell中给出我想要的输出,test。我已经知道$filename已经被分配了正确的值。我要做的是给一个变量分配没有扩展名的文件名。
当前回答
如果你的文件名包含一个点(除了扩展名),那么使用这个:
echo $filename | rev | cut -f 2- -d '.' | rev
其他回答
正如Hawker65在chepner answer的评论中指出的那样,投票最多的解决方案既不关心多个扩展名(如filename.tar.gz),也不关心路径其余部分的点(如this.path/with.dots/in.path.name)。 一个可能的解决方案是:
a=this.path/with.dots/in.path.name/filename.tar.gz
echo $(dirname $a)/$(basename $a | cut -d. -f1)
当您希望在script/command中执行命令时,应该使用命令替换语法$(command)。
所以直线是
name=$(echo "$filename" | cut -f 1 -d '.')
代码的解释:
Echo获取变量$filename的值并将其发送到标准输出 然后获取输出并将其输送到cut命令 切割将使用。作为分隔符(也称为分隔符),用于将字符串切割成段,并通过-f选择希望在输出中出现的段 然后$()命令替换将获得输出并返回其值 返回值将被赋给名为name的变量
注意,这给出了到第一个周期的变量部分:
$ filename=hello.world
$ echo "$filename" | cut -f 1 -d '.'
hello
$ filename=hello.hello.hello
$ echo "$filename" | cut -f 1 -d '.'
hello
$ filename=hello
$ echo "$filename" | cut -f 1 -d '.'
hello
在Zsh:
fullname=bridge.zip
echo ${fullname:r}
它简单,干净,可以被链接以删除多个扩展:
fullname=bridge.tar.gz
echo ${fullname:r:r}
它可以与其他类似的修饰语结合使用。
仅使用POSIX的内置:
#!/usr/bin/env sh
path=this.path/with.dots/in.path.name/filename.tar.gz
# Get the basedir without external command
# by stripping out shortest trailing match of / followed by anything
dirname=${path%/*}
# Get the basename without external command
# by stripping out longest leading match of anything followed by /
basename=${path##*/}
# Strip uptmost trailing extension only
# by stripping out shortest trailing match of dot followed by anything
oneextless=${basename%.*}; echo "$oneextless"
# Strip all extensions
# by stripping out longest trailing match of dot followed by anything
noext=${basename%%.*}; echo "$noext"
# Printout demo
printf %s\\n "$path" "$dirname" "$basename" "$oneextless" "$noext"
打印输出的演示:
this.path/with.dots/in.path.name/filename.tar.gz
this.path/with.dots/in.path.name
filename.tar.gz
filename.tar
filename
这个包含了所有的可能性!(点在路径上与否;延长或不延长):
Tmp1 =${filename##*/};tmp2=${tmp1:1};filename_noextension=$(echo -n ${Tmp1:0:1};echo ${tmp2%.*})
注:
它提供了没有任何扩展名的文件名。因此$filename_noextension变量中没有路径。 最后得到两个不需要的变量$tmp1和$tmp2。确保没有在脚本中使用它们。
测试示例:
文件名= . bashrc;Echo“filename: $filename”;文件名tmp1 = $ {# # * /}; tmp2 = $ {tmp1:1}; filename_noextension = $ (echo - n $ {tmp1:0:1}; echo $ {tmp2%。*});Echo“没有扩展名的文件名:$filename_noextension”
文件名= .bashrc.txt;Echo“filename: $filename”;文件名tmp1 = $ {# # * /}; tmp2 = $ {tmp1:1}; filename_noextension = $ (echo - n $ {tmp1:0:1}; echo $ {tmp2%。*});Echo“没有扩展名的文件名:$filename_noextension”
文件名= .bashrc.txt.tar;Echo“filename: $filename”;文件名tmp1 = $ {# # * /}; tmp2 = $ {tmp1:1}; filename_noextension = $ (echo - n $ {tmp1:0:1}; echo $ {tmp2%。*});Echo“没有扩展名的文件名:$filename_noextension”
文件名= ~ / . bashrc;Echo“filename: $filename”;文件名tmp1 = $ {# # * /}; tmp2 = $ {tmp1:1}; filename_noextension = $ (echo - n $ {tmp1:0:1}; echo $ {tmp2%。*});Echo“没有扩展名的文件名:$filename_noextension”
文件名= ~ / .bashrc.txt.tar;Echo“filename: $filename”;文件名tmp1 = $ {# # * /}; tmp2 = $ {tmp1:1}; filename_noextension = $ (echo - n $ {tmp1:0:1}; echo $ {tmp2%。*});Echo“没有扩展名的文件名:$filename_noextension”
文件名= bashrc; (Echo“filename: $filename”;文件名tmp1 = $ {# # * /}; tmp2 = $ {tmp1:1}; filename_noextension = $ (echo - n $ {tmp1:0:1}; echo $ {tmp2%。*});Echo“没有扩展名的文件名:$filename_noextension”
文件名= bashrc.txt;Echo“filename: $filename”;文件名tmp1 = $ {# # * /}; tmp2 = $ {tmp1:1}; filename_noextension = $ (echo - n $ {tmp1:0:1}; echo $ {tmp2%。*});Echo“没有扩展名的文件名:$filename_noextension”
文件名= bashrc.txt.tar;Echo“filename: $filename”;文件名tmp1 = $ {# # * /}; tmp2 = $ {tmp1:1}; filename_noextension = $ (echo - n $ {tmp1:0:1}; echo $ {tmp2%。*});Echo“没有扩展名的文件名:$filename_noextension”
文件名= ~ / bashrc; (Echo“filename: $filename”;文件名tmp1 = $ {# # * /}; tmp2 = $ {tmp1:1}; filename_noextension = $ (echo - n $ {tmp1:0:1}; echo $ {tmp2%。*});Echo“没有扩展名的文件名:$filename_noextension”
文件名= ~ / bashrc.txt.tar;Echo“filename: $filename”;文件名tmp1 = $ {# # * /}; tmp2 = $ {tmp1:1}; filename_noextension = $ (echo - n $ {tmp1:0:1}; echo $ {tmp2%。*});Echo“没有扩展名的文件名:$filename_noextension”