gpt4 book ai didi

Linux shell 重命名文件(如果存在)

转载 作者:太空狗 更新时间:2023-10-29 12:05:44 25 4
gpt4 key购买 nike

嘿,我有以下代码检查文件是否已经存在于它被删除之前的原始位置。
这行得通,如果它不存在,那么它将被移动,这也行得通,但我的问题是如何让 shell 脚本要求用户指定一个新名称以将文件重命名为,然后如果已经存在同名文件,则将其存储在新名称下。重命名的代码应该在 if

的第一部分中的 echo 下面
#! /bin/sh
#restore the dustbin contents

if [[ -f "$(grep $1 /root/TAM/store) " ]]; then
echo "A File with the same name already exists please rename this file to store it"
else
mv /root/TAM/dustbin/$1 `grep $1 /root/TAM/store`
cd /root/TAM
rm store
touch store
fi

编辑

read -p "please enter a new name:  " newName
mv /root/TAM/dustbin/$1 /root/TAM/dustbin/$newName
cd /root/TAM
rm store
touch store

最佳答案

要提示用户,请尝试这样做:

read -p "New name >>> " name
echo "The file name is $name"

关于Linux shell 重命名文件(如果存在),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13659439/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com