Linux Shell 笔记一

最近在学习linux,在这里给大家推荐一个网站http://linux.vbird.org/ 上面关于linux的教程写得很好,也避免了我在学习过程中的很多弯路。

 

程序一:HelloWord,在屏幕输出HelloWord

利用vim 创建一个sh01.sh文件

echo -e "Hello World!/a/n" exit 0

程序二:将输入参数拼接后输出
read -p "First Name:" firstname read -p "Last Name:" lastname echo -e "/n your full name is:$firstname $lastname"
程序三:
根据日期创建文件

echo -e "I will use touch comand to create 3 file./n" read -p "please input the filename what you want:" fileuser filename=${fileuser:-"filename"} date1=`date --date='2 days ago' +%Y%m%d` file1="$filename""$date1" touch $file1".log"

程序四:普通计算

[root@localhost scripts]# cat sh4.sh read -p "first:" first read -p "second:" second total=$(($first*$second)) echo -e "/n========>$total"

程序五:查找文件,判定权限
[root@localhost scripts]# cat sh5.sh echo -e "input filename" read -p "input the filename you want:" filename test -z $filename && echo "/filename can't be null" && exit 0 test ! -e $filename && echo "file not exist!" && exit 0 test -f $filename && filetype="regulare file" test -d $filename && filetype="directory" test -r $filename && perm="readable" test -w $filename && perm="$perm writable" test -x $filename && perm="$perm executable" echo "The filename:$filename is a $filetype" echo "And the permission are :$perm"
程序六:if else

[root@localhost scripts]# cat sh6.sh echo "hello world ,shell" [ -n "$0" ] && echo "$0" || exit 0 [ -n "$1" ] && echo "$1" || exit 0 echo "shell over!" exit 0 #调用时采用 sh sh6.sh par1 par2 #其中sh6.sh为第一个参数 #par1为第二个参数 #$0取第一个参数,$1取第二个参数
程序7:if else(2)

[root@localhost scripts]# cat sh7.sh read -p "yes or no (y/n)?" ny #注意空格!! if [ "$ny" == "Y" ] || [ "$ny" == "y" ]; then echo "OK,continue" exit 0 fi if [ "$ny" == "N" ] || [ "$ny" == "n" ]; then echo "Oh,No,interrupt!" exit 0 fi echo "A foolish man! fuck params wrong" && exit 0

程序8:if else(3)

[root@localhost scripts]# cat sh8.sh read -p "input y/n?" yn if [ "$yn" == "Y" ] || [ "$yn" == "y" ]; then echo "Hello World,Yes!" exit 0 else echo "fuck ,you choose,N!" exit 0 fi

程序9:if elif ese

[root@localhost scripts]# cat sh9.sh read -p "input y/n?" param if [ "$param" == "y" ]; then echo "yes,Hello World" exit 0 elif [ "$param" == "n" ]; then echo "no,good boy!" exit 0 else echo "fuck man ,wrong params!" fi

程序10:根据端口检测系统服务
#netstat 查询系开的端口,并过滤80端口,httpd使用的是80端口 [root@localhost scripts]# cat sh10.sh testing=`netstat -tuln | grep ":80"` if [ "$testing" != "" ]; then echo "WWW is running in your system." fi

程序11:时间运算

 

[root@localhost scripts]# cat sh11.sh read -p "enter date:" mydate #简单判断输入的时间是否合法 `是1键左的哦 date_d=`echo $mydate |grep '[0-9]/{8/}'` if [ "$date_d" == "" ]; then echo "wrong date" exit 1 else echo "date is right,computing..." fi declare -i date_dem=`date --date="$date_d" +%s` echo "$date_dem" declare -i date_now=`date +%s` declare -i date_total_s=$(($date_dem-$date_now)) echo "$date_total_s" declare -i date_d=$(($date_total_s/60/60/24)) echo "date is:""$date_d" exit 0

 

 

 

你可能感兴趣的:(Linux Shell 笔记一)