关键词:
[[email protected] ~]# vim a
#!/bin/bash
# Name:abc
# Description:Create script
# Author:mylinux
# Version:0.0.1
# Datatime:03/02/12 14:42:00
# Usage:mkscript FILENAME
cat >$1 <<EOF
#!/bin/bash
# Name: `basename $1`
# Description:
# Author: mylinux
# Version: 0.0.1
# Datatime: `date +‘%F %T‘`
# Usage: `basename $1`
EOF
vim +8 $1
---------------------
[[email protected] ~]# bash a test1.sh 运行此脚本自动生成
#!/bin/bash
# Name: test1.sh
# Description:
# Author: mylinux
# Version: 0.0.1
# Datatime: 2016-09-17 18:16:45
# Usage: test1.sh
-----------------------------------~
再判断如果有内容就直接打开 没内容才写入
[[email protected] ~]# vim a
#!/bin/bash
# Name:abc
# Description:Create script
# Author:mylinux
# Version:0.0.1
# Datatime:03/02/12 14:42:00
# Usage:mkscript FILENAME
if grep "[^[:space:]]" $1 &>/dev/null;then #grep非空白行
vim + $1 +表示打开最后一行
else
cat >$1 <<EOF
#!/bin/bash
# Name: `basename $1`
# Description:
# Author: mylinux
# Version: 0.0.1
# Datatime: `date +‘%F %T‘`
# Usage: `basename $1`
EOF
vim +8 $1
fi
---------------------------------------
保存退出后自动检查语法。
#!/bin/bash
# Name:abc
# Description:Create script
# Author:mylinux
# Version:0.0.1
# Datatime:03/02/12 14:42:00
# Usage:mkscript FILENAME
if grep "[^[:space:]]" $1 &>/dev/null;then
vim + $1
else
cat >$1 <<EOF
#!/bin/bash
# Name: `basename $1`
# Description:
# Author: mylinux
# Version: 0.0.1
# Datatime: `date +‘%F %T‘`
# Usage: `basename $1`
EOF
vim +8 $1
fi
until bash -n $1 &>/dev/null ;do
read -p "Syntax,error,q|Q for quiting, others for editing: " OPT
case $OPT in
q|Q)
echo "quit."
exit 1
;;
*)
vim + $1
;;
esac
done
chmod +x $1
----------------------------------------------
getopts:让脚本可以获取选项
[[email protected] ~]# bash a opttest.sh
#!/bin/bash
# Name: opttest.sh
# Description:
# Author: mylinux
# Version: 0.0.1
# Datatime: 2016-09-13 01:57:50
# Usage: opttest.sh
getopts "bd" OPT
echo $OPT
[[email protected] ~]# bash opttest.sh -b
b
[[email protected] ~]# bash opttest.sh -d
d
getopts 默认情况下只能获取一个选项
OPTARG:表示某一个变量的参数,需要带参数的选项后面必须带:
例如getopts "bd:" OPT
getopts "b:d:" OPT
#!/bin/bash
# Name: opttest.sh
# Description:
# Author: mylinux
# Version: 0.0.1
# Datatime: 2016-09-13 01:57:50
# Usage: opttest.sh
getopts "bd:" OPT
echo $OPT
echo $OPTARG
[[email protected] ~]# bash opttest.sh -d "haha"
d
haha
--------------------------------
在所有选项最前面输入:表示不显示错误信息
#!/bin/bash
getopts ":b:d:" OPT
echo $OPT
echo $OPTARG
[[email protected] ~]# bash opttest.sh -c
a b
c
-----------------------------
让一个脚本获取多个参数
#!/bin/bash
# Name: opttest.sh
# Description:
# Author: mylinux
# Version: 0.0.1
# Datatime: 2016-09-13 01:57:50
# Usage: opttest.sh
function USAGE {
echo "Usage: opttest.sh [-d argu] [-b argu]"
}
while getopts ":b:d:" RT;do
case $RT in
b) echo "The options is b."
echo $OPTARG
;;
d) echo "The options is d."
echo $OPTARG
;;
*) USAGE
;;
esac
done
-----------------------------------------------------------
[[email protected] ~]# bash a -d "hello" /tmp/a
OPTIND:选项索引,这里表示 -d , "hello" , /tmp/a这三个位置变量
-1代表不包含/tmp/a
shift $[$OPTIND-1]
最终完善版:
写一个脚本:用于创建脚本,并cat注释信息,并检查语法错误。如果语法错误则。提示用户是否打开修改
[[email protected] ~]#vim a
#!/bin/bash
# Name:abc
# Description:Create script
# Author:mylinux
# Version:0.0.1
# Datatime:03/02/12 14:42:00
# Usage:mkscript FILENAME
while getopts ":d:" OPTA;do
case $OPTA in
d)
DESC=$OPTARG
shift $[$OPTIND-1];;
*)
echo "Usage:mkscript [-d DESCRIPTION] FILENAME"
shift $[$OPTIND-1];;
esac
done
if ! grep "[^[:space:]]" $1 &>/dev/null;then
cat > $1 <<EOF
#!/bin/bash
# Name: `basename $1`
# Description: $DESC
# Author: mylinux
# Version: 0.0.1
# Datatime: `date +‘%F %T‘`
# Usage: `basename $1`
EOF
fi
vim + $1
until bash -n $1 &>/dev/null ;do
read -p "Syntax,error,q|Q for quiting, others for editing: " OPT
case $OPT in
q|Q)
echo "quit."
exit 1 ;;
*)
vim + $1;;
esac
done
chmod +x $1
-----------------------------------------------------------------------
getopts:
OPTARG
OPTIND
----------------------------------------
写一个脚本getinterface.sh,脚本可以接受选项(i,I,a)完成以下任务:
1.使用以下形式:getinterface.sh [-i interface|I IP|-a]
2.当用户使用-i选项时,显示其指定网卡的IP地址;
3.当用户使用-I选项时,显示其后面的IP地址所属的网络接口
#4.当用户单独使用-a选项时,显示所有网络接口及其IP地址(lo除外)
#!/bin/bash
# Name: getinterface.sh
# Description: Get ethernet information
# Author: mylinux
# Version: 0.0.1
# Datatime: 2016-09-13 07:34:56
# Usage: getinterface.sh
function SHOWIP {
if ! ifconfig|grep -o "^[^[:space:]]{1,}"|grep $1 &>/dev/null;then
return 13
fi
echo -n "${1}: "
ifconfig $1 |grep -o "inet addr:[0-9.]{1,}" |cut -d: -f 2 # {1,}表示引用前面至少1次
echo
}
SHOWETHER () {
if ! ifconfig |grep -o "inet addr:[0-9.]{1,}" |cut -d: -f 2|grep $1 &>/dev/null ;then
return 14
fi
echo -n "${1}: "
ifconfig |grep -B 1 "$1" |grep -o "^[^[:space:]]{1,}" #grep -B显示grep到的结果和结果的上一行
echo
}
USAGE() {
echo "getinterface.sh <-i interface|-I IP>"
}
while getopts ":i:I:" SWICH; do
case $SWICH in
i)
SHOWIP $OPTARG
[ $? -eq 13 ] && echo "Wrong ehtercard"
;;
I)
SHOWETHER $OPTARG
[ $? -eq 14 ] && echo "Wrong IP."
;;
*)
USAGE
;;
esac
done
----------------------------------
本文出自 “运维成长路” 博客,谢绝转载!
基于bash脚本自己开发ros的一键启动
...午节没啥事,左右苦思冥想,发现网上针对ROS的bash启动脚本几乎还是处于0的状态。为此针对性的给ROS开一个bash脚本的教程是非常有必要的。也希望各位大佬能提点意见,如果后续合适,我会继续根据各位的意见来继续开坑的。... 查看详情
在 bash 脚本的 if 语句中使用 traceroute
】在bash脚本的if语句中使用traceroute【英文标题】:Usingtracerouteinanifstatementinabashscript【发布时间】:2019-03-0818:00:34【问题描述】:我正在尝试根据traceroute是否从特定IP地址获得响应来执行代码。所以:iftraceroute123.456.78.9thenoption1el... 查看详情
收到错误mailx:非法选项-在bash脚本中添加附件时
】收到错误mailx:非法选项-在bash脚本中添加附件时【英文标题】:gettingerrormailx:illegaloption--awhileaddingattachmentinbashscripting【发布时间】:2020-06-1420:41:03【问题描述】:echo"$MAIL_MSG"|mailx-a"$opfile"-s"$MAIL_SUBJ""$DW_EMAIL_LIST"上面的命令抛... 查看详情
使用终端或 bash 脚本创建和写入 .plist
】使用终端或bash脚本创建和写入.plist【英文标题】:Creatingandwritinginto.plistwithTerminalorbashscript【发布时间】:2014-12-0912:57:12【问题描述】:我需要在安装后创建一个.plist文件,我可以使用的唯一选项是bash脚本。我必须使用bash脚... 查看详情
从 bash 脚本对文件中的 wget 命令运行 exec 会忽略 wget 选项
】从bash脚本对文件中的wget命令运行exec会忽略wget选项【英文标题】:Runningexeconwgetcommandsinafilefrombashscriptignoreswgetoptions【发布时间】:2014-03-2220:55:25【问题描述】:如果我运行这个shell脚本,我会得到exec导致wget表现得愚蠢。echo"w... 查看详情
将我的 bash 脚本变成守护进程的选项
】将我的bash脚本变成守护进程的选项【英文标题】:optiontoturnmybashscriptintoadaemon【发布时间】:2014-07-1710:10:21【问题描述】:我有一个运行良好的bash脚本来创建一些随机文件。它运行一个循环,创建随机bin文件,然后在睡眠时... 查看详情
为啥我不能在 bash 脚本中使用作业控制?
】为啥我不能在bash脚本中使用作业控制?【英文标题】:Whycan\'tIusejobcontrolinabashscript?为什么我不能在bash脚本中使用作业控制?【发布时间】:2010-10-1523:19:24【问题描述】:在thisanswer到另一个question,有人告诉我在您没有作业控... 查看详情
为啥我不能在 bash 脚本中使用作业控制?
】为啥我不能在bash脚本中使用作业控制?【英文标题】:Whycan\'tIusejobcontrolinabashscript?为什么我不能在bash脚本中使用作业控制?【发布时间】:2010-10-1523:19:24【问题描述】:在thisanswer到另一个question,有人告诉我在您没有作业控... 查看详情
如何使用 bash -c 调用 bash 脚本函数 [重复]
】如何使用bash-c调用bash脚本函数[重复]【英文标题】:Howtocallabashscriptfunctionusingbash-c[duplicate]【发布时间】:2021-06-0407:20:55【问题描述】:[更新]请注意,为了满足minimalreproducibleexample,这是本文的简化代码。脚本中会有更多的功... 查看详情
bash的执行结构续集
bash脚本编程(4) case选择分支结构: case:caseWORDin[PATTERN[|PATTERN]...)COMMANDS;;]...esac 在脚本中使用的case的结构: caseWORDin?//WORD应该是一个变量,若其不是变量,那么他只能匹配到其本身 PATTERN1)????//被匹配到的选项后面需要加一个... 查看详情
bash脚本小技巧之一:set-e和set-u(代码片段)
今天一朋友问我一个bash脚本在开头出现了set-u和set-e两行是什么作用,我也纳闷没见过这样的写法啊于是百度了一下,才发现原来这两个bash的选项这么有用,这边记录下方便以后查阅。set-e设置该选项后,当脚本中任何以一个命... 查看详情
在 bash 脚本中为 git 提供密码
】在bash脚本中为git提供密码【英文标题】:Providepassphrasetogitinbashscript【发布时间】:2016-03-1308:07:59【问题描述】:如何在bash脚本中使用gitfetch/pull提供密码。我真的需要在bash脚本中执行此操作,而不使用ssh-add或类似的东西。有... 查看详情
在 Bash 脚本中使用 Expect 为 SSH 命令提供密码
】在Bash脚本中使用Expect为SSH命令提供密码【英文标题】:UseExpectinaBashscripttoprovideapasswordtoanSSHcommand【发布时间】:2011-06-1410:22:42【问题描述】:我正在尝试在Bash脚本中使用expect来提供SSH密码。提供密码是可行的,但我并没有像... 查看详情
Bash & awk:尝试在 awk 中使用 bash 脚本参数时出现分段错误
】Bash&awk:尝试在awk中使用bash脚本参数时出现分段错误【英文标题】:Bash&awk:Segmentationfaultwhiletryingtousebashscriptarguementinawk【发布时间】:2021-12-2607:51:03【问题描述】:我正在尝试编写一个脚本来使用awk在两个时间戳之间提... 查看详情
如何从脚本本身中获取 Bash 脚本的源目录?
】如何从脚本本身中获取Bash脚本的源目录?【英文标题】:HowcanIgetthesourcedirectoryofaBashscriptfromwithinthescriptitself?【发布时间】:2010-09-0818:17:52【问题描述】:我如何获取Bash脚本所在目录的路径,inside该脚本?我想使用Bash脚本作... 查看详情
如何从脚本本身中获取 Bash 脚本的源目录?
】如何从脚本本身中获取Bash脚本的源目录?【英文标题】:HowcanIgetthesourcedirectoryofaBashscriptfromwithinthescriptitself?【发布时间】:2010-09-0818:17:52【问题描述】:我如何获得Bash脚本所在目录的路径,inside该脚本?我想使用Bash脚本作... 查看详情
如何从脚本本身中获取 Bash 脚本的源目录?
】如何从脚本本身中获取Bash脚本的源目录?【英文标题】:HowcanIgetthesourcedirectoryofaBashscriptfromwithinthescriptitself?【发布时间】:2010-09-0818:17:52【问题描述】:我如何获取Bash脚本所在目录的路径,inside该脚本?我想使用Bash脚本作... 查看详情
错误:在声明性 Jenkinsfile 中使用 Bash 脚本
】错误:在声明性Jenkinsfile中使用Bash脚本【英文标题】:Error:UsingBashscriptinsideDeclarativeJenkinsfile【发布时间】:2019-07-2615:02:11【问题描述】:在Jenkinsfile中编写bash脚本代码时我面临一些挑战。UserCase:我在一个GIT存储库的文件夹结... 查看详情