반응형

쉘프로그램중 많이쓰는 변수


1)일반변수

변수=값  선언

 ex) name=song


[root@system1 ~]# name=song

[root@system1 ~]# echo $name

song



2) $()   
$() =  command substitution  = 쉬운말로  명령어 결과값


ex1) 

name=song 변수로 선언하였을때

[root@system1 test]# echo $name              #  echo $name의 결과값은  song 

song                                                    


[root@system1 test]# touch $(echo $name)      

                                                           # touch 명령으로 파일을 만든다. 파일명은 echo $name결과값인 song

[root@system1 test]# ls -l

합계 16

drwx------. 2 root root 16384 12월 19 14:55 lost+found

-rw-r--r--. 1 root root     0 12월 23 15:51 song       #  $echo($name)   의 결과값인   song이 생성됨


ex2) 
date명령을 이용하여, 현재날짜의 파일 생성

[root@system1 test]# date +%y%m%d                 #date +%y%m%d 를 명령 실행결과, 년/월/일 결과가 출력됨
171223


[root@system1 test]# touch $(date +%y%m%d)     #   $(명령) 은  변수=명령결과  라고했으니,  
          date +%y%m%d명령결과   touch 명령결과(171223) 수행
[root@system1 test]# ls
171223  lost+found  song
[root@system1 test]# ll
합계 16
-rw-r--r--. 1 root root     0 12월 23 15:56 171223      #  touch $(date +%y%m%d)의 결과로 파일이 생성됨


3) ${ }

${} =  parameter substitution  = 쉬운말로  변수대체자

변수와 쓰임새는 똑같다, "변수${변수} + 상수" 형태를 출력하고자 주로 쓴다

[root@system1 test]# test=abcdef                

[root@system1 test]# echo $test                            #$test는   = abcdef  라는 변수다

abcdef


abcdefghijk   연달아 쓰고 싶다면


[root@system1 test]# echo $testghijk               #testghijk라는 변수가 없기때문에, 출력이 없다.
- 아무것도 출력하지 않음.     

${test}를 사용하면 결과는 달라진다


[root@system1 test]# echo ${test}ghijk                   

abcdefghijk


즉, test변수값 ( ${test} ) + 상수 ghijk가 합쳐딘 결과다


반응형

'프로그래밍 > 쉘프로그래밍' 카테고리의 다른 글

파일명 변환 쉘  (0) 2019.03.07
cut 명령 예제  (0) 2018.11.26
인자값이해, 인자값 쉘프로그램  (0) 2017.12.21

+ Recent posts