简单的脚本范文(优选3篇)

山崖发表网范文2023-03-24 17:31:09138

简单的脚本范文 第1篇

以下是重启Linux下某进程的shell脚本,以tomcat进程为例:

#!/bin/shpid=`ps -ef|grep tomcat|grep -v grep|awk '{print $2}'`if [ “$pid” = “” ] ; then echo “tomcat service does not start!”else kill -9 $pid pid1=`ps -ef|grep tomcat|grep -v grep|awk '{print $2}'` if [ “$pid1” = “” ] ; then echo “Successfully kill tomcat processes: ” $pid else echo “tomcat kill process failed!” exit 1 fifirm -rf /opt/tomcat/work/*./`ps -ef|grep tomcat|grep -v grep|awk '{print $2}'`if [ “$pid2” = “” ] ; then echo “tomcat service failed to start!”else echo “tomcat service starts successfully:” $pid2fi

简单的脚本范文 第2篇

#!/bin/sh

# -- 一个看起来像是bc的前端的命令行计算器

scale=2

show_help

cat << EOF

In addition to standard math function, calc also supports

a % b  remainder of a/b

a ^ b  exponential: a raised to the b power

s(x)  sine of x, x in radians

c(x)  cosine of x, x in radians

a(x)  actangent of x, returns radians

l(x)  natural log of x

e(x)  exponential log of raising e to the x

j(n, x) bessel function of integer order n of x

scale N show N fractional digits(default = 2)

EOF

if [ $# -gt 0 ]; then

exec “$@”

echo “Calc - a simple calculator. Enter 'help' for help, 'quit' to quit.”

echo -n “calc> ”

while read command args  # 像不像Python的顺序解包

case $command in

quit|exit) exit 0;;

help|?) show_help;;

scale)  scale=$args;;

*)     -p $scale “$command” “$args”;;

esac

echo -n “calc> ”

done

echo “”

exit 0

脚本如何运行:

可能这个脚本最有意思的部分就是那个while循环了,

交互式计算器脚本

它创建一个calc>的提示,直到用户完成输入。当然,这个脚本的间接性成就了它自己:shell脚本并不需要特别的复杂。

运行脚本:

这个脚本跑起来非常简单,因为它是一个交互式的,可以提示用户完成特定操作。如果有参数传递给它,它就转而把这些参数传给。

运行结果:

calc 150 /

Calc - a simple calculator. Enter 'help' for help, 'quit' to quit.

calc> help

In addition to standard math function, calc also supports

a % b  remainder of a/b

a ^ b  exponential: a raised to the b power

s(x)  sine of x, x in radians

c(x)  cosine of x, x in radians

a(x)  actangent of x, returns radians

l(x)  natural log of x

e(x)  exponential log of raising e to the x

j(n, x) bessel function of integer order n of x

scale N show N fractional digits(default = 2)

calc> 54354 ^ 3

160581137553864

calc> quit

简单的脚本范文 第3篇

1111111111111清除/var/log下messages日志文件的简单命令脚本

/var/log/下日志文件时系统文件,必须有root权限:

$UID是系统的全局变量:

make && make install 表示前面成功了,执行后面的

make || make install 表示前面不成功,执行后面的

清空日志的三种方法:适合保留文件,清空内容的场合

①echo “ ”>或echo >

②>

③cat /dev/null >

22222222222:shell脚本的基础知识

shell是弱类型语言:(语法宽松,不严格)较为通用

通用的shell有标准的Bourne shll(sh)和c shell(csh)其中Bourne shell(sh)已经被bash shell取代

shell脚本的建立:

tac /etc/ 将文件倒着显示

相当于:head -1 /etc/

①脚本开头

一个规范的shell脚本的第一行会指出由哪个程序(解释器)来执行脚本中的内容,在

333333333333linux bash 编程中一般为:

①解释器

#!/bin/bash

#!/bin/sh

②在linux下sh 相当于bash,

#!又称幻数,在执行bash脚本的时候,内核会根据它来确定用哪个程序来解释脚本中的内容

,这一行必须在脚本顶端的第一行,如果不是第一行,则为注释

Centos.和RedHat linux下默认的shell均为bash

④如果脚本的开头不指定解释器,那么,就要用对应的解释器来执行脚本

脚本注释

脚本的执行

shell脚本的执行

当shell脚本以非交互的方式运行时,它会先查找环境变量ENV,该变量指定了一个,环境文件(通常是.bashrc),然后从改环境变量文件开始执行,当读取了ENV文件后,SHELL才开始执行shell脚本中的内容,

shell脚本学习

Shell脚本的执行通常可以采用以下三种方式:

①bash script-name或 sh script-name(推荐使用)

②path/script-name或./script-name(当前路径下执行脚本)

③source script-name或. script-name# 注意点号

第一种方法是当脚本文件本身没有可执行权限(即文件X位为-号)时常使用的方法,这里推荐用bash执行,或者文件开头没有指定解释器,

第二种方法需要先将脚本文件的权限改为可执行(即文件加X位),具体方法:chmod u+x

script-name 或者chmod 755 script-name然后通过脚本路径,就可以直接执行脚本了

第三种方法通常是使用source或者“.” 号读入或加载指定的shell脚本文件语句,,然后,依次执行指定shell脚本文件中的所有。语句将作为当前父shell脚本进程的一部分运行,因此,使用source或者“.”点号等的可以将自身脚本中的变量的值,或者函数等的返回值传递到当前的父shell脚本中使用,这是第三种方法和前两种方法的最大区别

通过source 或“.” 点号加载执行过的脚本,在脚本结束后脚本中的变量(包括函数)值,在当前shell中依然存在,而sh和bash则不行,因此,在做shell脚本开发时。如果脚本中有需求引用其他脚本的内容,或者配置文件时,做好用“.” 点号或者source在脚本开头加载改脚本或配置文件,然后在下面的内容用可以调用source加载的脚本及文件中的变量及函数等内容。

笔试题:

已知如下命令及返回结果,请问echo $user的返回的结果为

[oldboy@test~]cat

[oldboy@test~]user=`whoami`

[oldboy@test~]sh

显示全文

注:本文部分文字与图片资源来自于网络,转载此文是出于传递更多信息之目的,若有来源标注错误或侵犯了您的合法权益,请立即后台留言通知我们,情况属实,我们会第一时间予以删除,并同时向您表示歉意

点击下载文档

文档为doc格式

发表评论

评论列表(7人评论 , 39人围观)

点击下载
本文文档