본문 바로가기

카테고리 없음

(linux/bash) 쉘 스크립트 for문, if문 정리

#!/bin/bash
#
#for i in 1 2 3 4 5
#do
#       echo "Welcome $i times"
#done
#
#for i in 1 2 3 4 5
#for i in {1..5}
#for i in {0..10..2}
#for i in $(seq 1 2 20)
#for i in {1..20..2}
#for (( i=1; i<=10; i += 2))
#do
#       echo "I want to love you $i times!"
#done
#
#for (( ; ; ))
#do
        #echo "infinite loops [ hit CTRL+C to stop ] "
#done
#
#
#for file in /etc/*
#do
#       if [ $file == "/etc/resolv.conf" ]
#       then
#               count=$(grep -c nameserver /etc/resolv.conf)
#               echo "Total $count nameservers defined in $file"
#               break
#       fi
#done
#
#
files="$@"

for f in $files
do
        if [ -f $f.bak ]
        then
                echo "Skipping $f file..."
                continue # read next file and skip cp command
        fi
        /bin/cp $f $f.bak
done

 

#!/bin/bash
#
#for a in `seq -w 10`
#do
#       mkdir TEST-$a
#done
#
#sum=0
#for a in $(seq 100)
#do
        #let sum+=a
#       ((sum += a))
#done
#echo $sum
#
#sum=0
#for a in `seq 3 3 100`
#do
#       let sum+=a
#done
#echo $sum
#
for a in `seq 100`
do
        echo "I want to drink cola"
done

 

#!/bin/bash
#

daemons=('httpd' 'mysqld' 'vsftpd')

echo ${daemons[0]}
echo ${daemons[1]}
echo ${daemons[2]}
echo ${daemons[@]}
echo ${daemons[*]}
echo ${#daemons[@]}

filelist=($(ls))
echo $filelist
#echo $filelist | wc -w
echo ${#filelist[@]}

me=("probecoding" "30" "IT")
echo ${me[*]}

mysql_id='root'
mysql_dir='/etc/mysql'

echo $mysql_id
echo $mysql_dir

myname="probecoding"
myage=30
mycarrer='IT'

echo $myname $myage $mycarrer


# $$: PID
# $0 : 이름
# S1 첫번째 인자
# $*: 이름을 뺀 나머지 인자 리스트
# $# 이름을 뺀 나머지 인자 개수
#
echo $$ $0 $1 [$*] \#$#

num=0
#let num=(3*5)/4+7  # 정수만 해당
num=`expr \( 3 \* 5 \) / 4 + 7`
echo $num

let num=(10+20)/8-8
echo $num

if [ -z $1 ]; then
	echo "\$1 이 없습니다."
elif [ -z $2 ]; then
	echo "\$1 = $1"
	echo "\$2 이 없습니다."
elif [ $1 != $2 ];then
	echo "\$1 과 \$2 가 다릅니다."
	echo "\$1: $1 ,  \$2: $2"
elif [ $1 == $2 ]; then
	echo "둘이 같습니다.!!!"
	echo "\$1: $1 , \$2: $2"
fi

a=10
b=15
c=100
d=20

if [ $a -eq 10 ]; then
	echo "같습니다."
fi

if [ $a -ne $b ]; then
	echo "같지 않습니다."
fi

if [ $a -lt $b ]; then
	echo "a값이 작습니다."
fi

if [ $a -le $c ]; then
	echo "a값이 작거나 같습니다."
fi

if [ $c -gt $b ]; then
	echo "c값이 b보다 큽니다."
fi

if [ $d -ge $b ]; then
	echo "d값이 b보다 크거나 같습니다."
fi

if [ -e $1 ] ; then
	echo "$1 is file"
fi

if [ -d $1 ]; then
	echo "$1 is directory"
fi

if [ -f $1 ]; then
	echo "$1 is normal file"
else 
	echo "$1 is not normal file"
fi

if [ -r $1 ]; then
	echo "$1 is readable"
fi

if [ -h $1 ]; then
	echo "$1 is simbolic link"
fi

if [ ! -s $1 ]; then
	echo "size of $1 is 0"
else
	echo "size of $1 is not 0"
fi

if [ -u $1 ]; then
	echo "set-user-id가 설정되어 있음"
fi

if [ -w $1 ]; then
	echo "$1 is writable"
fi

if [ -x $1 ]; then
	echo "$1 is excutable"
fi

a="test"
b="Test"
c=5
d=15
e=100

if [ $a -eq $b ]; then
	echo "same"
else
	echo "diff"
fi

if [[ $c > 0 && $d > 10 && $e == 100 ]]; then # 부호에 대해 이스케이프
	echo "everything is all right"
fi

if [ $c -ne 6 ]; then
	echo "$c is not 6."
fi

if [ $c -ge 100 -o $d -ne 15 -o $e -le 200 ]; then
	echo "all right..."
fi

if [ $c -eq 5 -a $d -eq 15 -a $e -eq 100 ] ; then
	echo "king right"
fi

if [ true ]; then
	echo "I want to fuck you"
fi

if [ !false ]; then
	echo "you will be fucked"
fi

gateway="192.168.35.1"

ping -c 1 $gateway > /dev/null

if [ $? == 0 ]; then
	echo "Gateway ping success!"
else
	echo "Gateway ping failed!"
fi

if [ -z $1 ]; then echo "give a argument"; fi


echo "-------------------------"
for db in $(ls)
do 
	echo $db
done

echo "-------------------------"
for db in $(ls); do
	echo $db
done


echo "-------------------------"
for db in $(ls); do echo $db; done

echo "-------------------------"

list=($(ls))
num=${#list[@]}
index=0
while [ $num -gt 0 ]
do
	echo ${list[$index]}
	let index+=1
	echo "index: $index"
	let num-=1
	echo "num: $num"
	echo ""
done


#과제 : 순수 파일만 찾아서 그 파일명과 파일 사이즈를 출력하라.

#filename=$1
filesize=$(stat -c%s $1)
echo "size of $1 = $filesize bytes."