bash 언어 기초

21647 단어 linux
내장 변수 와 특수 기호
$0,$1/$2..$0$1$2...  N     
$#
	             
"$*"
	                 ("$1$2…")
"$@"
	              ("$1""$2")
$-
	  shell       , set      
$?
	            ,      ,    0
$$
	       ID
$!
	            ID

1、   #
	  #!/bin/bash  #  
2、   $    。
	         ,             , $a    a  。
	        1  , {}   
3、   
	             ,     
	            ,          ,         。
	echo ‘my $SHELL’
4、   
	      , $    \    `        //`
	         ,          。
5、   (  1       )
	         shell       ,
	                  。
6.         
	           。
	            (\)    shell             。

..		     , cd         
.		       
*		       
?		          
[..]	        
\m            , *、? 
[a-z]  	*            
\		    ,             
~		    
;		   ,          ,    
$		BourneShell    ,     shell         
#		    
|&		          
>                   
<>  
>>     	                   
{..}             


수치 처리
num1 –eq num2   num1  num2,      
num1 –gt num2   num1  num2,      
num1 –lt  num2   num1  num2,      
num1 –ge num2   num1     num2,      
num1 –le num2   num1     num2,      
num1 –ne num2   num1   num2,      

expr  
r=`expr 4 + 5`
r=`expr 40 -5` 
r=`expr 4 \* 5` 	//  \*     *
r=`expr 40 / 5` 
r=`expr 100 % 43`
 :expr     

$(())  
r=$(( 4 + 5 ))
r=$(( 40 -5 ))
r=$(( 4 * 5 ))
r=$(( 40 / 5 ))
r=$((100 % 43))
r=$(( 2 ** 3 ))

$[]  
r=$[ 4 + 5 ]
r=$[ 40 -5 ]
r=$[ 4 * 5 ]
r=$[ 40 / 5 ]
r=$[100 % 43]
r=$[ 2 ** 3 ]

let  
let "r = 4+5" 
let "r = 40 -5"
let "r = 4 * 5"
let "r = 40 / 5"
let "r = 100 % 43"
let "r = 2**3"
 :     ,=       

문자열
문자열 상수
[:alpha:]
	                 
	'[0-9]{3}[[:alpha:]]{2}'[:alnum:]'[[:alnum:]]+'                  
[:digit:]
	          
	'[[:digit:]-]+'                  
[:lower:]'[[:lower:]]'   A,  A    
[:space:]
	      (' ')
	[[:space:]]    
[:upper:]
	              
	'[[:upper:]ab]'         :      、a  b。
[:whitespace:]
	          、   、       
[:ascii:]
	       ASCII            、   、   
[:blank:]
	             
	[[:blank:]]   [ \t]

문자열 일치 (포 지 셔 닝, 판단, 교체)
\b        ,           。
	 :“er\b”    “never”  “er”,     “verb”  “er”。
	echo "never verb" |xargs -n1 |grep "er\b"
^        
	 :   abc    :
	echo –e “abc
xyz”|grep –E ^abc $ : xyz echo –e “abc
xyz”|grep –E xyz$ * 0 + 1 ? 0 1 {n} N {n,} N {n,m} n m [] , () ( ) *? , +? 1 , ?? 0 1 , email ^[a-zA-Z0-9_]+@[a-zA-Z0-9_]+\.[a-zA-Z0-9_]+$ : ^[a-zA-Z0-9_]+ @ [a-zA-Z0-9_]+ \. [a-zA-Z0-9_]+$ QQ :[1-9][0-9]{4,} . \s ——[ \f
\r\t\v] \S ——[^ \f
\r\t\v] \w 、 、 ——[A-Za-z0-9_] \W 、 、 ——[^A-Za-z0-9_] \d 0-9——[0-9] \D ——[^0-9] -n string string -z string string String1=string2 string1 string2 String1!=string2 string1 string2 : , , ; ${#string} $string ${string:position} $string$position ${string:position:length} $string$position $length ${string#substring} $string$substring ${string##substring} $string$substring ${string%substring} $string$substring ${string%%substring} $string$substring ${string/old/new} $new, $old ${string//old/new} $new, $old ${string/#old/new}$string $old$new ${string/%old/new}$string $old$new

복합 조건
! expression 
	  expression  ,       。!          
expression1 –a expression2   
	  expression1 expression2    ,       
expression1 –o expression2   
	  expression1 expression2     ,       

구조 화 제어:
    :
	if  : if then else      
	Case  :      、    
    :
	for:            ,      。
	until:       ,      。
	while:       。
	continue:         。
	break:    ,           。
  1:
if[    ]
then
	  
fi

  2:
if[   ] ; then   
fi

  3:
if  [   ]
then
	  1
else
	  2
fi

  4:
if [   1 ]
then
	  1
elif[   2 ]
then  
	  2
else 
	  3
fi

case  :
case   in
	  1)
		  1
		;;
	  2)
	…
	*)
	  n
	;;
esac
//         in,             。          。
//            ,      ,            ;;
//*****                    。
//        ,  *     。

for  1
for     in   
do
	  
done

for  2:
for ((  =   ;    ;    ))
do
	  
done

  :
s=0
for ((i=1;i<=100;i++))
do
	s=$((s+i))
done
echo $s

while  :
while   
do
	  
done
//while             ,             。
//          ,  while do         。
//          ,
//           0 ,do done        ,
//        0,     。

until  
until   
do
	  
done
until        ,         。

break continue   for、while、until       
break       done     ,      。
continue        done  ,         。

입 출력
Linux   ,     3      ,   :
    :standard input 0。
	       ,        。
    :standard output 1。
	         。
    :error output 2。
	          。
	
                。
     :        ,          。
     :       ,            。

  	/dev/stdin0		    
   	/dev/stdout1	    
   	/dev/stderr2	      

“<”:        ,                ,        ,           。
“>>”:                  ,     。
“>”:        。

           
read a < 1.txt
echo $a

       	  >  	    
				  >>  	    
         
	    2>  	    
	    2>>  	    

                 
  >   2>&1	    
  >>   2>&1	    
   &>  		    
   &>>  		    
  >>  1 2>>  2

&>/dev/null
//                /dev/null 
//             .

cat filename >/dev/null
rm filename1 2>/dev/null
rm filename1 >/dev/null 2>&1
cat /dev/null > filename 
ls –l >/dev/null 2>&1   ===    &>/dev/null

함수.
        :1.   ; 2.   
                :
	     ,     shell     。
	                    ,        shell,               。
	                  ,                。

           ,    ,shell       。
             ,              。
                  ,                。

   ()
{
. . .
}

function    ()
{}

좋은 웹페이지 즐겨찾기