ARTICLE DETAIL

资讯详情

深耕郑州网站建设与运营推广的一线实战洞察。

【2015-02-03】Linux Shell脚本简单实例

【2015-02-03】Linux Shell脚本简单实例 [历史归档]本文原发布于 cstriker1407.info 个人博客内容为历史存档仅供参考。发布时间2015-02-03 标题Linux Shell脚本简单实例分类操作系统 / linux 标签linux·shellLinux Shell脚本简单实例最近需要写一个shell脚本就简单的笔记下用到的语法。# /bin/shfunctionsimple_add()#函数不用定义参数列表{num1$1;# $1 表示第一个参数num2$2;return$((num1num2));#返回值直接用return}functionshow_watchdog(){pidspgrep watchdog;#执行shell命令可以用Tab键上面的键返回值直接用变量存起来。mypid$$;#查看当前进程idforpidin$pidsdoif[[$pid-ne$mypid]]thenechops -f -p $pid;ps-f-p$pid;fi;done;}if[[$1add]]thensimple_add$2$3;echo$2$3$?;# $? 可以获取返回值。elif[[$1watch]]thenshow_watchdog;elseechoinvalid input;fi;输出结果为cstriker1407cstriker1407-x64:~$chmodax simplemath.sh cstriker1407cstriker1407-x64:~$ ./simplemath.shadd45459cstriker1407cstriker1407-x64:~$ ./simplemath.shwatchps-f-p22UIDPIDPPIDC STIME TTY TIME CMD root22201月31 ? 00:00:01ps-f-p23UIDPIDPPIDC STIME TTY TIME CMD root23201月31 ? 00:00:01ps-f-p28UIDPIDPPIDC STIME TTY TIME CMD root28201月31 ? 00:00:01ps-f-p33UIDPIDPPIDC STIME TTY TIME CMD root33201月31 ? 00:00:01 cstriker1407cstriker1407-x64:~$ ./simplemath.sh hello invalid input
返回列表