Shell Script

공부의 기록·2022년 9월 21일
0

ShellScript

목록 보기
1/1

Basic

실행환경은 다음과 같습니다.

  • 작성일 : 2022-09-22
  • 운영체제 : ubuntu@20.04
  • Bash 버전 : GNU Bash version 5.0.17 (x86_64-pc-linux-gnu)

참고한 튜토리얼은 다음과 같고 일부 부분 은 현재 Bash 버전과 호환 되지 않습니다.

*.sh parameter

아래 파일이 있습니다.

#!/bin/bash

echo "File name is "$0	# 0 번째, 인자는 파일명.sh
echo $3					# 3 번째, 인자는 파일명 뒤에 붙은 3 번째 인자
echo $5					# 5 번째, 인자는 파일명 뒤에 붙은 5 번째 인자

InstanceVariables=$5

echo "Instance-Variables is $InstanceVariables."
echo "Parameter count is $#"		# 파일명 뒤에 붙은 인자의 수
echo "Parameter List in [ $@ ]"		# 파일명 뒤에 붙은 모든 인자 배열

아래 명령어를 입력하면,

sh my_shopping.sh A B C D E

아래의 결과가 출력됩니다.

File name is my_shopping.sh
C
E
Instance-Variables is E.
Parameter count is 5
Parameter List in [ A B C D E ]

Basic Operators

echo "100 + 200 result is " $((100 + 200))	# 300
echo "400 - 300 result is " $((400 - 300))	# 100
echo "10 * 30 result is " $((10 * 30))		# 300
echo "300 / 30 result is $((300 / 30))"		# 10
echo "322 / 30 result is $((322 % 30))"		# 22
echo "3 ** 3 result is $((3**3))"			# my_array2.sh: 11: arithmetic expression: expecting primary: "3**3"

현재 실행 운영체제에서는 ** 연산자 가 지원이 되지 않습니다.


Basic String Operations

String Length

TOTAL_TITLE = "this is a string"
echo ${#TARGET_TEXT}						# 16

Index

# TOTAL_TITLE 에서 TARGET 탐색
TOTAL_TITLE = "this is a string"
TARGET = "this"
expr index "$TOTAL_TITLE" "$TARGET"			# 1

Substring Extraction

# idx 1 ~ 3 까지
TOTAL_TITLE = "this is a string"
TARGET = "this"

echo ${TOTAL_TITLE:1:3}						# his

echo ${TOTAL_TITLE:1} 						# $STRING contents without leading character
echo ${TOTAL_TITLE:12}						# ring

Deision Making

일반적인 프로그래밍 언어의 조건문 은 다음과 같이 구성되어 있습니다.

if [ exrepssion ]; then
   	echo "If expression is true, echo hello"
elif [ expression2 ]; then
    echo "If expression2 is true, echo hi"
else
	echo "If expression is false, echo mayaho"
fi

Type of numeric comparisons

comparison		evaluated to true when
 $a -lt $b       $a < $b
 $a -le $b       $a <= $b
 
 $a -gt $b       $a > $b
 $a -ge $b       $a >= $b
 
 $a -eq $b       $a is equal to $b
 $a -ne $b       $a is not equal to $b

Type of string comparisons

아래 연산자에서 주의사항은 다음과 같습니다.

  • = 근처에 공백이 필요합니다.
  • shesll script 의 일부 특수문자는 쉘 확장 현상을 일으킵니다. 따라서 "" 와 같은 문자열 표시로 이를 감싸야 합니다.
comparison		evaluated to true when
 "$a" = "$b"	 $a is the same as $b
 "$a" == "$b"	 $a is the same as $b
 "$a" != "$b"	 $a is different from $b
 -z "$a"		 $a is empty
  • Logical combinations
if [[ $VAR_A[0] -eq 1 && ( $VAR_B = "bee" || $VAR_T = "tee" ) ]]; then
    command...
fi

Case structure

case "$variables" in
     "$condition1" )
          command...
     ;;
     "$condition2" )
          command...
     ;;
esac
  • Simple case bash structure
number_of_case = 3

case $number_of_case in
	1) echo "You selected bash";;
    2) echo "You selected perl";;
    3) echo "You selected python";;
    4) echo "You selected c++";;
    5) exit
esac

Loops

for

for arg in [list]
do
    command(s)...
done

while

whiel [ condition ]
do
	commands(s)...
done
  • break, contionue statements
COUNT=0
while [ $COUNT -ge 0 ]; do

  echo "Value of COUNT is: $COUNT"
  
  COUNT=$((COUNT+1))
  if [ $COUNT -ge 5 ] ; then
    break
    # continue
  fi
done

Array Comparison

array=(23 45 34 1 2 3)

echo $(array[2])
echo $(array[@])
echo $(#array[@])

Shell Functions

function function_A {
  echo "$1"
}
function function_B {
  echo "Function B."
}
function adder {
  echo "$(($1 + $2))"
}

function_A "Function A."     # Function A.
function_B                   # Function B.
adder 12 56                  
profile
2022년 12월 9일 부터 노션 페이지에서 작성을 이어가고 있습니다.

0개의 댓글