我要从巴什搬到努希尔。我的一个步骤是移动这个函数:
ex ()
{
if [ -f $1 ] ; then
case $1 in
*.tar.bz2) tar xjf $1 ;;
*.tar.gz) tar xzf $1 ;;
*.bz2) bunzip2 $1 ;;
*.rar) unrar x $1 ;;
*.gz) gunzip $1 ;;
*.tar) tar xf $1 ;;
*.tbz2) tar xjf $1 ;;
*.tgz) tar xzf $1 ;;
*.zip) unzip $1 ;;
*.Z) uncompress $1;;
*.7z) 7z x $1 ;;
*) echo "'$1' cannot be extracted via ex()" ;;
esac
else
echo "'$1' is not a valid file"
fi
}我在“努谢尔”中写道:
def ex [$file?: string] {
if $file == null {"No file defined"} else {
if $file == *.tar.bz2 {
tar xjf $file;
}
else if $file == *.tar.gz {
tar xzf $file;
}
else if $file == *.bz2 {
bunzip2 $file;
}
else if $file == *.rar {
unzip $file;
}
else if $file == *.gz {
gunzip $file;
}
else if $file == *.tar {
tar xf $file;
}
else if $file == *.tbz2 {
tar xjf $file;
}
else if $file == *.tgz {
tar xzf $file;
}
else if $file == *.zip {
unzip $file;
}
else if $file == *.Z {
uncompress $file;
}
else if $file == *.7z {
7z x $file;
}
}
}但是当我用这个命令测试它时(我在执行命令的目录中有一个openssl源代码存档):ex openssl-1.1.1.tar.gz,我得到了以下错误:
ex openssl-1.1.1.tar.gz
Error: nu::shell::external_command (link)
× External command failed
╭─[/home/ysyltbya/.config/nushell/config.nu:523:1]
523 │ }
524 │ else if $file == *.tar.gz {
· ──┬─
· ╰── did you mean 'ls'?
525 │ tar xzf $file;
╰────
help: No such file or directory (os error 2)我不明白问题出在哪里。
发布于 2022-11-25 20:11:24
主要问题是您仍然试图使用Bash模式进行字符串匹配。您可以在Nushell中使用以下两种方法来完成这一任务:
ends-with字符串比较操作符:如果$file以“.tbz2”结尾.
如果$file =~ '.*.tbz2‘.
但是,您可能会考虑一种更实用的/数据驱动的/Nushell方式:
def ex [$file?: string] {
let archivers = [
[ pattern , command , options ];
[ ".tar.bz2" , "tar" , "xjf" ]
[ ".tar.gz" , "tar" , "xzf" ]
[ ".bz2" , "unzip2" , "" ]
[ ".tar.bz2" , "tar" , "xjf" ]
[ ".rar" , "unrar" , "" ]
[ ".gz" , "gunzip" , "" ]
[ ".tar" , "tar" , "xf" ]
[ ".tbz2" , "tar" , "xjf" ]
[ ".tgz" , "tar" , "xzf" ]
[ ".zip" , "unzip" , "" ]
[ ".Z" , "uncompress" , "" ]
[ ".7z" , "7z" , "x" ]
]
if $file == null {
print -e "No file defined"
} else if not ($file | path exists) {
print -e $"Can't find ($file)"
} else {
let matchingArchivers = ($archivers | where { |archiver| $file ends-with $archiver.pattern })
if ($matchingArchivers | length) > 0 {
let archiver = $matchingArchivers.0
run-external $archiver.command $archiver.options $file
} else {
print -e $"($file) cannot be extracted via ex\(\)"
}
}
}备注:
使用这种形式的
case。rar文件上使用的unzip命令。case或if/else的正常“短路”行为,但它的影响很小。case的行为。https://stackoverflow.com/questions/74575468
复制相似问题