Linux命令行和shell脚本编程宝典 Richard Blum

所属分类: 脚本专栏 / linux shell 阅读数: 810
收藏 0 赞 0 分享

第一个脚本文件

复制代码 代码如下:

#!/bin/bash
echo "This is my first bash code!"
exit 0

重定向符号和数学计算
复制代码 代码如下:

#!/bin/bash
echo -n "The time and date are: "
date
value1=100  #等号前后不允许出现空格
value2=$value1
echo -n "value1="
echo $value1
echo -n "value2="
echo $value2
ls -l | sort > out.txt   #管道符号(|)和重定向输出符号>
ls -l >> out.txt   #重定向追加输出符号>>
echo -n  "wc<out.txt:"
wc < out.txt  #重定向输入符号<
echo "sort<<EOF ... EOF"
sort << EOF  #内置输入重定向<<
`date`
EOF
#数学计算
echo -n "expr进行计算:1+5="
expr 1+5
echo -n "使用方括号进行计算:1+5="
echo $[1+5]
echo "使用bc计算器进行浮点运算"
var1=100
var2=200
var3=`echo "scale=4;$var1/$var2" | bc`
echo "$var1 / $var2 = $var3"
var4=71
var5=`bc<<EOF
scale=4
a1=($var1*$var2)
b1=($var3*$var4)
a1+b1
EOF`
echo "var5=$var5"
exit 0

使用test命令
复制代码 代码如下:

#!/bin/bash
#使用test命令
var1=10
var2=100
if [ $var1 -gt $var2 ]
then
    echo "var1 grate var2"
else
    echo "var2 grate var1"
fi
#只能比较整数
test_user=hanxi
if [ $USER = $test_user ]
then
    echo "Welcome $test_user"
fi
str1=Hanxi
str2=hanxi
if [ $str1 \> $str2 ]
then
    echo "$str1 > $str2"
else
    echo "$str1 < $str2"
fi
if [ -n $str1 ]
then
    echo "The string '$str1' is not empty"
else
    echo "the string '$str1' is empty"
fi
#检查文件目录
if [ -d $HOME ]
then
    echo "your Home dir exists"
    cd $HOME
    ls -a
else
    echo "there's a problem with your HOME dir"
fi
pwfile=/etc/shadow
if [ -f $pwfile ]
then
    if [ -r $pwfile ]
    then
        tail $pwfile
    else
        echo "Sorry, I'm unable to reas the $pwfile file "
    fi
else
    echo "Sorry, the file $pwfile doesn't exist"
fi
if [[ $USER == h* ]]
then
    echo "Hello $USER"
else
    echo "Sorry, I don't know you"
fi

循环语句
复制代码 代码如下:

#!/bin/bash
for file in /home/hanxi/*
do
    if [ -d "$file" ]
    then
        echo "$file is a directory"
    elif [ -f "$file" ]
    then
        echo "$file is a file"
    fi
done
var1=10
while [ $var1 -gt 0 ]
do
    echo $var1
    var1=$[ $var1 - 1 ]
done
var1=100
until [ $var1 -eq 0 ]
do
    echo $var1
    var1=$[ $var1 - 25 ]
done
#文件数据的循环
IFSOLD=$IFS
IFS=$'\n'
for entry in `cat /etc/passwd`
do
    echo "Values in $entry -"
    IFS=:
    for value in $entry
    do
        echo " $value"
    done
done | more
for file in /home/hanxi/*
do
    if [ -d "$file" ]
    then
        echo "$file is directory"
    elif
        echo "$file is a file"
    fi
done > output.txt

读取参数
复制代码 代码如下:

#!/bin/bash
name=`basename $0`
echo the commane entered is : $name
c_args=$#
echo count args:$c_args
#取最后一个参数
echo the last parameter is ${!#}
echo all parameter: $*
echo all parameter: $@
count=1
for param in "$@"
do
    echo "\$@ parameter #$count = $param"
    count=$[ $count + 1 ]
done
#getopts
while getopts :ab:c opt
do
    case "$opt" in
    a) echo "Found the -a option";;
    b) echo "Found the -b option, with value $OPTARG";;
    c) echo "Found the -c option";;
    *) echo "Unknown option : $opt";;
    esac
done
shift $[ $OPTIND - 1 ]
count=1
for param in "$@"
do
    echo "Parameter $count: $param"
    count=$[ $count + 1 ]
done
read -p "Please enter your age:" age
echo age:$age
if read -t 5 -p "Please enter your name: " name
then
    echo "Hellp $name,welcome to my script"
else
    echo
    echo "sorry ,too slow!"
fi
read -n1 -p "Do you want to continue [Y/N]?" answer
case $answer in
Y | y) echo
       echo " fine, continue on...";;
N | n) echo
       echo OK,Good bye
       exit;;
esac
echo "This is the end of the script"
read -s -p "Enter your password: " pass
echo
echo "Is your password really $pass?"
#读取文件
count=1
cat for.txt | while read line
do
    echo "Line $count: $line"
    count=$[ $count+1 ]
done
echo "Finished processing the file"

重定向文件描述符
复制代码 代码如下:

#!/bin/bash
#永久重定向
exec 9>&2
exec 2>testerror
echo "this will in testerror">&2
exec 2<&9
exec 9<&0
exec 0<testin
count=1
while read line
do
    echo "Line #$count:$line"
    count=$[ $count + 1 ]
done
exec 0<&9
#重定向文件描述符
exec 3>&1
exec 1>testout
echo "this should store in the output file"
echo "along with this line."
exec 1>&3
echo "Now things should be back to nomarl"
exec 4<&0
exec 0<testin
count=1
while read line
do
    echo "Line #$count:$line"
    count=$[ $count + 1 ]
done
exec 0<&4
read -p "Are you done now?" answer
case $answer in
Y|y) echo "Goodbye";;
N|n) echo "continue...";
esac
#创建读写文件描述符
exec 8<> testfile
read line <&8
echo "Read:$line"
echo "This is a test line" >&8
#关闭文件描述符
exec 8>&-
#列出文件描述服
#`/usr/sbin/lsof -a -p $$`|more
#禁止命令输出
#2 > /dev/null
#创建本地临时文件
tempfile=`mktemp test.XXXXXX`
exec 4>$tempfile
echo "This is the first line">&3
exec 4>&-
#在/temp中创建临时文件
tmpfile=`mktemp -t tmp.XXXXXX`
echo "The temp file is located at:$tempfile"
cat $tempfile
rm -f $tempfile
#创建临时文件夹
tmpdir=`mktemp -d dir.XXXXXX`
cd $tmpdir
tempfile1=`mktemp temp.XXXXXX`
ls -l
cd ..
#记录消息
a=`date | tee testfile;\
cat testfile;\
date | tee -a testfile;\
cat testfile`

信号处理
复制代码 代码如下:

#!/bin/bash
#信号处理
trap "echo 'get a sign'" SIGINT SIGTERM
trap "echo byebye" EXIT
echo "This is a test program"
count=1
while [ $count -le 10 ]
do
    echo "Loop #$count"
    sleep 10
    count=$[ $count+1 ]
done
echo "This is the end of the test program"
trap - EXIT#移除捕获
#后台牧师运行
#./test6.sh &
#不使用终端的情况下运行脚本
#nohup ./test6.sh &
#查看作业
#jobs
#重新启动作业
#bg 2(作业序号)//后台
#fg 2//前台
#优先级
#nice -n 10 ./test6.sh
#renice 10 -p 25904(进程号)
#预计时间运行at命令
#at -f test6.sh 20:00
#batch命令,系统平均负载低于0.8时运行,可以设定时间,比at命令更好
#corn表格可以设定循环运行,格式:
#min hour dayofmonth month dayofweek command
#每个月第一天运行:
#12 16 * * 1 command
#每个月最后一天运行:
#12 16 * * * if [ `date +%d =d tommorrow` = 01 ] ; then ; command

函数的使用
复制代码 代码如下:

#!/bin/bash
#函数
#使用返回值
function func1
{
    read -p "Enter a value: " value
    echo $[ $value * 2 ]
}
result=`func1`
echo "the new value is $result"
#传递参数
function func2
{
    echo $[ $1+$2 ]
}
result=`func2 2 2`
echo "the new result is $result"
#局部变量, 递归
function func3
{
    if [ $1 -eq 1 ]
    then
        echo 1
    else
        local temp=$[ $1-1 ]
        local result=`func3 $temp`
        echo $[ $result*$1 ]
    fi
}
read -p "Enter value:" value
result=`func3 $value`
echo "the factorial of $value is: $result"
#调用当前目录下到函数库
#. ./myfuncs

更多精彩内容其他人还在看

Linux shell中的printf的详细用法

这篇文章主要介绍了Linux shell中的printf的详细用法的相关资料,希望能通过本文能帮助到大家,需要的朋友可以参考下
收藏 0 赞 0 分享

shell字符截取命令之cut命令的实例详解

这篇文章主要介绍了shell字符截取命令之cut命令的实例详解的相关资料,希望通过本文大家能够掌握这部分内容,需要的朋友可以参考下
收藏 0 赞 0 分享

Linux 在Bash脚本中怎么关闭文件描述符的实例

这篇文章主要介绍了Linux 在Bash脚本中怎么关闭文件描述符的实例的相关资料,希望通过本文能帮助到大家实现这样的功能,需要的朋友可以参考下
收藏 0 赞 0 分享

Linux使用文本编辑器vi常用命令

vi就是一种功能强大的文本编辑器,而vim则是高级版的vi,不但可以用不同颜色显示文字内容,还能进行诸如shell脚本、C语言程序编辑等功能,可以作为程序编辑器。下面通过本文给大家介绍linux 文本编辑器vi常用命令,一起看看吧
收藏 0 赞 0 分享

Linux 中unzip解压时中文乱码的解决办法

这篇文章主要介绍了Linux 中unzip解压时中文乱码的解决办法的相关资料,这里提供两种解决办法,希望能通过本文帮助到大家,需要的朋友可以参考下
收藏 0 赞 0 分享

Linux shell ftp命令根据文件日期下载文件的方法

最近做项目遇到这样的需求要求ftp获取远程数据的文件,根据文件的创建时间点下载文件。下面小编给大家分享知识点小结,感兴趣的朋友要求看看吧
收藏 0 赞 0 分享

Linux下服务器重启的脚本命令

Linux关闭和重启系统一般使用相同的命令可以实现。下面脚本之家小编给大家带来了Linux下服务器重启的脚本命令,感兴趣的朋友一起看看吧
收藏 0 赞 0 分享

Linux中的特殊符号与正则表达式

这篇文章主要介绍了Linux中的特殊符号与正则表达式,需要的朋友可以参考下
收藏 0 赞 0 分享

利用shell命令统计日志的方法详解

这篇文章主要给大家介绍了关于利用shell命令统计日志的方法,通过这个命令将会对大家的学习或者工作具有一定的参考学习价值,文中给出了详细的示例代码,需要的朋友可以参考借鉴,下面随着小编来一起学习学习吧。
收藏 0 赞 0 分享

Linux中的Syslog命令

syslog是Linux系统默认的日志守护进程,默认的syslog配置文件是/etc/syslog.conf文件。接下来通过本文给大家分享Linux中的Syslog命令,感兴趣的朋友一起看看吧
收藏 0 赞 0 分享
查看更多