我是 linux 和 bash 编程/脚本的新手。无论如何,我想制作一个 bash 脚本来做一些事情:
- 将获得关于要复制哪些文件的输入
- 将获得 dest 文件夹
- 会询问是复制还是移动
- 将执行所需的操作
- 最后会询问是否继续执行其他操作
现在,至于第 2、3、5 部分我还可以,但第 1 部分有点问题。所以,这是脚本,我希望你能告诉我我错在哪里以及为什么:)
#! /bin/bash
#beginning
clear
f=1
Beginning()
{
echo "HELLO $USER , THIS PROGRAM WILL COPY THE FILES YOU WANT TO YOUR DESIRED DESTINATION DIRECTORY"
echo -n "please enter the filname/s you want to copy (up to 6 files at a time): ";
echo
EnteringF
}
EnteringF()
{
echo " filename$f : ";
read cop$f
Confirm
}
Confirm()
{
echo "do you wish to copy another file (y/n)?";
read ans
if [ "$ans" = "y" ] || [ "$ans" = "Y" ]; then
f=$[sum=$f+1]
EnteringF
elif [ "$ans" = "n" ] || [ "$ans" = "N" ]; then
Destination
else
echo " invalid choice please try again "
Confirm
fi
}
Destination()
{
echo -n "now please enter the destination folder:";
read dest;
CopyMove
}
CopyMove()
{
echo -n "Do you want to copy or move the files/s? please enter c/m? ";
read choice;
if [ "$choice" = "c" ] || [ "$choice" = "C" ]; then
cp "$cop$f" "$dest";
echo "copied successfully"
UseAgain
elif [ "$choice" = "m" ] || [ "$choice" = "M" ] ; then
mv [ "$files" + "$f" ] "$dest"
echo "moved successfully"
UseAgain
else
echo " invalid choice please try again ";
CopyMove
fi
}
UseAgain()
{
echo "Do you want to use the program again y/n?";
read confirm
if [ "$confirm" = "y" ] || [ "$confirm" = "Y" ]; then
Beginning
UseAgain
elif [ "$confirm" = "n" ] || [ "$confirm" = "N" ]; then
End
else
echo "invalid choice please try again"; UseAgain
fi
}
End()
{
clear
echo "THANK YOU $USER FOR USING THIS PROGRAM :-)"
}
Beginning
现在我的主要问题是没有数据记录到 cop 变量中,因此cp
没有任何内容要复制。也许我应该使用其他循环,但我仍然不知道如何使用其他循环。