Shell脚本初级练习篇

脚本1

作用:创建10个1M的文件

[root@pythonscript]#catmake_file.sh#!/bin/bash#foriin$(seq110);doddif=/dev/zeroof=/data/test/test"${i}"bs=1Mcount=1done

脚本2

作用:移走/data/test目录下大于100K的普通文件到/tmp目录下

[root@pythonscript]#catfile_mv.sh#!/bin/bash#find/data/test-typef-size+100k|xargs-imv{}/tmp

脚本3

作用:删除/tmp目录下包含test的任意字符且大小大于100K的文件

[root@pythonscript]#catfile_rm.sh#!/bin/bash#find/tmp-name"test*"-typef-size+100k|xargs-irm-f{}

脚本4

结合continue,break的for循环示例

[root@pythonscript]#catfor.sh#!/bin/bash#forloopegsforIin{1..10};doif[[$I-eq6]];thenecho"sixsixsix"continueelif[[$I-eq9]];thenecho"byebye9"breakfiecho$Idone

脚本5

简单while循环示例

[root@pythonscript]#catwhile.sh#!/bin/bash#whileloopegsNUM=5while[[$NUM-gt0]];doecho$NUMletNUM-=1done

脚本6

简单until循环示例

[root@pythonscript]#catuntil.sh#!/bin/bash#untilloopegs#NUM=5until[[$NUM-lt0]];doecho$NUMletNUM-=1done

脚本7

结合位置参数的case语句用法

[root@pythonscript]#catcase.sh#!/bin/bash#caseloopegs#VAR=$1case$VARinneo)echohacker;;sternberg)echorigorous;;michael)echocreative;;*)echounknow;;esac

脚本8

function函数示例

[root@pythonscript]#catfunction.sh#!/bin/bash#functionegs##1stfunctionfunctionhi(){echo"Hi,youarebeautiful!"}#sencondfunctionhello(){echo-e"JunLeisays\"hellothankyou\""}hihello