linux shell 脚本命令操作案例大全
2014-07-12 14:10
288 查看
/*************************************************
author:郑金玮
time:2014/07/12
desc:impl shell command opt
*************************************************/
案例1:变量、函数、判断、比较
test.sh
案例2:字符串操作(取子串)
案例3:迭代、数组
案例4:时间操作
author:郑金玮
time:2014/07/12
desc:impl shell command opt
*************************************************/
案例1:变量、函数、判断、比较
test.sh
#!/bin/bash echo ======================== this is a bash demo======================== printf "%s is %d years old \n" zhengjinwei 23 declare count=10 declare fruit=apple declare strNormal="we have $count ${fruit}'s " declare strSpecial="we have $count ${fruit} " function printStr() { if [ $count -gt 1 ] ; then echo "$strNormal" else echo "$strSpecial" fi } printStr echo $(uname) ######################################### declare num=1000 function printNum() { local num=10 let num+=10 echo $num } echo "the global num is : $num " echo -e "call printNum Function :" printNum o "==================this is if else fi ....test demo=====================" if [ -f "test.sh" ] ; then echo "named test.sh is a file.." else echo "named test.sh is not a file.." fi function checkVariableHaveNum() { if [ -n "$1" ] ; then echo "this variable have value" else echo "this variable have not value" fi } declare g_num=10 checkVariableHaveNum $g_num function checkVariableEqual() { if [ "$1" -eq "$2" ] ; then printf "%d == %d " $1 $2 elif [ "$1" -ne "$2" ] ; then printf "%d != %d " $1 $2 elif [ "$1" -ge "$2" ] ; then printf "%d >= %d " $1 $2 elif [ "$1" -gt "$2" ] ; then printf "%d > %d" $1 $2 elif [ "$1" -lt "$2" ] ; then printf "%d < %d" $1 $2 else printf "%d <= %d" $1 $2 fi } checkVariableEqual 2 4 checkVariableEqual 4 2 checkVariableEqual 2 2
案例2:字符串操作(取子串)
echo "-------------------------------------" ########this is string opt################################ #get string's length declare strName="zhengjinwei" declare strNameLength=${#strName} printf "the length of %s is %d \n" $strName $strNameLength function getChildString() { local StrResourse=$1 local nParamCount=$2 local nStartPos=$3 local nEndPos=$4 if [[ $nParamCount -lt 3 ]] ; then echo "param less than 3 is err" elif [[ $nParamCount -gt 4 ]] ; then echo "param more than 4 is err" else if [[ $nParamCount -eq 3 ]] ; then let nEndPos=${#StrResourse} else let nEndPos=$4 fi fi local strResult=${StrResourse:$nStartPos:$nEndPos} printf "the child string in %s from position %d to %d is %s: \n\n\n" $StrResourse $nStartPos $nEndPos $strResult } getChildString "zhengjinwei" 4 2 6 getChildString "zhengjinwei" 3 2
案例3:迭代、数组
########################################################### <span style="font-size:24px;"># this is array test demo declare array_int=(1 2 3 4 5 6 7) printf "the int value in array_int which index is %d is %d\n\n" 0 ${array_int["0"]} echo "=============for loop==============================" declare array_int_len=${#array_int[*]} for (( i=0 ;i<"$array_int_len";i++ )) do echo -e ${array_int[$i]} done echo "" echo "============for iterator loop================================" for value in ${array_int[@]} do echo -e $value done echo "" echo "==========while loop==================================" declare i=0 while (( $i<$array_int_len )) do echo -e ${array_int[$i]} let i=i+1 done ########################################################</span>
案例4:时间操作
function getNowtime() { local _date=$(date +%Y-%m-%d:%H-%M-%S:%A) echo $_date } function getCostTime() { local _start=$(date +%S) sleep 2 local _end=$(date +%S) local _cost=$(( _end - _start )) echo $_cost } function getDiffTimeBetweenInSeconds() { local _start=$(date +%s -d '2010-09-01 17:00:00') local _end=$(date +%s -d '2011-09-01 18:40:40') tep=$(($_end-$_start)) tep2=$(( 3600*24 )) result=$(( $tep/$tep2 )) extra=$[ $tep%$tep2 ] declare _hours=0 declare _seconds=0 if [ $extra -ge 3600 ] ; then _hours=$(( $extra/3600 )) _seconds=$(( $extra%3600)) else _hours=0 _seconds=$extra fi printf "%d days ---%d hours ----%d seconds \n\n" $result $_hours $_seconds } getNowtime getCostTime getDiffTimeBetweenInSeconds
相关文章推荐
- nuix 系统操作命令大全
- UNIX系统操作命令大全
- Linux系统的常用操作命令大全
- adb操作命令详解及大全
- python写的批量操作远程主机脚本(命令执行,上传、下载文件)
- unix系统操作命令大全
- UNIX系统操作命令大全
- 使用source与点命令执行Linux shell脚本的体会
- LINUX Shell脚本中点号和source命令
- Linux系统的常用操作命令大全
- 自动ftp脚本[ftp命令大全]
- 转载一篇Unix操作命令大全
- Oracle导入SQL脚本执行和常用命令大全
- 用命令来操作vSphere:二十五 五大必备vSphere PowerCLI脚本(附下载地址)
- Linux Shell脚本ldd命令原理及使用方法
- powerbuilder操作excel命令大全
- vbs脚本大全,配有实例 DOS命令,批处理 脚本 代码
- SQL操作命令大全
- Monkeyrunner 写脚本的技巧(shell 命令,文件操作)
- mongodb操作命令------shell脚本启动-插入