这两天学习了shell入门知识,现将我的学习笔记与心得与大家分享一下
第一个shell:
#! /bin/sh
play="hello"
printf "$play\n"
printf "the programming $0 is now running\n"
printf "the $1 is one\n"
printf "the $2 is two\n"
printf "just $*\n"
printf "enter a string\n"
read play
printf "$play\n"
exit 0
这个程序主要是注意$的使用:
$0 代表脚本程序本身的名字
$1 $2 $3 。。。分别代表 脚本后跟的参数
$* 代表所有的参数
printf 就是跟c语言中的printf基本用法都一样
不过很多人更喜欢用 echo
但是 echo 里面如果 有转义字符 要用 -e 会导致移植性差一点(由于写C写惯了,喜欢用printf )
read 是一个命令
即读入一个字符串
(shell 里面变量默认都是以字符串存储的)
这个脚本 建议大家 在系统上试一下
就是先输入脚本名 ,然后 在后面加几个参数(这里就不详细说了)
第二个脚本
#! /bin/bash
if [ -f /bin/bash ]
then
printf "file /bin/bash exits\n"
fi
if [ -d /bin/bash ]
then
printf "/bin/bash is a directory\n"
else
printf "/bin/bash is NOT a directory\n"
fi
exit 0
这个脚本涉及 if 语句
格式如下:
if [ comment ]
then
statement
else
statement
fi
总之感觉跟C差别不大
中括号里面 如果返回0 表示 true ,1 表示 false (这点有点不同,但是在 算术表达式的时候,有点差别)
[ -f /bin/bash ] 表示判断是否 /bin/bash 是否是个普通文件
[ -d /bin/bahs ] 判断是否是目录
更详细的参数,请 man test
第三个脚本
#! /bin/bash
for foo in bar fud 43
do
printf "$foo\n"
done
exit 0
主要是熟悉 for 语句语法
如下:
for var in x1 x2 x3
do
statement
done
斜体是关键字
var 是变量 $var 是引用变量
第四个脚本
#! /bin/bash
printf "enter password\n"
read try
while [ "$try" != "xxp" ]; do
printf "sorry , wrong password\n"
read try
done
exit 0
主要是while循环
如下:
while 条件
do
statement
done
第五个脚本
#! /bin/bash
printf "is it moring\n"
printf "please input yes or no\n"
read timeofday
while true
do
case "$timeofday" in
yes | Y | y | YES ) printf "Good moring\n"
break;
;;
n* | N* ) printf "Good afternoon\n"
break;
;;
* ) printf "you don't input yes or no\n"
read timeofday
;;
esac
done
exit 0
case 语法:
case var in
a ) statement ;;
b ) statement ;;
c ) ..... ;;
* )
esac
上面那个break不是用来退出case语句的(case 语句不需要 break ),是用来退出 while 循环的
第六个脚本
#! /bin/bash
foo() {
printf "hello world\n"
}
printf "function start\n"
result="$(foo)"
printf "$result\n"
printf "end of funtion\n"
exit 0
主要是函数的使用
很简单
函数名(){
statement
}
就行了
$(foo)
是获得函数的输出
调用可以直接这样 foo
第七个脚本
#! /bin/bash
x=5
x=$(expr $x + 1)
echo $x
exit 0
expr 进行数学计算
上面输出 5
#! /bin/bash
while [ "$1" != "" ]; do
printf "$1\n"
shift
done
exit 0
shift 表示 将参数左移动一个位置
比如开始时,为 $1 , $2 ,$3,$4
shift 之后 变为 $1=$2 $2 =$3 $3=$4
第八个脚本
#! /bin/bash
trap 'rm -f /tmp/my_temp_file_$' INT
printf "creating file /tmp/my_temp_file_$\n"
date > /tmp/my_temp_file_$
printf "press CTRL-C to interrupt ...\n"
while [ -f /tmp/my_temp_file_$ ] ; do
printf "File exist\n"
sleep 2
done
trap INT
printf "creating File /tmp/my_temp_file_$"
date > /tmp/my_temp_file_$
printf "press CTRL-C to interrupt ...\n"
while [ -f /tmp/my_temp_file_$ ] ; do
printf "File exist\n"
sleep 2
done
printf "Never go to here"
exit 0
主要是对于信号的处理
trap 'command' 信号
表示遇到什么信号进行什么处理
第九个脚本
#! /bin/sh
x=0
while [ "$x" -ne 10 ]; do
printf "$x\n"
x=$(($x+1))
done
exit 0
主要是: ((数值计算))
-ne 表示 不等于
两个括号表示计算数值
最后一个综合脚本
模仿linux程序设计里面写的一个超级简单的图书管理系统
#! /bin/bash
save_file="book.cdb"
temp_file="temp.cdb"
trap 'rm -f $temp_file' INT
get_confirm(){
printf " do you confirm ?y/n \n"
local choice=""
while true
do
read choice
case "$choice" in
y | Y | yes | Yes ) return 0 ;;
n | N | No | NO ) return 1 ;;
* ) printf " please input right choice \n" ;;
esac
done
}
update_message(){
printf " please input the number of the book \n"
read number
printf " please input the price of every book \n"
read price
printf " you update message -: \n"
printf " number:$number,price:$price \n"
printf "$1 $number $price \n" >> $temp_file
}
add_message(){
local title=""
local number=""
local price=""
local choice=""
while [ "$choice" != "q" ]
do
printf " Please input the title of the book \n"
read title
printf " please input the number of the book \n"
read number
printf " please input the price of every book \n"
read price
printf " you add message -: \n"
printf " title:$title,number:$number,price:$price\n"
if get_confirm ; then
printf "$title $number $price\n" >> $save_file
else
printf " Canceled \n"
fi
printf " Do you want Continue?y/n \n"
read choice
case "$choice" in
y | Y | yes | Yes ) ;;
n | N | No | NO ) choice="q";;
* ) printf " please input right choice \n" ;;
esac
sleep 1
done
}
remove_message(){
local search=""
printf " input title that you want to delete \n"
read search
grep "$search" $save_file > $temp_file
if [ ! -s "$temp_file" ]; then
printf " sorry, no record \n"
else
printf " you want to delete: "
cat $temp_file
printf "\n"
if get_confirm ; then
grep -v "$search" $save_file > $temp_file
mv $temp_file $save_file
printf " finished:$search delete \n"
else
printf " Now :Canceled \n"
fi
fi
sleep 1
}
list_message(){
if [ ! -s $save_file ] ; then
printf " No Records \n"
else
printf " The Book Message : \n"
printf "Book Number Price \n"
cat $save_file
printf "\n"
fi
sleep 2
}
update(){
printf " input you want to update book title \n"
local search=""
read search
grep "$search" $save_file > $temp_file
if [ ! -s "$temp_file" ]; then
printf " sorry, no record \n"
else
printf " you want to update: "
cat $temp_file
printf "\n"
if get_confirm ; then
grep -v "$search" $save_file > $temp_file
printf " now input new message about the $search\n"
update_message $search
mv $temp_file $save_file
printf " finished:$search update \n"
else
printf " Now :Canceled \n"
fi
fi
sleep 1
}
find_message(){
printf " input title you want find \n"
local search=""
read search
grep "$search" $save_file > $temp_file
if [ ! -s "$temp_file" ]; then
printf " sorry, no record \n"
else
printf " message:- \n"
printf "Book Number Price \n"
cat $temp_file
fi
sleep 2
}
set_menu_choice(){
local menu_choice=""
while [ "$menu_choice" != "q" ];
do
clear
printf " Welcome to System \n"
printf "a) Add a book message \n"
printf "r) remove a book message \n"
printf "l) list all book message \n"
printf "u) update a book message \n"
printf "f) find a book message \n"
printf "q) quit \n"
printf " please select a option \n"
read menu_choice
case "$menu_choice" in
a ) add_message;;
r ) remove_message;;
l ) list_message;;
u ) update;;
f ) find_message;;
q ) ;;
* ) printf "Sorry,please select the right one\n"
sleep 1;;
esac
done
}
rm -f "$temp_file"
if [ ! -f "$save_file" ]; then
touch $save_file
fi
clear
printf " Mini BookManger System \n"
sleep 1
set_menu_choice
rm -f "$temp_file"
好了,笔记写到这了
希望大家指正,提出bug
谢谢
转载请注明转载自:nuptxxp 博客地址