linux shell 例子,Linux shell 示例(一)

一、环境

系统:Centos6.6 x64

shell:bash、sh

[centos@Shell ~]$ hostname

Shell

[centos@Shell ~]$ lsb_release -a

LSB Version: :base-4.0-amd64:base-4.0-noarch:core-4.0-amd64:core-4.0-noarch:graphics-4.0-amd64:graphics-4.0-noarch:printing-4.0-amd64:printing-4.0-noarch

Distributor ID: CentOS

Description: CentOS release 6.6 (Final)

Release: 6.6

Codename: Final

[centos@Shell ~]$ echo $SHELL

/bin/bash

[centos@Shell ~]$ ifconfig eth0 |grep "t addr"| awk -F '[: ]+' '{print $4}'

192.168.101.110

二、脚本示例

1、终端打印

#!/bin/bash

#The printf of Linux termnal,Include "echo,printf"

#author by woon

echo "The shell\`s name is $0"

#examples of echo,printf

#默认打印。输出换行

echo "Hello World!"

echo 'Hello World!'

#echo -n参数接受不换行输出

echo -n "Hello World"

echo "Hello World"

#-e参数接受双引号内字符串的转移列表

echo "Hello\tWorld!"

echo -e "Hello\tWorld!"

#printf使用文本或由空格分割的参数,可指定输出的宽度、对其方式等,可以格式化输出,默认情况下,printf不输出换行

printf "%-5s %-10s %-4s\n" No. NAME Mark

printf "%-5s %-10s %-4s\n" 1 Lee 80

printf "%-5s %-10s %-4.2f\n" 2 Woon 90.456

printf "%-5s %-10s %-4.2f\n" 3 James 85.654321

printf "%-5s %-10s %-5.3f\n" 4 Jeff 85.123789

运行结果:

The shell`s name is shell_print.sh

Hello World!

Hello World!

Hello WorldHello World

Hello World!

-e Hello World!

No. NAME Mark

1 Lee 80

2 Woon 90.46

3 James 85.65

4 Jeff 85.124

2、环境变量

#!/bin/bash

#author by woon

#env#获取全局环境变量

env > /tmp/env_$(date +%Y%m%d).$$

head -n 10 /tmp/env_$(date +%Y%m%d).$$

echo "^^^^^^^^^^^^^^^^^分隔符^^^^^^^^^^^^^^^^^^^^^^"

#获取单个全局变量的值

echo $PATH

echo "^^^^^^^^^^^^^^^^^分隔符^^^^^^^^^^^^^^^^^^^^^^"

#自定义变量和自定义全局变量,删除环境变量

var="Hello World"

echo -n "$var"

echo -n $var

echo ${var}

#设置全局环境变量

export VAR

#获取当前的shell版本

echo $SHELL

#or

echo $0

#获取变量的长度

var="Hello World"

echo ${#var}

#变量内容的删除

path=$PATH

echo $path

echo "最短删除,非贪婪模式,由前向后删除"

var1=${path#/*:}

echo $var1

echo "贪婪模式,删除匹配到最长的,由前向后删除"

var2=${path##/*:}

echo $var2

echo "非贪婪模式%,由后向前删除"

var3=${path%:*bin}

echo $var3

echo "贪婪模式,由后向前删除最长的"

var4=${path%%:*bin}

echo $var4

3、shell数学运算

#!/bin/bash

#author by woon.

#数学运算

sum_jia=0

sum_cheng=1

i=1

while [ $i -le 10 ];

do

let "sum_jia+=i"

let "sum_cheng*=i"

let "i += 2"

done

sum_jian1=$[ sum_cheng - sum_jia]

sum_jian2=$(( sum_cheng - sum_jia ))

echo $sum_jia

echo $sum_cheng

echo $sum_jian1

echo $sum_jian2

运行结果

$ ./shell_num.sh

25

945

920

920

4、文件查找和文件列表几操作

#!/bin/bash

#author by woon

#find 可以基于名字、类型、时间、大小、目录深度、大小权限、用户等查找并执行动作

#example

sudo find /etc/ -iregex ".*\(\.py\|\.sh\)" -type f -atime -1 -user root -size -2k -perm 644 -print

#sudo find /etc/ -iregex ".*\(\.py\|\.sh\)" -type f -atime -1 -user root -size -2k -perm 644 -delete

sudo find /etc/ -iregex ".*\(\.py\|\.sh\)" -type f -atime -1 -user root -size -2k -perm 644 -exec ll {} \;

sudo find /etc/ -iregex ".*\(\.py\|\.sh\)" -type f -atime -1 -user root -size -2k -perm 644 -print0 | xargs -0 ls -l

5、tr转换

tr参数属于集合映射关系

tr删除

[centos@Shell scripts]$ uuidgen | tr -d [a-z]

37643-2050-48-25-547825

tr替换

本替换其实值将0-a、1-b …… 9-j的映射关系替换

[centos@Shell scripts]$ echo | md5sum |tr [0-9] [a-z]

gibdcjdajijdedeajjchdiadfcbjcjea -

6、校验和核实

md5sum

[centos@Shell scripts]$ ls

dialog_t.md5 os_monitor-dialog.sh shell_print.sh test.sh

dialog_t.sh pcpu_usage.sh shell_search.sh top10_commands.sh

inpath shell_num.sh shell_varable.sh

[centos@Shell scripts]$ md5sum os_monitor-dialog.sh >os_monitor-dialog.md5

[centos@Shell scripts]$ md5sum -c os_monitor-dialog.md5

os_monitor-dialog.sh: 确定

脚本示例

#!/bin/bash

#为/etc/passwd生成一个MD5并校验,在生成MD5前检查/etc/passwd文件是否已经被排过序

sort -C /etc/passwd

if [ $? -eq 0 ]; then

echo "Sorted"

else

echo "Unsorted!"

md5sum /etc/passwd > passwd.md5

fi

md5sum -c passwd.md5

7、生成随机数

$RANDOM生成一个随机数;date +%s%N获取随机数字字符串

脚本如下:

#!/bin/bash

#创建example目录,在该目录下批量生成10个日志文件,日志文件名包含10个随机小写字母和固定字符串example;当前用户不具有权限,需要设置权限

if [ ! -d /example ]; then

echo "centos"|sudo -S mkdir /example

echo "centos" | sudo -S chown centos:centos /example -R

fi

#产生随机数$RANDOM,或uuidgen命令或者用MD5sum,然后替换

for num in $(seq 1 10)

do

touch /example/$(echo $RANDOM | md5sum |tr "0-9" "a-z"|cut -c 1-10)_example.log

done

8、根据扩展名切分文件名

#!/bin/bash

#shell name:shell_split.sh

#切分文件名并批量重命名或移动,比如图形文件等

num=1;

for img in *.jpg *.img

do

mv $img image-$num.{img##*.} 2>/dev/null

if [ $? -eq 0 ]; then

echo "Rename $img to image-$num.${img##*.}"

let num++

fi

done

9、列举文件类型统计信息

给出一个路径,统计该路径下各文件类型的数量

#!/bin/bash

#shell name:shell_filestat.sh

#列举文件类型数量

if [ $# -ne 1 ];

then

echo -e "$0 path\nexample:$0 /etc/"

else

path=$1

declare -A array;

while read line;

do

#echo $(file -b $line)

ftype=$(file -b $line)

let array["$ftype"]++;

done<

fi

echo ========== File types and counts ===========

for ftype in "${!array[@]}"

do

echo $ftype : ${array["$ftype"]}

done

10、判断当前系统是否支持该命令

#!/bin/sh

in_path()

{

cmd=$1 path=$2 retval=1

oldIFS=$IFS IFS=":"

for directory in $path

do

if [ -x $directory/$cmd ] ; then

retval=0

fi

done

IFS=$oldIFS

return $retval

}

checkForCmdInPath()

{

var=$1

if [ "$var" != "" ] ; then

if [ "${var%${var#?}}" = "/" ] ; then

if [ ! -x $var ] ; then

return 1

fi

elif ! in_path $var $PATH ; then

return 2

fi

fi

}

if [ $# -ne 1 ] ; then

echo "Usage: $0 command" >&2 ; exit 1

fi

checkForCmdInPath "$1"

case $? in

0 ) echo "$1 found in PATH" ;;

1 ) echo "$1 not found or not executable" ;;

2 ) echo "$1 not found in PATH" ;;

esac

exit 0

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Shell脚本高级编程教程,希望对你有所帮助。 Example 10-23. Using continue N in an actual task: 1 # Albert Reiner gives an example of how to use "continue N": 2 # --------------------------------------------------------- 3 4 # Suppose I have a large number of jobs that need to be run, with 5 #+ any data that is to be treated in files of a given name pattern in a 6 #+ directory. There are several machines that access this directory, and 7 #+ I want to distribute the work over these different boxen. Then I 8 #+ usually nohup something like the following on every box: 9 10 while true 11 do 12 for n in .iso.* 13 do 14 [ "$n" = ".iso.opts" ] && continue 15 beta=${n#.iso.} 16 [ -r .Iso.$beta ] && continue 17 [ -r .lock.$beta ] && sleep 10 && continue 18 lockfile -r0 .lock.$beta || continue 19 echo -n "$beta: " `date` 20 run-isotherm $beta 21 date 22 ls -alF .Iso.$beta 23 [ -r .Iso.$beta ] && rm -f .lock.$beta 24 continue 2 25 done 26 break 27 done 28 29 # The details, in particular the sleep N, are particular to my 30 #+ application, but the general pattern is: 31 32 while true 33 do 34 for job in {pattern} 35 do 36 {job already done or running} && continue 37 {mark job as running, do job, mark job as done} 38 continue 2 39 done 40 break # Or something like `sleep 600' to avoid termination. 41 done 42 43 # This way the script will stop only when there are no more jobs to do 44 #+ (including jobs that were added during runtime). Through the use 45 #+ of appropriate lockfiles it can be run on several machines 46 #+ concurrently without duplication of calculations [which run a couple 47 #+ of hours in my case, so I really want to avoid this]. Also, as search 48 #+ always starts again from the beginning, one can encode priorities in 49 #+ the file names. Of course, one could also do this without `continue 2', 50 #+ but then one would have to actually check whether or not some job 51 #+ was done (so that we should immediately look for the next job) or not 52 #+ (in which case we terminate or sleep for a long time before checking 53 #+ for a new job).

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值