bash if、then、else文

bash if、then、else文

Bashを学んでいますが、小さなスクリプトを書こうとすると小さな問題に遭遇しました。

#!/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

私が経験している問題は、ディレクトリを確認したいということです。それ以外の場合は、コピーコマンドを実行しようとすると、「ディレクトリが存在しません」という2番目のエコーが発生します。 "cp:can stat '/et/passwd': 対応するファイルやディレクトリはありません。" 「ファイルが存在しません」と表示されます。表示されません。誰でも解決策があれば、本当に感謝します。ありがとうございます!

編集:ここに私の完全なスクリプトがあります

#!/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

(チャットから)更新してください。ここで達成しようとしている作業にケースの説明がより適していると思います。

#!/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


passwdファイルをこの/homeディレクトリにコピーしますか?それ以外の場合は、次のように動作します。

#!/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

この-dパラメータは次のことを確認します。FILE exists and is a directory-eどの小切手が欲しいですFILE existsか?

さらに、あなたがしたいことに合わないので、より多くの衝突を引き起こした|後でパイプを試しています。then

おすすめ記事