setting, git, shell etc

shell script 기초.

qkqhxla1 2017. 6. 13. 17:35

https://www.shellscript.sh


변수 선언과 변수 사용, 변수와 문자열 concat등. 

#!/bin/sh
echo Hello * World
var="first val...."
echo $var
echo "$var"!!!

변수 선언과 사용. ''안에서는 $로 출력해도 문자열 그대로 나온다. 변수 사용시 ""로 감싸서 사용할것. 변수와 문자열을 연결해서 파일 만들기 등. 변수 선언은 ${변수이름} 으로 사용하는걸 습관들이자.

#!/bin/sh
echo 'What is your name?'
read myname
echo 'myname is $myname'
echo "myname is $myname"
touch "${myname}_file"

*의 의미와 반복문 예제

#!/bin/sh
echo "*"
echo *
for i in 1 2 3 4 5
do
    echo "looping $i"
done

for i in hello 1 * 2 goodbye
do
    echo "Looping ... i is set to $i"
done

간단하게 쉘 상태에서 반복문 쓰기 예제.

mkdir rc{0,1,2}_file를 실행하면 rc0_file ~ rc2)_file 이 만들어짐을 확인할수 있다.


if 문.

#!/bin/sh
read input
if [ $input == 'go' ]
then
    echo "hello!"
fi

까다롭다. if의 []안에 조건식이 들어가는데 조건식 앞뒤로 스페이스가 있어야 한다. 없으면 에러뜬다. 

[] 를 테스트라고 하며 [는 실제 프로그램 명령어라고 한다. 그러니까 [를 실행시키려면 [ 뒤에 스페이스가 하나 있어서 구분을 해야되는것 같다. $foo라는 변수를 출력할때 $fooss처럼 써있으면 $foo는 인식이 안되고 $fooss라는 변수로 인식되는것과 같다고 생각한다.

else if, else 는 elif, else를 쓰며 아래와 같은 포맷을 가진다.

if  [ something ]; then
 echo "Something"
 elif [ something_else ]; then
   echo "Something else"
 else
   echo "None of the above"
fi

이거말고도 if (( $input > 0 )) 처럼 쓸수있는데 이게 더 편한것같다. 이걸로 되도록 쓰는걸로.


파라미터 관련.

#!/bin/sh
echo "parameter counted = $#"
echo "$0 $1"
echo "all parameters = $@“

함수. 인자전달도 프로그램을 실행시키듯 한다. 그리고 변수에 값을 할당할때 name = $1처럼 = 사이사이에 스페이스가 있으면 에러난다. 

#!/bin/sh
say_hello()
{
    name=$1
    echo "hello $name"
}
say_hello go

try catch문.

#!/bin/bash
{
    echs s;
} ||
{
    echo "catch!";
}

{}안에 아무 명령어라도 들어가야지 그냥 비워놓으면 에러가 뜬다.


리스트를 만들고 리스트를 반복

#!/bin/bash
declare -a list_example=("rara" "haha" "asdfasfwff" "234234")
for ip in "${list_example[@]}"
do
    echo "$ip"
done

그리고 써먹기 좋은 레퍼런스들.

Command 

DescriptionExample
&Run the previous command in the backgroundls &
&&Logical ANDif [ "$foo" -ge "0" ] && [ "$foo" -le "9"]
||Logical ORif [ "$foo" -lt "0" ] || [ "$foo" -gt "9" ] (not in Bourne shell)
^Start of linegrep "^foo"
$End of linegrep "foo$"
=String equality (cf. -eq)if [ "$foo" = "bar" ]
!Logical NOTif [ "$foo" != "bar" ]
$$PID of current shellecho "my PID = $$"
$!PID of last background commandls & echo "PID of ls = $!"
$?exit status of last commandls ; echo "ls returned code $?"
$0Name of current command (as called)echo "I am $0"
$1Name of current command's first parameterecho "My first argument is $1"
$9Name of current command's ninth parameterecho "My ninth argument is $9"
$@All of current command's parameters (preserving whitespace and quoting)echo "My arguments are $@"
$*All of current command's parameters (not preserving whitespace and quoting)echo "My arguments are $*"
-eqNumeric Equalityif [ "$foo" -eq "9" ]
-neNumeric Inqualityif [ "$foo" -ne "9" ]
-ltLess Thanif [ "$foo" -lt "9" ]
-leLess Than or Equalif [ "$foo" -le "9" ]
-gtGreater Thanif [ "$foo" -gt "9" ]
-geGreater Than or Equalif [ "$foo" -ge "9" ]
-zString is zero lengthif [ -z "$foo" ]
-nString is not zero lengthif [ -n "$foo" ]
-ntNewer Thanif [ "$file1" -nt "$file2" ]
-dIs a Directoryif [ -d /bin ]
-fIs a Fileif [ -f /bin/ls ]
-rIs a readable fileif [ -r /bin/ls ]
-wIs a writable fileif [ -w /bin/ls ]
-xIs an executable fileif [ -x /bin/ls ]


'setting, git, shell etc' 카테고리의 다른 글

intellij es lint 끄기, indent 4로 설정되어있는데 2로 적용되는경우.  (0) 2018.09.11
expect  (0) 2017.07.14
crontab  (0) 2017.05.12
intellij 뚝뚝 끊김.  (0) 2017.03.16
gradle 관련.  (0) 2017.02.19