I am learning bash, I ran into a small problem when trying to write a small script
#!/bin/bash
clear
read num
if [ "$num" -eq 2 ]
then [ -d "/etc/passwd" ] | sudo cp /etc/passwd /home
echo -e "${lred}The passwd file has been copied to your home directory. ${NC}"
else
echo "The directory does not exist"
fi
the problem that I have is I want the directory to be checked, else display the second echo "The directory does not exist" when attempting to run the copy command. I get "cp: cannot stat ‘/et/passwd’: No such file or directory" but the first echo that states "The passwd file has been copied to your home directory." still shows up and the second echo "The file does not exists" does not. If someone has a solution, I really appreciate it. Thanks!
Edit: here is my entire script
#!/bin/bash
clear
lred='\033[1;31m'
red='\033[0;31m'
NC='\033[0m' # No Color
blue='\033[0;34m'
lblue='\033[1;34m'
echo -e "${red}Welcome to Lab 7 Utilities Menu ${NC}" # tells echo to enable backslash escapes
sleep 3
clear
echo -e "${lblue}Choose one of the options from the following list:${NC}"
echo -e "${blue}1. Monitor existing processes ${NC}"
echo -e "${blue}2. Copy passwd to /home directory ${NC}"
echo -e "${blue}3. Ping local host ${NC}"
echo -e "${lred}4. Exit ${NC}"
read num
if [ $num -eq 1 ]
then ps aux
echo -e "${lred}The list has been successfully generated! ${NC}"
fi
if [ "$num" -eq 2 ]; then
if [ -e "/etc/passwd" ]; then
sudo cp /etc/passwd /home
echo -e "${lred}The passwd file has been copied to your home directory. ${NC}"
else
echo "The File does not exist"
fi
else
echo "You entered number that isn't 2"
fi
if [ "$num" -eq 3 ]
then ping -c 4 127.0.0.1
echo -e "${lred}You have completed pinging localhost. ${NC}"
elif [ "$num" -eq 4 ]
then clear
elif [ "$num" -gt 4 ]
then echo -e "${red}Please choose between number 1 and 4. ${NC}"
clear
fi