실행환경은 다음과 같습니다.
참고한 튜토리얼은 다음과 같고 일부 부분
은 현재 Bash 버전과 호환 되지 않습니다.
Learnshell ORG
- https://www.learnshell.org/아래 파일이 있습니다.
#!/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 ]
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"
현재 실행 운영체제에서는 ** 연산자
가 지원이 되지 않습니다.
TOTAL_TITLE = "this is a string"
echo ${#TARGET_TEXT} # 16
# TOTAL_TITLE 에서 TARGET 탐색
TOTAL_TITLE = "this is a string"
TARGET = "this"
expr index "$TOTAL_TITLE" "$TARGET" # 1
# 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
일반적인 프로그래밍 언어의 조건문
은 다음과 같이 구성되어 있습니다.
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
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
아래 연산자에서 주의사항은 다음과 같습니다.
=
근처에 공백이 필요합니다.쉘 확장
현상을 일으킵니다. 따라서 "" 와 같은 문자열 표시로 이를 감싸야 합니다.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
if [[ $VAR_A[0] -eq 1 && ( $VAR_B = "bee" || $VAR_T = "tee" ) ]]; then
command...
fi
case "$variables" in
"$condition1" )
command...
;;
"$condition2" )
command...
;;
esac
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
for arg in [list]
do
command(s)...
done
whiel [ condition ]
do
commands(s)...
done
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=(23 45 34 1 2 3)
echo $(array[2])
echo $(array[@])
echo $(#array[@])
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