linux - 在 FOR 循环中查找时如何从名称中删除扩展名

标签 linux bash shell

<分区>

好的,所以我正在做一个 shell 脚本,目的是“安全删除东西”,所以它应该在调用“/safedell.sh file1 file2 file3”时将文件 TAR 并将其发送到我名为 LIXO 的文件夹。 我是一个 shell 新手,所以我会尝试解释我的代码的每一步,以便你们理解它,到目前为止,这是我所拥有的:

#!/bin/bash
#Purpose = Safe delete
#Created on 20-03-2018
#Version 4.0
#START

##Constants##

dir="/home/cunha/LIXO"

#check to see if the imput is a file#
if ! [ -e $1 ]; then`
   echo "Not a file!"
    exit 0`
fi


###main###
##Cycle FOR so the script accepts multiple file inputs##
for file in "$@"; do
#IF the input file already exist in LIXO#
if [[ -f $dir/$file ]]; then
    echo "|||File EXISTS|||"
#IF the input file is newer than the file thats already in LIXO#
    if [[ $file -nt $2 ]]; then
            echo "file is newer"
    fi
else
echo "File doesnt exist, ziping it and moving"
fi
done`

我的问题:所以我在 LIXO 中有 2 个文件,teste1.txt 和 teste2.tar.bz2,我的问题是当我输入 teste2.txt

--> 输入:/safedell teste2.txt

--> 输出:文件不存在,正在压缩...

最佳答案

删除扩展:

filename_without_ext="${file%.*}"

这意味着,删除以点开头的最短后缀。

然后检查存档是否存在类似

if [ -e "$dir/$filename_without_ext.tar.bz2" ]; then

关于linux - 在 FOR 循环中查找时如何从名称中删除扩展名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49452590/

相关文章:

linux - 使用输入参数执行 shell 脚本

linux - 历史扩展在脚本中不起作用

regex - Linux 的 "D210"的正则表达式?

java - 安装后找不到 openjfx,除了用红色显示文件名的终端

php - UTF-8贯穿始终

linux - bash 子 shell 与 vanilla 命令执行

linux - 在Ubuntu下安装Google Chrome Stable 43.0.2357

string - 变量插值期间的 bash 行连接

bash - find 后的字符串被识别为一个参数

python - Bash 脚本传输到 Python - 将日志从服务器复制到另一个远程服务器