最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Linux命令行和shell腳本編程寶典 Richard Blum

 更新時(shí)間:2012年09月23日 20:56:31   作者:  
Linux命令行和shell腳本編程寶典,主要介紹了linux一些命令的使用

第一個(gè)腳本文件

復(fù)制代碼 代碼如下:

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

重定向符號(hào)和數(shù)學(xué)計(jì)算
復(fù)制代碼 代碼如下:

#!/bin/bash
echo -n "The time and date are: "
date
value1=100  #等號(hào)前后不允許出現(xiàn)空格
value2=$value1
echo -n "value1="
echo $value1
echo -n "value2="
echo $value2
ls -l | sort > out.txt   #管道符號(hào)(|)和重定向輸出符號(hào)>
ls -l >> out.txt   #重定向追加輸出符號(hào)>>
echo -n  "wc<out.txt:"
wc < out.txt  #重定向輸入符號(hào)<
echo "sort<<EOF ... EOF"
sort << EOF  #內(nèi)置輸入重定向<<
`date`
EOF
#數(shù)學(xué)計(jì)算
echo -n "expr進(jìn)行計(jì)算:1+5="
expr 1+5
echo -n "使用方括號(hào)進(jìn)行計(jì)算:1+5="
echo $[1+5]
echo "使用bc計(jì)算器進(jìn)行浮點(diǎn)運(yùn)算"
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命令
復(fù)制代碼 代碼如下:

#!/bin/bash
#使用test命令
var1=10
var2=100
if [ $var1 -gt $var2 ]
then
    echo "var1 grate var2"
else
    echo "var2 grate var1"
fi
#只能比較整數(shù)
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

循環(huán)語(yǔ)句
復(fù)制代碼 代碼如下:

#!/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
#文件數(shù)據(jù)的循環(huán)
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

讀取參數(shù)
復(fù)制代碼 代碼如下:

#!/bin/bash
name=`basename $0`
echo the commane entered is : $name
c_args=$#
echo count args:$c_args
#取最后一個(gè)參數(shù)
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"

重定向文件描述符
復(fù)制代碼 代碼如下:

#!/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
#創(chuàng)建讀寫(xiě)文件描述符
exec 8<> testfile
read line <&8
echo "Read:$line"
echo "This is a test line" >&8
#關(guān)閉文件描述符
exec 8>&-
#列出文件描述服
#`/usr/sbin/lsof -a -p $$`|more
#禁止命令輸出
#2 > /dev/null
#創(chuàng)建本地臨時(shí)文件
tempfile=`mktemp test.XXXXXX`
exec 4>$tempfile
echo "This is the first line">&3
exec 4>&-
#在/temp中創(chuàng)建臨時(shí)文件
tmpfile=`mktemp -t tmp.XXXXXX`
echo "The temp file is located at:$tempfile"
cat $tempfile
rm -f $tempfile
#創(chuàng)建臨時(shí)文件夾
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`

信號(hào)處理
復(fù)制代碼 代碼如下:

#!/bin/bash
#信號(hào)處理
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#移除捕獲
#后臺(tái)牧師運(yùn)行
#./test6.sh &
#不使用終端的情況下運(yùn)行腳本
#nohup ./test6.sh &
#查看作業(yè)
#jobs
#重新啟動(dòng)作業(yè)
#bg 2(作業(yè)序號(hào))//后臺(tái)
#fg 2//前臺(tái)
#優(yōu)先級(jí)
#nice -n 10 ./test6.sh
#renice 10 -p 25904(進(jìn)程號(hào))
#預(yù)計(jì)時(shí)間運(yùn)行at命令
#at -f test6.sh 20:00
#batch命令,系統(tǒng)平均負(fù)載低于0.8時(shí)運(yùn)行,可以設(shè)定時(shí)間,比at命令更好
#corn表格可以設(shè)定循環(huán)運(yùn)行,格式:
#min hour dayofmonth month dayofweek command
#每個(gè)月第一天運(yùn)行:
#12 16 * * 1 command
#每個(gè)月最后一天運(yùn)行:
#12 16 * * * if [ `date +%d =d tommorrow` = 01 ] ; then ; command

函數(shù)的使用
復(fù)制代碼 代碼如下:

#!/bin/bash
#函數(shù)
#使用返回值
function func1
{
    read -p "Enter a value: " value
    echo $[ $value * 2 ]
}
result=`func1`
echo "the new value is $result"
#傳遞參數(shù)
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"
#調(diào)用當(dāng)前目錄下到函數(shù)庫(kù)
#. ./myfuncs

相關(guān)文章

  • Linux監(jiān)控cpu以及內(nèi)存使用情況之top命令(詳解)

    Linux監(jiān)控cpu以及內(nèi)存使用情況之top命令(詳解)

    下面小編就為大家?guī)?lái)一篇Linux監(jiān)控cpu以及內(nèi)存使用情況之top命令(詳解)。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2017-05-05
  • Linux網(wǎng)絡(luò)設(shè)置詳情

    Linux網(wǎng)絡(luò)設(shè)置詳情

    這篇文章主要介紹了Linux網(wǎng)絡(luò)設(shè)置,需要的朋友可以參考下面文章內(nèi)容
    2021-08-08
  • linux shell實(shí)現(xiàn)隨機(jī)數(shù)幾種方法分享(date,random,uuid)

    linux shell實(shí)現(xiàn)隨機(jī)數(shù)幾種方法分享(date,random,uuid)

    這篇文章主要介紹了linux shell實(shí)現(xiàn)隨機(jī)數(shù)多種方法(date,random,uuid),需要的朋友可以參考下
    2015-10-10
  • Bash?中?nohup?與?&?的區(qū)別及用法詳解

    Bash?中?nohup?與?&?的區(qū)別及用法詳解

    在Bash中,`&`、`nohup`和`disown`各有用途:`&`用于后臺(tái)運(yùn)行命令,選擇合適的工具可以實(shí)現(xiàn)后臺(tái)任務(wù)的穩(wěn)定運(yùn)行,本文介紹Bash?中?nohup?與?&?的區(qū)別及用法,感興趣的朋友一起看看吧
    2025-01-01
  • Shell腳本中判斷輸入變量或者參數(shù)是否為空的方法

    Shell腳本中判斷輸入變量或者參數(shù)是否為空的方法

    這篇文章主要介紹了Shell腳本中判斷輸入變量或者參數(shù)是否為空的方法,本文總結(jié)了5種方法,并分別給出了代碼實(shí)例,需要的朋友可以參考下
    2014-10-10
  • 詳談linux中sar的使用方法

    詳談linux中sar的使用方法

    下面小編就為大家?guī)?lái)一篇詳談linux中sar的使用方法。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2017-03-03
  • git 使用及常用命令

    git 使用及常用命令

    本文是關(guān)于git 的使用和一些git使用小技巧,以及git的常用命令,進(jìn)行的整理,希望能幫助有需要的小伙伴
    2016-07-07
  • Shell中select in的具體使用

    Shell中select in的具體使用

    本文主要介紹了Shell中select in的具體使用,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2023-06-06
  • shell腳本換行問(wèn)題實(shí)戰(zhàn)記錄

    shell腳本換行問(wèn)題實(shí)戰(zhàn)記錄

    換行相信大家都不陌生,下面這篇文章主要給大家介紹了關(guān)于shell腳本換行問(wèn)題的相關(guān)資料,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2023-02-02
  • Django中shell命令的使用

    Django中shell命令的使用

    Django的manage工具提供了shell命令,本文主要介紹了Django中shell命令的使用,方便直接在終端中執(zhí)行測(cè)試python語(yǔ)句,有需要的小伙伴可以參考下
    2021-05-05

最新評(píng)論

紫云| 璧山县| 铜鼓县| 临邑县| 始兴县| 新疆| 田林县| 萝北县| 循化| 长治市| 富宁县| 安溪县| 报价| 行唐县| 宜昌市| 安陆市| 婺源县| 湖州市| 嘉定区| 塔城市| 静乐县| 古丈县| 吴桥县| 宽甸| 正宁县| 同德县| 虎林市| 正宁县| 龙州县| 浏阳市| 龙游县| 阿尔山市| 临猗县| 张家港市| 乌海市| 双江| 军事| 青海省| 武鸣县| 淮阳县| 明水县|