How to create a shell script that would create a copy of a file (selected by the user) and copy it to a directory (selected by the user) in Bash?
-1
votes
1
answer
2704
views
How to solve this task?
> To create a copy of a file.
>
> - The file should be in your current directory (the name of the file to be given by the user)
> - The destination directory name to be given by the user.
This is how far I've got:
echo "Enter the name of the file:"
read filename
cp -r /home/$FILE ;;
Just to add more info, this is what the full exercise is based on:
The script should offer a menu with the following options:
1. To create a backup copy of a script file.
> The name of the backup copy should have backup after the name of the script
and be date stamped e.g. work3Script_backup_10_11_2015.
> It should be saved to your home directory using the Environment Variable
for your home directory.
> The script should error check that the file exists and is a normal file. If
this is not the case then the script should allow the user to re-enter the
filename until a valid filename is entered
2. To create a date stamped log file called e.g. log_file_10_11_2015
containing
> A list of who is logged into the system
> The disk usage and
> Your currently running processes.
> The file should be saved to an existing directory called log_dir which
should
be situated off your home directory
3. To create a copy of a file.
> The file should be in your current directory (the name of the file to be
given by the user)
> The destination directory name to be given by the user.
> The script should error check that the file exists and is a normal file. If this is not the case then the script should allow the user to re-enter the filename until a valid filename is entered.
> The script should check that the destination directory exists. If this is
not the case then the script should allow the user to re-enter the
destination directory until a valid directory is entered.
Here is the menu I created:
#!/bin/bash
while true
do
echo "=============================="
echo "Menu"
echo "=============================="
echo "Option 1 Backing up files: "
echo "Option 2 Date stamped log file: "
echo "Option 3 Create a copy of a file: "
echo "Option 4 Moving a file to place giving by the user: "
echo "Enter q to quit the menu: "
echo -e "\n"
echo -e "Enter your option: \c"
read answer
case "$answer" in
1) while true ; do
echo "Enter the file name:"
read filename
if [ -f "$filename" ]; then
echo "The file exists."
break
else
echo "File doesn't exist."
fi
done
FILE=$(date +"outcome3_backup_%d_%m_%y.sh")
touch /home/robert/$FILE ;;
2)FILE=$(date +"log_file_%d_%m_%y.txt")
touch /home/robert/log_dir/$FILE
echo "Users online: " > /home/robert/log_dir/$FILE
(users) >> /home/robert/log_dir/$FILE
echo "Running proccesses:" >> /home/robert/log_dir/$FILE
ps -a >> /home/robert/log_dir/$FILE
echo "Available disk space:" >> /home/robert/log_dir/$FILE
df --total -h|head -n 1 >> /home/robert/log_dir/$FILE
df --total -h|tail -n 1 >> /home/robert/log_dir/$FILE ;;
3) ;;
q) exit ;;
esac
done
Asked by Angelism
(3 rep)
May 29, 2020, 10:54 PM
Last activity: May 30, 2020, 11:06 AM
Last activity: May 30, 2020, 11:06 AM