1) 함수 function 이용하기

 

#!/bin/bash
function sum_x_y
{
echo "sum_$1_${2}"
  for (( i=$1, sum=0 ; i<=$2; i++ ))
     do
        (( sum += i ))
     done
  echo "sum_x_y: sum: $sum"
  return $sum
}


echo
rst=$(sum_x_y 1 10)
echo "result of sum_1_10:  $rst"
echo
rst=$(sum_x_y 1 100)
echo "result of sum_1_100:  $rst"

 

=> $?은 1byte 255이상을 넘어갈 수 없다. 따라서 위와 같이 rst 변수에 저장해서 함수로 넘겨준다



 

 


 

 


2) Daily Backup 만들기

 

#!/bin/bash
# Daily File Backup example

echo
echo "======== File Daily Backup =============="
echo

CONF_FILE=backupfiles
DEST_DIR=./backup
DATE=$(date +%y%m%d)
FILE_NAME=$DATE.tar.gz
DEST_FILE=$DEST_DIR/$FILE_NAME

#Check if BACKUP_FILES exist
if [ -f $CONF_FILE ]
then
 echo $CONF_FILE exist!!!
 echo
else
 echo $CONF_FILE does not exist!!!
 exit 1
fi

#Check Backup Directory exist

if [ -f $DEST_DIR ]
then
 echo $DEST_DIR is regular file!!!
 exit 2
else
 if [ -e $DEST_DIR ] && [ -d $DEST_DIR ]
 then
  ls -ld $DEST_DIR
 else
  mkdir $DEST_DIR
  ls -ld $DEST_DIR
 fi
fi

#Make backup file name list

file_no=1
exec < $CONF_FILE
read filename
echo
echo "======== $filename ============"

while [ $? -eq 0 ]
do
 if [ -f $filename ] || [ -d $filename ]
 then
  filelist="$filelist $filename"
 else
  echo $filename does not exist!!!
  echo "continue to build backup list"
 fi
 fine_no=$[ $file_no + 1 ]
 read filename
done
echo
echo "Backup file list : $filelist"
echo

#Makeup backup file and compress it using tar
echo "------ Start archiving backup files ---------"
echo
tar -czf $DEST_FILE $filelist 2> /dev/null

echo "Archiving completed!!!"
echo
echo "Resulting Archiving file : $DEST_FILE"
echo

ls -l $DEST_FILE


 


 

 


 

 


주의 : 쉘은 특별한 경우가 아니라면 항상 서브쉘에서 돌려야 한다...

기존 환경변수를 건드리면 안되기 때문에...

sh test.sh

source test.sh

. test.sh

는 되도록이면 쓰지 않는다!!

 

 


 

 


 

+ Recent posts