0

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
1

Update (from our chat), I think a case statement works better for what you are trying to accomplish here:

#!/bin/bash

clear
lred='\033[1;31m'
red='\033[0;31m'
NC='\033[0m' # No Color
blue='\033[0;34m'
lblue='\033[1;34m'

# tells echo to enable backslash escapes
echo -e "${red}Welcome to Lab 7 Utilities Menu ${NC}"
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

case $num in
        1)
           ps aux
           echo -e "${lred}The list has been successfully generated! ${NC}"
        ;;
        2)
           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
        ;;
        3)
           ping -c 4 127.0.0.1
           echo -e "${lred}You have completed pinging localhost. ${NC}"
        ;;
        4)
           clear
        ;;
        *)
           echo -e "${red}Please choose between number 1 and 4. ${NC}"
        ;;
esac


Are you sure you want to copy your passwd file to the /home directory? Other than that, this should work:

#!/bin/bash
clear
read num
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 a number that isn't '2'"
fi

The -d argument checks if FILE exists and is a directory. What you want is -e which checks FILE exists.

Also you were trying to pipe | after the then, which would cause more conflicts as it is incorrect for what you are trying to do.

  • i'm in VM so just testing out. I've tried but It still isn't working properly. This is just a part of my full script. And for some reason whenever I execute one of other options, the "The directory does not exist" shows up. I want it to check just for one of the scripts, not any other. – Hp88 Feb 23 '15 at 3:32
  • @Hp88 Are you trying to check if the /etc/passwd file exists of the /home directory exists first before copying? Also edit your question and post your full script if it is having problems. I can't speak for the rest of the script not working without seeing it. – devnull Feb 23 '15 at 3:35
  • @Hp88 I also fixed the script. It was wrong. Sorry, it's late and I'm starting to see stars. :D lol – devnull Feb 23 '15 at 3:50
  • Thanks, I tried it, but it still does not tie up to my full script, I edited my entire script. Thanks for your help! – Hp88 Feb 23 '15 at 4:29
  • @Hp88, I updated my answer with a case statement of your script. I tested it and everything works. See if that works for you. It should give you more flexibility than your nested if-then-else statements. – devnull Feb 23 '15 at 5:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.