Linux 프로그래밍 고전 사례

3106 단어
1. 디지털 출력
#!/bin/bash
echo "Please type your number:"
read a
for ((i=1;i<=a;i++))
  do
  for((p=1;p<=i;p++))
    do
    echo -n "$p"
    done
  echo 
  done

2. 구구단 출력
#!/bin/bash
for((i=1;i<10;i++))
  do
  for((p=1;p<=i;p++))
    do
    echo -ne " $p x $i = "`expr $i \* $p`
    done
  echo
  done
echo

3. 마름모꼴 출력
#!/bin/bash
echo "Please type a number:"

read num
for((i=1;i<=num;i++))
  do
  for((j=0;j

4. 계산기
#!/bin/bash
s=0
while (())
#echo "..........+"
#echo "..........-"
#echo "..........x"
#echo "........../"
#echo "..........q"
echo "Please type your word:(eg.1 + 2)"

read a b c
do
case $b in 
+)
let s=a+c
echo "$a + $c = "$s;;
-)
let s=a-c
echo "$a - $c = "$s;;
/)
echo "$a / $c = "$(printf "%.2f" `echo "scale=2;$a/$c"|bc`)
;;
*)
let s=a*c
echo "$a * $c = "$s;;

esac
case $a in 
q) break;;
esac
done

5. 전화번호부
#!/bin/bash
read file
if [ -f "$file" ]
then
  echo "$file found."
  echo "Continue!"
else
  echo "$file not found."
  touch $file
  echo "Create $file success!"
fi

while(())
echo "If you want to add a user plese type----------------------add" 
echo "If you want to del a user plese type----------------------del" 
echo "If you want to watch all users plese type-----------------ls" 
echo "If you want to edit users plese type----------------------edit" 
echo "If you want to exit plese type----------------------------q" 

read b
do
case $b in
add)
echo "Please type your name:"
read name
echo "Please type your number:"
read number
echo $name $number >>/data/crm/crmpluscx/0108/phone
;;

del)
echo "Please type which name do you want to del:"
read name
cat /data/crm/crmpluscx/0108/phone|grep -v $name > /data/crm/crmpluscx/0108/trance
cat ~/0108/trance > ~/0108/phone
;;

ls)
cat  /data/crm/crmpluscx/0108/phone;;

edit)
echo "Please type which name do you want to edit:"
read name
cat /data/crm/crmpluscx/0108/phone|grep -v $name > /data/crm/crmpluscx/0108/trance
cat ~/0108/trance > ~/0108/phone
echo "Please type your new name"
read name
echo "Please type your new nunber"
read number
echo $name $number >> /data/crm/crmpluscx/0108/phone
;;

q)exit;;

esac
done

6, 메뉴 시뮬레이션 셸 명령
#!/bin/bash
while (())
echo "List Directory..........l"
echo "Change Directory........c"
echo "Edit File...............e"
echo "Remove File.............r"
echo "Exit Menu...............q"

read ch
do
case $ch in
l)ls;;
c)
echo Enter target directory
read direc
cd "$direc"
;;
e)
echo Enter file name
read file
vi $file 
;;
r)
echo Enter file name
read file
rm -f $file
;;

q|Q)
echo Goodbye
break;;

*)
echo illegal Option

esac
done

좋은 웹페이지 즐겨찾기