'전체 글'에 해당되는 글 120건

  1. 2021.04.15 [Shell Script] 함수(function)
  2. 2021.04.15 [Shell Script] 반복문(while)
  3. 2021.04.15 [Shell Script] 반복문(for)
posted by 귀염둥이채원 2021. 4. 15. 12:38

 

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

# 함수(Function)

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

* function는 생략해도 된다.

* 함수명을 쓰면 함수가 호출이 된다.

* 호출 코드가 함수 코드보다 반드시 뒤에 있어야 된다.

- 함수 코드 보다 앞에서 호출 오류가 발생한다.

 

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

# 함수 예시

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

#!/bin/bash

string_test1() {

echo "string test1"

}

 

function string_test2() {

echo "string test2"

echo -n "인자값: ${@}"

}

 

string_test1

string_test2

 

# 함수에 인자값 전달하기(공백으로 띄워서 2개의 인자값을 넘김)

string_test2 "hello" "world"

 

# output

$ bash test.sh

string test1

string test2

인자값: string test2

인자값: hello world

 

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

# example

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

#!/bin/bash

# BASH FUNCTIONS CAN BE DECLARED IN ANY ORDER

function function_B {

echo Function B.

}

function function_A {

echo $1

}

function function_D {

echo Function D.

}

function function_C {

echo $1

}

# FUNCTION CALLS

# Pass parameter to function A

function_A "Function A."

function_B

# Pass parameter to function C

function_C "Function C."

function_D

 

# output

$ bash test.sh

Function A.

Function B.

Function C.

Function D.

 

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

# 문자열을 반환하는 방법 - 전역변수 사용

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

* 전역 변수를 사용하여 문자열 값을 반환

* 함수를 호출 한 후 전역 변수의 값이 변경



#!/bin/bash

function F1() {

retval='I like programming'

}

 

retval='I hate programming'

echo $retval

F1

echo $retval

 

# output

$ bash test.sh

I hate programming

I like programming

 

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

# 문자열을 반환하는 방법 - 함수 명령 사용

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

#!/bin/bash

function F2() {

local retval='Using BASH Function'

echo "$retval"

}

 

getval=$(F2)

echo $getval

 

# output

$ bash test.sh

Using BASH Function

 

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

# 문자열을 반환하는 방법 - 변수 사용

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

#!/bin/bash

function F3() {

local arg1=$1

 

if [[ $arg1 != "" ]]; then

retval="BASH function with variable"

else

echo "No Argument"

fi

}

 

getval1="Bash Function"

F3 $getval1

echo $retval

 

getval2=$(F3)

echo $getval2

 

# output

$ bash test.sh hello

BASH function with variable

No Argument

 

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

# 문자열을 반환하는 방법 - Return 문 사용

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

#!/bin/bash

function F4() {

echo 'Bash Return Statement'

return 35

}

 

F4

echo "Return value of the function is $?"

 

# output

$ bash test.sh hello

Bash Return Statement

Return value of the function is 35

 

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

# 참고사이트

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

https://devhints.io/bash

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

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

 

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

# Syntax of while loop:

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

while [ condition ]; do

commands

done

 

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

# 고정 횟수만큼 반복

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

#!/bin/bash

n=1

while [ $n -le 5 ]; do

echo "Running $n time"

((n++))

done

 

# output

$ bash test.sh

Running 1 time

Running 2 time

Running 3 time

Running 4 time

Running 5 time

 

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

# while에서 break 사용

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

#!/bin/bash

n=1

while [ $n -le 10 ]; do

if [ $n == 6 ]; then

echo "terminated"

break

fi

echo "Position: $n"

((n++))

done

 

# output

$ bash test.sh

Position: 1

Position: 2

Position: 3

Position: 4

Position: 5

terminated

 

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

# while에서 continue 사용

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

#!/bin/bash

n=0

while [ $n -le 5 ]; do

((n++))

 

if [ $n == 3 ]; then

continue

fi

echo "Position: $n"

 

done

 

# output

$ bash test.sh

Position: 1

Position: 2

Position: 4

Position: 5

Position: 6

 

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

# 무한루프

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

#!/bin/bash

n=1

while :; do

printf "The current value of n=$n\n"

if [ $n == 3 ]; then

echo "good"

elif [ $n == 5 ]; then

echo "bad"

elif [ $n == 7 ]; then

echo "ugly"

elif [ $n == 10 ]; then

exit 0

fi

((n++))

done

 

# output

$ bash test.sh

The current value of n=1

The current value of n=2

The current value of n=3

good

The current value of n=4

The current value of n=5

bad

The current value of n=6

The current value of n=7

ugly

The current value of n=8

The current value of n=9

The current value of n=10

 

posted by 귀염둥이채원 2021. 4. 15. 09:43

 

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

# Syntax of for loop:

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

for variable_name in lists; do

commands

done

 

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

# 정적 값 읽기

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

#!/bin/bash

 

for color in Blue Green Pink White Red; do

echo "Color = $color"

done

 

# output

$ bash test.sh

Color = Blue

Color = Green

Color = Pink

Color = White

Color = Red

 

for i in 1 2 3 4 5; do

echo "Welcome $i times"

done

 

# output

$ bash test.sh

Welcome 1 times

Welcome 2 times

Welcome 3 times

Welcome 4 times

Welcome 5 times

 

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

# 배열 변수 읽기

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

#!/bin/bash

ColorList=("Blue Green Pink White Red")

for color in $ColorList; do

if [ $color == 'Pink' ]; then

echo "My favorite color is $color"

fi

done

 

# output

$ bash test.sh

My favorite color is Pink

 

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

# 명령 줄 인수 읽기

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

#!/bin/bash

for myval in $*; do

echo "Argument: $myval"

done

 

# output

$ bash test.sh hello world shell

Argument: hello

Argument: world

Argument: shell

 

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

# 홀수 및 짝수 찾기

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

#!/bin/bash

for ((n = 1; n <= 5; n++)); do

if (($n % 2 == 0)); then

echo "$n is even"

else

echo "$n is odd"

fi

done

 

# output

$ bash test.sh

1 is odd

2 is even

3 is odd

4 is even

5 is odd

 

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

# 구구단 출력

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

#!/bin/sh

 

if [ $# -eq 0 ]; then

echo "Error - Number missing form command line argument"

echo "Syntax : $0 number"

echo "Use to print multiplication table for given number"

exit 1

fi

 

n=$1

 

for (( i = 0; i <= 9; i++ )); do

#for i in 1 2 3 4 5 6 7 8 9; do

echo "$n * $i = $(expr $i \* $n)"

done

 

# output

$ bash test.sh 3

3 * 0 = 0

3 * 1 = 3

3 * 2 = 6

3 * 3 = 9

3 * 4 = 12

3 * 5 = 15

3 * 6 = 18

3 * 7 = 21

3 * 8 = 24

3 * 9 = 27

 

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

# for문 range 설정

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

#!/bin/bash

for i in {1..10}; do

echo $i

done

 

# output

$ bash test.sh

1

2

3

4

5

6

7

8

9

10

 

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

# ls 결과 출력하기

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

#!/bin/bash

for i in $(ls); do

echo $i

done

 

# output

$ bash test.sh

01_tip.sh

02_print.sh

03_variable.sh