반응형

사전지식

아래와 같이 $( )로 구문을 묶어주면 명령어 실행 결과를 변수에 저장할 수 있습니다.

#!/bin/bash

COMMAND=$(ls -al)

echo $COMMAND

결과

takin@DESKTOP-QMGL8TS:~/practice/00_shell_study$ ./command.sh
total 16 drwxr-xr-x 2 takin takin 4096 Feb 22 01:02 . drwxr-xr-x 4 takin takin 4096 Jan 30 01:06 .. -rwxr-xr-x 1 takin takin 46 Feb 22 01:02 command.sh -rwxr-xr-x 1 takin takin 411 Feb 22 01:01 cut_1.sh

 

예제코드

 #!/bin/bash

 b="car/avante/tire"

 check="origin: ${b}";
 echo ${check}

 test1=$(echo ${b} | cut -d '/' -f1-)
 echo test1: $test1
 #결과: "test1: car/avante/tire"

 test2=$(echo ${b} | cut -d '/' -f2-)
 echo test2: $test2
 #결과: "test2: avante/tire" 

 test3=$(echo ${b} | cut -d '/' -f3-)
 echo test3: $test3
 #결과: "test3: tire"

 test4=$(echo ${b} | cut -d '/' -f1)
 echo test4: $test4
 #결과: "test4: car"

 test5=$(echo ${b} | cut -d '/' -f2)
 echo test5: $test5
 #결과: "test5: avante"

 test6=$(echo ${b} | cut -d '/' -f3)
 echo test6: $test6
 #결과: "test6: tire"

 

결과

origin: car/avante/tire
test1: car/avante/tire
test2: avante/tire
test3: tire
test4: car
test5: avante
test6: tire

 

 

 

참고자료

https://www.lesstif.com/lpt/linux-cut-93127007.html

반응형