Hey I have the following code which checks if the file already exists in the location that it was originally in before it was removed.
This works and if it does not exist then it will be moved which also works, But My question is how can I get the shell script to ask the user to specify a new name to rename the file to and then store it under the new name if a file with the same name exists already. The code for the rename should go underneath the echo in the first part of the if
嘿,我有下面的代码,它检查文件是否已经存在于它被删除之前的位置。这个作品,如果它不存在就会被感动也工作,但我的问题是我怎样才能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
EDIT
编辑
read -p "please enter a new name: " newName
mv /root/TAM/dustbin/$1 /root/TAM/dustbin/$newName
cd /root/TAM
rm store
touch store
1 个解决方案
#1
3
To prompt the user, try doing this :
要提示用户,请尝试以下操作:
read -p "New name >>> " name
echo "The file name is $name"
#1
3
To prompt the user, try doing this :
要提示用户,请尝试以下操作:
read -p "New name >>> " name
echo "The file name is $name"