linux shell 小程序,几个shell小程序 --鸟哥的linux私房菜

1. 读入一个文件夹,察看其下所有文件的属性

read -p "enter the dir:" dir

filelist=`ls $dir`                   #`` 而非‘’

for filename  in $filelist

do

perm=""           # 每次都重置为空

test -r "$dir/$filename" && perm="$perm readable"

test -w "$dir/$filename" && perm="$perm writable"

test -x "$dir/$filename" && perm="$prem excuted"

echo "$dir/$filename   has the permssions of $perm"

done

2. 输入的如果是yes就继续,如果是no,就停止

read -p "input y/n: " yn

if [ "$yn" == "Y" -o "$yn" == "y" ]; then   #此处的-o用法 和“$y”而不能是$y,且if之后有,

echo "ok,continue"

elif [ "yn" == "N" -o "$yn" == "n" ];then

echo "oh,interrupt"

else

echo "i don't know what your choice"

fi

3.通过端口检测使用的协议

#detect the www ,ftp ssh

echo "the www ,ftp ,ssh will be detect"

testing=`netstat -tunl|cut -d ":" -f 1|cut -d " " -f 1` #此处的netstat -tnul 还有后面的cut的格式

if [ "$testing" == "80" ];then

echo "www is running on your sysytem"

elif [ "$tesing" == "21" ];then

echo "ftp is ruuning on your sysytem"

elif [ "$tesing" == "22" ];then

echo "ssh is running on your system"

else

echo "none of the www ftp ssh is running on your system"

fi

4.看看/etc/passwd里面的用户在第几行

#output the users of passwd

accountlist=`cat /etc/passwd|cut -d ":" -f 1`

line=1

for account in $accountlist

do

echo "the $line  is $account"

line=$(($line + 1))

done

5. 写一个文件,文件名是你输入的名字加日期

echo -e "i will use touch make a file "

read -p  "please input a fileuser" fileuser

filename=${fileuser:-"filename"}

date1=`date --date='2 days ago' +%F`

date2=`date --date='1 days ago' +%F`

date3=`date +%Y%m%d`

file1="$filename""$date1"

file2="$filename""$date2"

file3="$filename""$date3"

touch $file1

touch $file2

touch $file3

6.数值的加法

echo -e "you can input two numbers,i will compute them"

read -p "the first  number" num1

read -p "the second  number" num2

total=$(($num1+$num2))                                      #here the + could be replaced by */ %*

echo "the total number of $num1 and $num2 is $total "

7.查看文件的存在性,是否是目录及文件的权限

#!bin/bash

#program

#     let user input a filename ,the program will searchfilename

#  1.exit ? 2.file/directory ? 3 file permissions

#history

#2/24/2012        Vbird  first release

PATH=/usr/lib/lightdm/lightdm:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games

export PATH

#1.input a filename

read -p "please input a filename:  " filename

test ! -e  $filename && echo "filename doesn't exit" && exit 0

#2.test the filetype

test -f  $filename && filetype="regular file"

test -d  $filename && filetype="directory file"

#3.test the permissions

test -r  $filenam &&  perm="readable"

test -w  $filenam &&  perm="$perm writable"

test -x  $filenam &&  perm="$perm excutalbe"

#4.output the result

echo "the $filename is a $filetype "

echo "the $filename has the $perm of permissions"

8.test yes or no 和上面的一个比价类似

#test yes or no

read -p "input yes or no: " yn

[ "$yn" == "Y" -o "$yn" == "y" ]&& echo "ok,continue"&&exit 0

[ "$yn" == "N" -o "$yn" == "n" ]&& echo "oh,interrupt"&&exit 0

echo "i don't know what your choice"

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值