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