下面的代码有什么问题?
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已经被分配了正确的值。我要做的是给一个变量分配没有扩展名的文件名。
当前回答
之前提供的答案在包含点的路径上有问题。一些例子:
/xyz.dir/file.ext
./file.ext
/a.b.c/x.ddd.txt
我更喜欢使用|sed -e 's/\.[^./]*$//'。例如:
$ echo "/xyz.dir/file.ext" | sed -e 's/\.[^./]*$//'
/xyz.dir/file
$ echo "./file.ext" | sed -e 's/\.[^./]*$//'
./file
$ echo "/a.b.c/x.ddd.txt" | sed -e 's/\.[^./]*$//'
/a.b.c/x.ddd
注意:如果你想删除多个扩展(就像上一个例子一样),使用|sed -e 's/\.[^/]*$//':
$ echo "/a.b.c/x.ddd.txt" | sed -e 's/\.[^/]*$//'
/a.b.c/x
但是,该方法在没有扩展名的“点文件”中会失败:
$ echo "/a.b.c/.profile" | sed -e 's/\.[^./]*$//'
/a.b.c/
为了涵盖这些情况,你可以使用:
$ echo "/a.b.c/.profile" | sed -re 's/(^.*[^/])\.[^./]*$/\1/'
/a.b.c/.profile
其他回答
你的代码有两个问题:
您使用' (tick)而不是'(反tick)来包围生成想要存储在变量中的字符串的命令。 您没有将变量“$filename”“echo”到管道中的“cut”命令中。
我会把你的代码改为"name= ' echo $filename | cut -f 1 -d '。' ' ",如下所示(再次注意,后面的勾号围绕着name变量定义):
$> filename=foo.txt
$> echo $filename
foo.txt
$> name=`echo $filename | cut -f1 -d'.'`
$> echo $name
foo
$>
假设你的文件有。new扩展名
ls -1 | awk '{ print "mv "$1" `basename "$1" .new`"}' | sh
由于张贴后不显示特别报价,请参阅图片。
#!/bin/bash
filename=program.c
name=$(basename "$filename" .c)
echo "$name"
输出:
program
这个包含了所有的可能性!(点在路径上与否;延长或不延长):
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”
仅使用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