1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
sshd_config 수정
sudo vi /etc/ssh/sshd_config
 
# To enable empty passwords, change to yes (NOT RECOMMENDED)
PermitEmptyPasswords no
 
# Change to yes to enable challenge-response passwords (beware issues with
# some PAM modules and threads)
ChallengeResponseAuthentication no
 
# Change to no to disable tunnelled clear text passwords
PasswordAuthentication yes
 
 
 
SSH 서비스 재시작
sudo service ssh restart
OR
sudo service sshd restart
 
 
cs






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

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

 

 


 

 


 

Shell에서 쌍따옴표(")나 따옴표(') 는 동일하다. String으로 인식한다.

 

1) 특정 Directory의 실행 파일만 모두 출력:

 

#!/bin/bash

for file in /home/aiadm/lab/chat01/*
    do
       if [ -d $file ]
            then
                echo $file is directory
       elif [ -f  $file ]
            then
                echo $file is regular file
       else
            echo nothing
       fi
    done

 


 

 


 

 


 

2) File Path를 입력받고 실행가능한 파일 리스트 출력

 

#!/bin/bash

read -p "input directory : " dir

for file in $dir/*
  do
     if [ -x $file ]
        then
        echo $file is executable
        echo $file >> execfiles
     else
        echo nothing
     fi
 done


 

 


 

 


 

3) For Loop을 돌려서 Sum을 출력

 

#!/bin/bash

for (( i=1, sum=0; i<11; i++))
    do
       echo "loop_$i is running"
       sum=$[$sum+$i] #아님 $(($sum+ $i))
    done
echo $sum


 

 


 

 


4) 반복문으로 구구단 

 

#!/bin/bash

read -p "Input num1 : "  num1
read -p "input num2 : "  num2


for (( i=1; i<=num1; i++ ))
   do
     for (( j=1; j<=num2; j++))
        do
           echo "$i단  i:$i *  j:$j = $(($i*$j))"
        done
        echo
    done

 

1) 디렉터리 또는 파일 유형 확인 :

#!/bin/bash

read -p "Input file name : " fname

if [ -e $fname ] #fname 유무여부를 확인한다.

    then
        echo $fname exists
fi

if [ -d $fname ]
    then
        echo $fname is directory
        cd $fname #fname이 디렉터리일 경우 해당 디렉터리로 이동한다.
        pwd
fi

if [ -f $fname ] #fname이 파일인지 여부를 확인한다.

    then
        echo $name is regular file
fi

 


 

 


 

 


2) 파일 중 오래되거나 더 최근 파일을 찾는 프로그램 

 

#!/bin/bash

read -p "Input two file : " fname1 fname2

if [ $fname1 -nt $fname2 ]
    then
        echo $fname1 is newer then $fname2
else
    echo $fname1 is older than $fname2
fi

 

 


 

 


3) Password Digit Count 제한 주기 :

 

Digit Count를 할 때는 ${#password} 와 같이 입력한다.

 

#!/bin/bash

read -p "ID: " id
#read -s -p "Password : " password
prompt="Password : "
while read -p "$prompt" -r -s -n 1 char
 do
  if [[ $char == $'\0' ]]
     then break;
  fi
    prompt='*'
    password+=$char
 done
echo
if [ ${#password} -lt 8 ]
   then
        echo your password must be gretater than 8 digits
        exit 0
fi
echo
echo Your id: $id, Password: $password

 


 

 


 

 


 


*서브쉘 실행

./test.sh

 

*쉘 자체 실행

. test.sh

source test.sh

 

시스템 변수와 로컬변수가 충돌 할 경우 서브쉘로 돌려주는 것이 좋다.

 

예를 들어...아래와 같은 shell파일이 있다고 하면...

 

test.sh

 

#!/bin/bash

HOME=/home/aiadm
echo $HOME

 

test.sh을 실행하기 전에 echo $HOME 입력했을 때

/home/test 였을 경우...

 

./test.sh(서브쉘로 실행)를 실행한 후에 $HOME 환경변수는 바뀌지 않는다.

 

. test.sh(쉘 자체를 실행)를 실행한 후에 $HOME 환경변수는 변경되며...환경변수가 변경될 수 있기 때문에 위험하다.

 


 

 


 

 


 

1) If문 사용하기 :


 

#!/bin/bash

if pwd
    then
        echo pwd command exists
fi

if mypwd
    then
        echo mypwd exists
fi

 

if cat /etc/passwd
    then echo cat exists
fi

if...then...fi

 

 

2) if else문 사용하기

 

#!/bin/bash

read -p "Input User Name : " name

if grep $name /etc/passwd #입력받은 name값이 /etc/passwd 밑에 있으면
        then echo $name exists!!
        else echo $name does not exist
fi

exit 0

 

if...then....else..fi


 

 


 

 


3) if 조건문을 따로 분리할 경우 :

 

#!/bin/bash

read -p "Input User Name : " name
grep $name /etc/passwd
if (($? == 0 ))  # 이전 조건에 에러가 없을 경우

        then echo $name exists!!
        else echo $name does not exist
fi

exit 0

 

4) test 명령어 사용하기 :

 

#!/bin/bash

value="Apple"

if test $value
        then echo Value : $value
else
        echo value is empty
fi

exit 0

 


 

 


 

 


5) 숫자 비교 테스트


n1 -eq n2

=> n1과 n2가 같은지 검사함

n1 -ne n2

=> n1과 n2가 같지 않은지 검사함

n1 -ge n2

=> n1이 n2보다 크거나 같은지 검사함
n1 -gt n2

=> n1이 n2보다 큰지 검사함

n1 -le n2

=> n1이 n2보다 작거나 같은지 검사함

n1 -lt n2

=> n1이 n2보다 작은지 검사함

 

1) 2자리 이상 숫자 쉘 연산

 

#!/bin/bash

add=$[$1 + $2]
sub=$[$1 - $2]
mul=$[$1 * $2]
div=$[$1 / $2]

rst=$[${10} + ${11}]

echo add : $add sub : $sub mul : $mul div : $div
echo
echo rst : $rst

>> . test.sh 10 20 30 40 50 60 70 80 90 100 110

=> {}로 닫아주어야지 그러지 않을 경우 1자리로 인식한다.


 

 


 

 


 

2) 인자를 interactive 하게 받기

 

#!/bin/bash
echo -n "Input your name : "
read name

 

echo -n "Input your age : "
read age

 

echo NAME : $name AGE :  $age

 

 


 

 


 

3) 인자로 Password(입력을 안보이게 받기)를 받기 :

 

#!/bin/bash

read -p "ID: " id
read -s -p "Password : " password

echo
echo Your id: $id, Password: $password

 

 

 

 


 

 


 

 


4) password 입력 시 *로 찍히게 하기 :

 

#!/bin/bash

read -p "ID: " id
#read -s -p "Password : " password


prompt="Password : "

while read -p "$prompt" -r -s -n 1 char
 do
  if [[ $char == $'\0' ]]
     then break;
  fi
    prompt='*'
    password+=$char
 done

echo
echo Your id: $id, Password: $password

 

+ Recent posts