Linux shell重命名文件(如果存在)
嘿,我有下面的代码来检查文件在被删除之前是否已经存在于它原来所在的位置。Linux shell重命名文件(如果存在),linux,shell,Linux,Shell,嘿,我有下面的代码来检查文件在被删除之前是否已经存在于它原来所在的位置。 这是可行的,如果它不存在,那么它将被移动,这也是可行的, 但我的问题是,如果已经存在同名文件,如何让shell脚本要求用户指定一个新名称来重命名文件,然后将其存储在新名称下。 重命名代码应位于if #! /bin/sh #restore the dustbin contents if [[ -f "$(grep $1 /root/TAM/store) " ]]; then echo "A File with th
这是可行的,如果它不存在,那么它将被移动,这也是可行的, 但我的问题是,如果已经存在同名文件,如何让shell脚本要求用户指定一个新名称来重命名文件,然后将其存储在新名称下。 重命名代码应位于
if
#! /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 "New name >>> " name
echo "The file name is $name"
@我将此代码
改为-p“请输入新名称”newName
然后读$newName
和mv/root/TAM/dustbin/$1$newName
但是这不起作用,为什么?你能看看我上面的新编辑,给我一些关于如何将文件移动到目的地的提示吗grep$1/root/TAM/store
至于我的生活,我就是想不出怎么做我试过mv那样的版本这个mv/root/TAM/dustbin/$newName
grep$1/root/TAM/store,但我确信它不起作用mv/root/TAM/dustbin/$newName`grep$1/root/TAM/store`
。请注意grep表达式周围的`符号。您的脚本似乎已损坏。最好用英语告诉我们你真正想做什么,你的目标是什么&背景也许我忘记了墓穴钥匙,我会再试一次
read -p "New name >>> " name
echo "The file name is $name"