2022-04-22 Shell_script_4

2022. 4. 22. 15:36BE/Linux

IF

#!/bin/bash

read -p "enter income amount" Income
read -p "enter expenses aount" Expense

let Net=$Income-$Expense

if [ "$Net" -eq "0" ]
then
    echo "Income and expences are equal"
elif [ "$Net" -gt "0" ]
then
    echo "profit : " $Net
else
    echo "Loss : " $Net
fi

CASE

case word in
	pattern1) command-list1
	;;
	pattern2) command-list2
	;;
	patternN) command-listN
	;;
esac
checked against word for match
may also contain:

	*
	?
	[ … ]
	[:class:]

multiple patterns can be listed via:
	|
#!/bin/bash

echo "enter Y to see all files including hidden files"
echo "enter N to sell non_hidden files"
echo "enter Q to quit"

read -p "enter your choice " reply

case $reply in
	Y|YES) echo "Display all files"
				ls -a ;;

	N|NO) echo "Display all non-hidden files"
				ls ;;

	Q) exit 0 ;;

	*) echo "Invalid choice !!!"; exit 1 ;;

esac
#!/bin/bash

ChildRate=3
AdultRate=10
SeniorRate=7

read -p "Enter your age: " age

case $age in
  [1-9]|[1][0-2])   # child, if age 12 and younger
     echo "your rate is" '$'"$ChildRate.00" ;;

	# adult, if age is between 13 and 59 inclusive
  [1][3-9]|[2-5][0-9]) 
     echo "your rate is" '$'"$AdultRate.00" ;;

  [6-9][0-9])       # senior, if age is 60+
     echo "your rate is" '$'"$SeniorRate.00" ;;
esac

DO-WHILE

while [ expressin ]
do 
	command-list
done
#!/bin/bash

counter=0

while [ $counter -lt 10 ]
do
    echo "the counter is $counter"
    let counter=$counter+1
    sleep 1
done
#!/bin/bash

Cont="Y"
while [ $Cont = "Y" ]
do
	ps -A
	read -p "want to continue? (Y/N)" reply
	Cont=`echo $reply | tr [:lower:] [:upper:]`
done
echo "done"
#!/bin/bash 
# copies files from home- into the webserver- directory 
# A new directory is created every hour

PICSDIR=/home/carol/pics 
WEBDIR=/var/www/carol/webcam 

while true; do 
   DATE=`date +%Y%m%d` 
   HOUR=`date +%H` 
   mkdir $WEBDIR/"$DATE" 
   while [ $HOUR -ne "00" ]; do 
      DESTDIR=$WEBDIR/"$DATE"/"$HOUR" 
      mkdir "$DESTDIR" 
      mv $PICSDIR/*.jpg "$DESTDIR"/ #cp? mv?
      sleep 3600 
      HOUR=`date +%H` 
   done 
done

UNTIL

until [ expression ]
do
	command-list
done

while이랑 다른 점 : To execute commands in “command-list” as long as “expression” evaluates to false

 

조건이 거짓일 경우 반복문 수행.

#!/bin/bash 

COUNTER=20

until [ $COUNTER -lt 10 ]
do 
   echo $COUNTER 
   let COUNTER-=1 
done
#!/bin/bash

Stop="N"
until [ $Stop = "Y" ]; do
  ps -A
  read -p "want to stop? (Y/N)" reply
  Stop=`echo $reply | tr [:lower:] [:upper:]`
done
echo "done"

FOR

for variable in argument-list
do 
	command-list
done
#!/bin/bash

for i in 0 1 2 3 4 5 6 7 8 9
do
	echo $i
	sleep 1
done
#!/bin/bash
# compute the average weekly temperature

for num in 1 2 3 4 5 6 7
do
   read -p "Enter temp for day $num: " Temp
   let TempTotal=$TempTotal+$Temp
done

let AvgTemp=$TempTotal/7
echo "Average temperature: " $AvgTemp
#!/bin/bash

Sum=0

for num in {1..100}
do
    let Sum=$Sum+$num
done

echo $Sum

SELECT

select word in list
do
	...
done

# ⌃d(ctrl+d)나 ⌃c(ctrl+c)를 입력하기 전까진 무한 반복
#!/bin/bash

select var in alpha beta gamma
do
	echo $var
done

BREAK & CONTINUE

 

#!/bin/bash

for index in 1 2 3 4 5 6 7 8 9 10
do
        if [ $index -le 3 ]; then
             echo "continue"
             continue
        fi
        echo $index
        if  [ $index -ge 8 ]; then
             echo "break"
             break
        fi
done

FUNTION

funtion-name () {
	statements
}
#!/bin/bash
 
funky () { 
	# This is a simple function 
	echo "This is a funky function." 
	echo "Now exiting funky function." 
} 

# declaration must precede call: 

funky

선언이 호출보다 먼저 있어야 한다.

LOCAL VARIABLES IN FUNC

#!/bin/bash

global="pretty good variable"

foo () {
        local inside="not so good variable"
        echo $global
        echo $inside
        global="better variable"
}

echo $global
foo
echo $global
echo $inside

output
"pretty good vaiables"
"pretty good vaiables"
"not so good variable"
"better variable"

'BE > Linux' 카테고리의 다른 글

2022-05-01 file을 찾는 방법  (0) 2022.05.01
2022-04-23 Shell_script_조건문_사용  (0) 2022.04.23
2022-04-22 Shell_산술_연산하는_방법  (0) 2022.04.22
2022-04-16 Shell_script_추천글  (0) 2022.04.16
2022-04-14 Shell_script_3  (0) 2022.04.15