posted by 귀염둥이채원 2021. 4. 15. 08:10

 

---------------------------------------------------------

# Syntax of while case:

---------------------------------------------------------

* case 문은 "case"로 시작하고 "esac"로 끝납니다.

* ")"는 패턴을 종료하는 데 사용됩니다.

* 명령이 있는 패턴은 절이라고하며 모든 절은 (;;)로 끝납니다.

* 별표 기호 *를 사용하여 기본 대소 문자를 정의 할 수 있습니다.

* case 문은 먼저 입력된 $ ariable을 다른 패턴과 일치시킵니다.

패턴이 일치하면 이중 세미콜론 (;;)까지 해당하는 명령 집합이 실행됩니다.

* 정규식을 지원하며 | 기호로 다중 값을 입력 가능하며 조건의 문장 끝에는 ;; 기호로 끝을 표시한다

* 대문자와 소문자는 구별한다.

 

case $variable in

pattern-1)

commands

;;

pattern-2)

commands

;;

pattern-3)

commands

;;

pattern-N)

commands

;;

*)

commands

;;

esac



조건에 다중 값을 사용하고 싶다면 "|" 연산자를 사용한다.

case $variable in

pattern-1 | pattern-2)

commands

....

....

;;

pattern-3 | pattern-4)

commands

....

....

;;

 

---------------------------------------------------------

# 월 이름이 일치하면 해당 월의 해당 이벤트를 표시

---------------------------------------------------------

* "shopt -s nocasematch"는 대소 문자에 관계없이 패턴을 일치시키는 데 사용됩니다.

 

#!/bin/bash

 

shopt -s nocasematch

printf "Enter name of the month: "

read month

 

case $month in

January)

echo " 24th January international Day of Education."

;;

February)

echo " 20 FebruaryWorld Day of Social Justice ."

;;

March)

echo "8th March International women’s day."

;;

April)

echo "7th April The World Health Day"

;;

*)

echo "No matching information found"

;;

esac

 

# output

$ bash test.sh

Enter name of the month: February

20 FebruaryWorld Day of Social Justice

 

$ bash test.sh

Enter name of the month: April

7th April The World Health Day

 

$ bash test.sh

Enter name of the month: april

7th April The World Health Day

 

$ bash test.sh

Enter name of the month: Undefine

No matching information found

 

---------------------------------------------------------

# 국가에 수도 출력

---------------------------------------------------------

#!/bin/bash

shopt -s nocasematch

echo -n "Enter the name of a country: "

read country

 

echo -n "The capital of $country is "

case $country in

UK | "United Kingdom")

echo -n "London"

;;

Turkey)

echo -n "Ankara"

;;

USA)

echo -n "Washington DC"

;;

*)

echo -n "Information not available"

;;

esac

echo ""

 

# output

$ bash test.sh

Enter the name of a country: USA

The capital of USA is Washington DC

 

---------------------------------------------------------

# case문 테스트를 위한 반복문

---------------------------------------------------------

#!/bin/bash

for string in "HELLO" "WORLD" "hello" "world" "s" "start" "end" "etc"; do

 

# case문 시작

case ${string} in

hello | HELLO)

echo "${string}: hello 일때"

;;

wo*)

echo "${string}: wo로 시작하는 단어 일때"

;;

s | start)

echo "${string}: s 혹은 start 일때"

;;

e | end)

echo "${string}: e 혹은 end 일때"

;;

*)

echo "${string}: 기타"

;;

esac

# //case문 끝

 

done

 

# output

$ bash test.sh

HELLO: hello 일때

WORLD: 기타

hello: hello 일때

world: wo로 시작하는 단어 일때

s: s 혹은 start 일때

start: s 혹은 start 일때

end: e 혹은 end 일때

etc: 기타

 

---------------------------------------------------------

# 참고 사이트

---------------------------------------------------------

https://blog.gaerae.com/2015/01/bash-hello-world.html

 

posted by 귀염둥이채원 2021. 4. 15. 06:23

 

---------------------------------------------------------

# if 기본 구문

---------------------------------------------------------

if [ Condition ]

then

< Statement >

fi

 

# 조건문

# AND

if [ ${string1} == ${string2} ] && [ ${string3} == ${string4} ]

..생략

 

# OR

if [ ${string1} == ${string2} ] || [ ${string3} == ${string4} ]

..생략

 

# 다중 조건

if [[ ${string1} == ${string2} || ${string3} == ${string4} ]] && [ ${string5} == ${string6} ]

..생략



---------------------------------------------------------

# Simple Bash if/else statement

---------------------------------------------------------

#!/bin/bash

printf "Enter a number: "

read number

 

if [ $number -lt 100 ]; then

"echo “Your entered number is less than 100"

fi

 

# output

$ bash if.sh

Enter a number: 10

“Your entered number is less than 100”

 

$ bash if.sh

Enter a number: 100

 

---------------------------------------------------------

# Simple Bash if/else statement

---------------------------------------------------------

#!/bin/bash

directory="./BashScripting"

 

# bash check if directory exists

if [ -d $directory ]; then

echo "Directory exists"

else

echo "Directory does not exists"

fi

 

# output

$ bash test.sh

Directory does not exists

 

---------------------------------------------------------

# if-elif-else

---------------------------------------------------------

#!/bin/bash/

printf "Enter a number from 1-20: "

read number

if [ $number -lt 10 ]; then

echo "Your entered number is less than 10"

elif [ $number -le 20 ]; then

echo "Your entered number is greater than 10"

else

echo "You entered number is not between 1-20"

fi

 

# output

$ test.sh

Enter a number from 1-20: 4

Your entered number is less than 10

 

$ bash test.sh

Enter a number from 1-20: 14

Your entered number is greater than 10

 

$ bash test.sh

Enter a number from 1-20: 30

You entered number is not between 1-20

 

---------------------------------------------------------

# Nested if/else

---------------------------------------------------------

#!/bin/bash

choice=4

 

# Print to stdout

echo "1. Bash"

echo "2. Scripting"

echo "3. Tutorial"

echo -n "Please choose a word [1,2 or 3]? "

 

# Loop while the variable choice is equal 4

# bash while loop

while [ $choice -eq 4 ]; do

# read user input

read choice

 

# bash nested if/else

if [ $choice -eq 1 ]; then

echo "You have chosen word: Bash"

else

if [ $choice -eq 2 ]; then

echo "You have chosen word: Scripting"

else

if [ $choice -eq 3 ]; then

echo "You have chosen word: Tutorial"

else

echo "Please make a choice between 1-3 !"

echo "1. Bash"

echo "2. Scripting"

echo "3. Tutorial"

echo -n "Please choose a word [1,2 or 3]? "

choice=4

fi

fi

fi

done

 

# output1

$ bash test.sh

1. Bash

2. Scripting

3. Tutorial

Please choose a word [1,2 or 3]? 1

You have chosen word: Bash

 

# output2

$ bash test.sh

1. Bash

2. Scripting

3. Tutorial

Please choose a word [1,2 or 3]? 2

You have chosen word: Scripting

 

# output3

$ bash test.sh

1. Bash

2. Scripting

3. Tutorial

Please choose a word [1,2 or 3]? 3

You have chosen word: Tutorial

 

---------------------------------------------------------

# OR 조건 (||)

---------------------------------------------------------

#!/bin/bash/

printf "Enter a number: "

read number

if [ $number -le 10 ] || [ $number -le 20 ]; then

echo "You have entered the correct number"

else

echo "Your entered the incorrect number"

fi

 

# output

$ bash test.sh

Enter a number: 5

You have entered the correct number

 

$ bash test.sh

Enter a number: 13

You have entered the correct number

 

$ bash test.sh

Enter a number: 70

Your entered the incorrect number

 

---------------------------------------------------------

# Mathematical Operators

---------------------------------------------------------

#!/bin/bash

 

if [ $1 -gt 0 ]; then

echo "$1 > 0"

fi

 

if [ $1 -ge 0 ]; then

echo "$1 >= 0"

fi

 

if [ $1 -eq 0 ]; then

echo "$1 == 0"

fi

 

if [ $1 -ne 0 ]; then

echo "$1 != 0"

fi

 

if [ $1 -lt 0 ]; then

echo "$1 < 0"

fi

 

if [ $1 -le 0 ]; then

echo "$1 <= 0"

fi

 

# output

$ bash test.sh -3

-3 != 0

-3 < 0

-3 <= 0

 

$ bash test.sh 5

5 > 0

5 >= 0

5 != 0

 

$ bash test.sh 0

0 >= 0

0 == 0

0 <= 0

 

---------------------------------------------------------

# String Operators

---------------------------------------------------------

#!/bin/bash

string_null=""

string1="string1"

 

if [ $string_null -n ]; then

echo "not null string"

else

echo "null string"

fi

 

if [ $string_null -z ]; then

echo "null string"

else

echo "not null string"

fi

 

if [ "$string_null" == "$string1" ]; then

echo "strings equal"

else

echo "strings not equal"

fi

 

if [ "$string_null" != "$string1" ]; then

echo "strings not equal"

else

echo "strings equal"

fi

 

# output

$ bast test.sh

not null string

null string

strings not equal

strings not equal

 

---------------------------------------------------------

# Test for files and directories

---------------------------------------------------------

#!/bin/bash

 

if [ -s $1 ]; then

echo "$1 not empty file"

fi

 

if [ -f $1 ]; then

echo "$1 normal file. Not a directory"

fi

 

if [ -e $1 ]; then

echo "$1 exists"

fi

 

if [ -d $1 ]; then

echo "$1 is directory and not a file"

fi

 

if [ -r $1 ]; then

echo "$1 is read-only file"

fi

 

if [ -x $1 ]; then

echo "$1 is executable"

fi

 

# output

$ bash test.sh test.sh

test.sh not empty file

test.sh normal file. Not a directory

test.sh exists

 

---------------------------------------------------------

# if…else…fi

---------------------------------------------------------

#!/bin/sh

if [ $# -eq 0 ]; then

echo "$0 : You must give/supply one integers"

exit 1

fi

 

if [ $1 -gt 0 ]; then

echo "$1 number is positive"

else

echo "$1 number is negative"

fi

 

# output

$ bash test.sh

test.sh : You must give/supply one integers

 

$ bash test.sh 1

1 number is positive

 

$ bash test.sh -1

-1 number is negative

 

$ bash test.sh 100 100 100

100 number is positive

 

---------------------------------------------------------

# 참고사이트

---------------------------------------------------------

https://devhints.io/bash

http://programmingexamples.wikidot.com/bash-scripting

https://blog.gaerae.com/2015/01/bash-hello-world.html

'쉘스크립트' 카테고리의 다른 글

[Shell Script] 반복문(for)  (0) 2021.04.15
[Shell Script] 조건문(case)  (0) 2021.04.15
[Shell Script] read를 이용한 입력값 받기  (0) 2021.04.15
[Shell Script] 배열(Array)  (0) 2021.04.15
[Shell Script] 인자(Argument)  (0) 2021.04.15
posted by 귀염둥이채원 2021. 4. 15. 05:57

---------------------------------------------------------

# 간단한 읽기 명령 예시

---------------------------------------------------------

#!/bin/bash

# echo -n 옵션은 줄바꿈을 하지 않는다.

echo -n "What is your favorite food : "

read answer

echo "Oh! you like $answer!"

 

# echo는 줄바꿈 한다.

echo "What is your favorite food : "

read answer

echo "Oh! you like $answer!"

 

# printf는 줄바꿈을 하지 않는다.

printf "What is your favorite food : "

read answer

echo "Oh! you like $answer!"

 

# printf에서 "\n"을 사용하여 줄바꿈한다.

printf "What is your favorite food : \n"

read answer

echo "Oh! you like $answer!"

 

# output

$ bash test.sh

What is your favorite food : pizza

Oh! you like pizza!

What is your favorite food :

piaaz

Oh! you like piaaz!

What is your favorite food : pizza

Oh! you like pizza!

What is your favorite food :

pizza

Oh! you like pizza!

 

---------------------------------------------------------

# 옵션과 함께 읽기 명령 사용

---------------------------------------------------------

* -p 옵션은 입력과 관련된 사용자에게 유용한 메시지를 표시

* -s 옵션은 사용자가 입력 할 터미널에서 텍스트를 숨기는 데 사용

 

#!/bin/bash

# Type your Login Information

read -p 'Username: ' user

read -sp 'Password: ' pass

 

if (($user == "admin" && $pass == "12345")); then

echo -e "\nSuccessful login"

else

echo -e "\nUnsuccessful login"

fi

 

# output

$ bash test.sh

Username: admin

Password:

Successful login

 

---------------------------------------------------------

# 읽기 명령을 사용하여 여러 입력 가져 오기

---------------------------------------------------------

#!/bin/bash

# Taking multiple inputs

echo -n "Type four names of your favorite programming languages: "

read lan1 lan2 lan3 lan4

echo "$lan1 is your first choice"

echo "$lan2 is your second choice"

echo "$lan3 is your third choice"

echo "$lan4 is your fourth choice"

 

# output

$ bash test.sh

Type four names of your favorite programming languages: python java go javascript

python is your first choice

java is your second choice

go is your third choice

javascript is your fourth choice

 

---------------------------------------------------------

# 시간 제한이있는 읽기 명령 사용

---------------------------------------------------------

* -t옵션을 사용한 사용자에 대한 시간 제한 설정 (초단위)

* 사용자가 5 초 내에 데이터를 입력 할 수 없으면 프로그램은 값없이 종료

#!/bin/bash

read -t 5 -p "Type your favorite color : " color

echo $color

 

# output

$ bash test.sh

Type your favorite color : white

white

 

---------------------------------------------------------

# 간단한 메뉴를 생성하고 선택된 메뉴 항목 에 따라 조치를 취하는 스크립트

---------------------------------------------------------

#!/bin/bash

while :; do

clear

echo "-------------------------------------"

echo " Main Menu "

echo "-------------------------------------"

echo "[1] Show Todays date/time"

echo "[2] Show files in current directory"

echo "[3] Show calendar"

echo "[4] Start editor to write letters"

echo "[5] Exit/Stop"

echo "======================="

echo -n "Enter your menu choice [1-5]: "

read yourch

case $yourch in

1)

echo "Today is $(date) , press a key. . ."

read

;;

2)

echo "Files in $(pwd)"

ls -l

echo "Press a key. . ."

read

;;

3)

cal

echo "Press a key. . ."

read

;;

4) vi ;;

5) exit 0 ;;

*)

echo "Opps!!! Please select choice 1,2,3,4, or 5"

echo "Press a key. . ."

read

;;

esac

done

 

# output

$ bash test.sh

-------------------------------------

Main Menu

-------------------------------------

[1] Show Todays date/time

[2] Show files in current directory

[3] Show calendar

[4] Start editor to write letters

[5] Exit/Stop

=======================

Enter your menu choice [1-5]: 1

Today is 2021년 4월 10일 토요일 03시 58분 48초 KST , press a key. . .

 

---------------------------------------------------------

# 참고 사이트

---------------------------------------------------------

https://linuxhint.com/bash-script-user-input/

'쉘스크립트' 카테고리의 다른 글

[Shell Script] 조건문(case)  (0) 2021.04.15
[Shell Script] 조건문(if-elif-else)  (0) 2021.04.15
[Shell Script] 배열(Array)  (0) 2021.04.15
[Shell Script] 인자(Argument)  (0) 2021.04.15
[Shell Script] 변수(Variable)  (0) 2021.04.15