变量:
set 显示当前用户所有变量:
非常多,以下只是一部分展示。
mac_addresses ()
{
local re='\([A-Fa-f0-9]\{2\}:\)\{5\}[A-Fa-f0-9]\{2\}';
local PATH="$PATH:/sbin:/usr/sbin";
COMPREPLY+=($( { ifconfig -a || ip link show; } 2>/dev/null | sed -ne "s/.*[[:space:]]HWaddr[[:space:]]\{1,\}\($re\)[[:space:]].*/\1/p" -ne "s/.*[[:space:]]HWaddr[[:space:]]\{1,\}\($re\)[[:space:]]*$/\1/p" -ne "s|.*[[:space:]]\(link/\)\{0,1\}ether[[:space:]]\{1,\}\($re\)[[:space:]].*|\2|p" -ne "s|.*[[:space:]]\(link/\)\{0,1\}ether[[:space:]]\{1,\}\($re\)[[:space:]]*$|\2|p"
));
COMPREPLY+=($( { arp -an || ip neigh show; } 2>/dev/null | sed -ne "s/.*[[:space:]]\($re\)[[:space:]].*/\1/p" -ne "s/.*[[:space:]]\($re\)[[:space:]]*$/\1/p" ));
COMPREPLY+=($( sed -ne "s/^[[:space:]]*\($re\)[[:space:]].*/\1/p" /etc/ethers 2>/dev/null ));
COMPREPLY=($( compgen -W '${COMPREPLY[@]}' -- "$cur" ));
__ltrim_colon_completions "$cur"
}
_minimal ()
{
local cur prev words cword split;
_init_completion -s || return;
$split && return;
_filedir
}
_modules ()
{
local modpath;
modpath=/lib/modules/$1;
COMPREPLY=($( compgen -W "$( command ls -RL $modpath 2>/dev/null | sed -ne 's/^\(.*\)\.k\{0,1\}o\(\.[gx]z\)\{0,1\}$/\1/p' )" -- "$cur" ))
}
_ncpus ()
{
local var=NPROCESSORS_ONLN;
[[ $OSTYPE == *linux* ]] && var=_$var;
local n=$( getconf $var 2>/dev/null );
printf %s ${n:-1}
}
_parse_help ()
{
eval local cmd=$( quote "$1" );
local line;
{
case $cmd in
-)
cat
;;
*)
LC_ALL=C "$( dequote "$cmd" )" ${2:---help} 2>&1
;;
esac
} | while read -r line; do
[[ $line == *([ ' '])-* ]] || continue;
while [[ $line =~ ((^|[^-])-[A-Za-z0-9?][[:space:]]+)\[?[A-Z0-9]+\]? ]]; do
line=${line/"${BASH_REMATCH[0]}"/"${BASH_REMATCH[1]}"};
done;
__parse_options "${line// or /, }";
done
}
位置参量(其实就是执行脚本后面加上参数):
写一个简单的脚本:
[root@localhost scrips-test]# cat test.sh
dd
[root@localhost scrips-test]# chomd u+x test.sh
bash: chomd: 未找到命令...
相似命令是: 'chmod'
[root@localhost scrips-test]# chmod u+x test.sh
[root@localhost scrips-test]#
[root@localhost scrips-test]#
[root@localhost scrips-test]# ll
总用量 4
-rwxr--r--. 1 root root 3 5月 26 00:46 test.sh
[root@localhost scrips-test]#
用editplus或notepad++ 连接,写脚本比较方便:
test.sh 文件内容 echo "hello shell"
执行:
[root@localhost scrips-test]# ./test.sh
hello shell
[root@localhost scrips-test]#
注意:脚本里没有加前缀: 也打印出来了,也可以执行。
加上后:
#!/bin/bash
echo "hello shell"
执行效果:
[root@localhost scrips-test]#
[root@localhost scrips-test]# ./test.sh
hello shell
[root@localhost scrips-test]# sh test.sh
hello shell
[root@localhost scrips-test]# . test.sh
hello shell
[root@localhost scrips-test]#
再把脚本改为:
#!/bin/bash
echo "hello shell"
echo "hello ${1}"
echo "hello ${1} $2"
再次执行:
[root@localhost scrips-test]# ./test.sh hadoop
hello shell
hello hadoop
hello hadoop
[root@localhost scrips-test]# ./test.sh hadoop spark
hello shell
hello hadoop
hello hadoop spark
[root@localhost scrips-test]#
如果参数中有空格,需要引号引起来。
[root@localhost scrips-test]# ./test.sh hadoop "hdp mapreduce"
hello shell
hello hadoop
hello hadoop hdp mapreduce
[root@localhost scrips-test]#
数组:
[root@localhost scrips-test]# arr={zhangsan lisi wangwu}
bash: lisi: 未找到命令...
[root@localhost scrips-test]# arr=(zhangsan lisi wangwu)
[root@localhost scrips-test]# echo arr
arr
[root@localhost scrips-test]# echo ${arr[0]}
zhangsan
[root@localhost scrips-test]# echo ${arr[1]}
lisi
[root@localhost scrips-test]# echo ${arr[3]}
[root@localhost scrips-test]# echo ${arr[2]}
wangwu
[root@localhost scrips-test]#
[root@localhost scrips-test]# echo ${arr[*]}
zhangsan lisi wangwu
[root@localhost scrips-test]# echo ${#arr[*]}
3
[root@localhost scrips-test]# arr[0]=huaheshang
[root@localhost scrips-test]# echo ${arr[0]}
huaheshang
[root@localhost scrips-test]#
日期 date:
man命令查看帮助。
DATE(1) FSF DATE(1)
NAME
date - 打印或设置系统日期和时间
总览
date [选项]... [+格式] []里的选项是可选项,后面加一些标准的格式(下面)
date [选项] [MMDDhhmm[[CC]YY][.ss]]
描述
根据指定格式显示当前时间或设置系统时间.
-d, --date=STRING
显示由 STRING 指定的时间, 而不是当前时间
-f, --file=DATEFILE
显示 DATEFILE 中每一行指定的时间, 如同将 DATEFILE 中的每行作为 --date 的参数一样
-I, --iso-8601[=TIMESPEC] 按照 ISO-8601 的日期/时间格式输出时间.
TIMESPEC=`date' (或者不指定时)仅输出日期,等于 `hours', `minutes', 或`seconds' 时按照指定精度输出日期及时间.
-r, --reference=FILE
显示 FILE 的最后修改时间
-R, --rfc-822
根据 RFC-822 指定格式输出日期
-s, --set=STRING
根据 STRING 设置时间
-u, --utc, --universal
显示或设置全球时间(格林威治时间)
--help 显示本帮助文件并退出
--version
显示版本信息并退出
格式 FORMAT 控制着输出格式. 仅当选项指定为全球时间时本格式才有效。 分别解释如下: /上面说的加的格式 就是如下这些/
%% 文本的 %
%a 当前区域的星期几的简写 (Sun..Sat)
%A 当前区域的星期几的全称 (不同长度) (Sunday..Saturday)
%b 当前区域的月份的简写 (Jan..Dec)
%B 当前区域的月份的全称(变长) (January..December)
%c 当前区域的日期和时间 (Sat Nov 04 12:02:33 EST 1989)
%d (月份中的)几号(用两位表示) (01..31)
%D 日期(按照 月/日期/年 格式显示) (mm/dd/yy)
%e (月份中的)几号(去零表示) ( 1..31)
%h 同 %b
%H 小时(按 24 小时制显示,用两位表示) (00..23)
%I 小时(按 12 小时制显示,用两位表示) (01..12)
%j (一年中的)第几天(用三位表示) (001..366)
%k 小时(按 24 小时制显示,去零显示) ( 0..23)
%l 小时(按 12 小时制显示,去零表示) ( 1..12)
%m 月份(用两位表示) (01..12)
%M 分钟数(用两位表示) (00..59)
%n 换行
%p 当前时间是上午 AM 还是下午 PM
%r 时间,按 12 小时制显示 (hh:mm:ss [A/P]M)
%s 从 1970年1月1日0点0分0秒到现在历经的秒数 (GNU扩充)
%S 秒数(用两位表示)(00..60)
%t 水平方向的 tab 制表符
%T 时间,按 24 小时制显示(hh:mm:ss)
%U (一年中的)第几个星期,以星期天作为一周的开始(用两位表示) (00..53)
%V (一年中的)第几个星期,以星期一作为一周的开始(用两位表示) (01..52)
%w 用数字表示星期几 (0..6); 0 代表星期天
%W (一年中的)第几个星期,以星期一作为一周的开始(用两位表示) (00..53)
%x 按照 (mm/dd/yy) 格式显示当前日期
%X 按照 (%H:%M:%S) 格式显示当前时间
%y 年的后两位数字 (00..99)
%Y 年(用 4 位表示) (1970...)
%z 按照 RFC-822 中指定的数字时区显示(如, -0500) (为非标准扩充)
%Z 时区(例如, EDT (美国东部时区)), 如果不能决定是哪个时区则为空
默认情况下,用 0 填充数据的空缺部分. GNU 的 date 命令能分辨在 `%'和数字指示之间的以下修改.
`-' (连接号) 不进行填充 `_' (下划线) 用空格进行填充
BUG报告
请向<bug-sh-utils@gnu.org>报告BUG.
参考
关于 date 的详细说明是个 Texinfo 手册. 如果在你的计算机上已经成功安装了 info 和 date 程序,你可以使用
info date
命令访问完全手册.
版权
Copyright © 1999 Free Software Foundation, Inc. This is free software; see the source for copying conditions. There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
[中文版维护人]
liguoping <email>
[中文版最新更新]
2001/07/15
《中国linux论坛man手册页翻译计划》:
http://cmpp.linuxforum.net
GNU sh-utils 2.0
如何使用呢?举例如下:
root@localhost scrips-test]# date
2019年 05月 26日 星期日 01:49:40 CST
[root@localhost scrips-test]#
[root@localhost scrips-test]# date +%Y-%M-%D
2019-50-05/26/19
[root@localhost scrips-test]# date +%Y-%M-%d
2019-50-26
[root@localhost scrips-test]# date +%Y-%m-%d
2019-05-26
[root@localhost scrips-test]# date +%Y/%m/%dT%H:%M:%S
2019/05/26T01:51:41
[root@localhost scrips-test]#
将脚本改为:
#!/bin/bash
echo "hello shell"
echo "hello ${1}"
echo "hello ${1} $2"
echo "========date test=========="
date=$(date)
echo ${date}
date1=$(date +%Y-%m-%d)
echo ${date1}
date2=$(date --date='2 days ago' +%Y%m%d)
echo ${date2}
date3=$(date --date='-1 days ago' +%Y-%m-%d)
echo ${date3}
date4=$(date --date='-1 days' +%Y-%m-%d)
echo ${date4}
date5=$(date --date='1 days' +%Y-%m-%d)
echo ${date5}
执行效果如下:
[root@localhost scrips-test]# ./test.sh
hello shell
hello
hello
========date test==========
2019年 05月 26日 星期日 02:07:40 CST
2019-05-26
20190524
2019-05-27
2019-05-25
2019-05-27
[root@localhost scrips-test]#
判断表达式及if语句使用
for循环
将上面的脚本改为:
#!/bin/bash
echo "hello shell"
echo "hello ${1}"
echo "hello ${1} $2"
echo "========date test=========="
date=$(date)
echo ${date}
date1=$(date +%Y-%m-%d)
echo ${date1}
date2=$(date --date='2 days ago' +%Y%m%d)
echo ${date2}
date3=$(date --date='-1 days ago' +%Y-%m-%d)
echo ${date3}
date4=$(date --date='-1 days' +%Y-%m-%d)
echo ${date4}
date5=$(date --date='1 days' +%Y-%m-%d)
echo ${date5}
echo "========for循环=========="
for var in 1 2 3 4 5
do
echo ${var}
done
echo "==========================="
num=10
s=0
for((i=0;i<${num};i=i+1))
do
s=$((${s}+${i}))
done
echo ${s}
echo "========while循环=========="
执行效果为:
[root@Master scrips-test]# ./test.sh
hello shell
hello
hello
========date test==========
2019年 05月 26日 星期日 14:10:50 CST
2019-05-26
20190524
2019-05-27
2019-05-25
2019-05-27
========for循环==========
1
2
3
4
5
===========================
45
========while循环==========
[root@Master scrips-test]#
while循环
新建文件testfile.tang,内容如下:
hadoop~~~
spark~~~~~
kafka~~~
zookeeper~~~
hive~~
hbase~~
脚本内添加:
echo "========while循环=========="
testfile=/opt/scrips-test/testfile.tang
cat ${testfile} | while read line
do
echo${line}
done
执行 脚本报错:
========while循环==========
./test.sh:行49: echohadoop~~~: 未找到命令
./test.sh:行49: echospark~~~~~: 未找到命令
./test.sh:行49: echokafka~~~: 未找到命令
./test.sh:行49: echozookeeper~~~: 未找到命令
./test.sh:行49: echohive~~: 未找到命令
./test.sh:行49: echohbase~~: 未找到命令
[root@Master scrips-test]#
原因是 我循环体内的echo后未加空格:
修改后:
#!/bin/bash
echo "hello shell"
echo "hello ${1}"
echo "hello ${1} $2"
echo "========date test=========="
date=$(date)
echo ${date}
date1=$(date +%Y-%m-%d)
echo ${date1}
date2=$(date --date='2 days ago' +%Y%m%d)
echo ${date2}
date3=$(date --date='-1 days ago' +%Y-%m-%d)
echo ${date3}
date4=$(date --date='-1 days' +%Y-%m-%d)
echo ${date4}
date5=$(date --date='1 days' +%Y-%m-%d)
echo ${date5}
echo "========for循环=========="
for var in 1 2 3 4 5
do
echo ${var}
done
echo "==========================="
num=10
s=0
for((i=0;i<${num};i=i+1))
do
s=$((${s}+${i}))
done
echo ${s}
echo "========while循环=========="
testfile=/opt/scrips-test/testfile.tang
cat ${testfile} | while read line
do
echo ${line}
done
执行结果:
[root@Master scrips-test]# ./test.sh
hello shell
hello
hello
========date test==========
2019年 05月 26日 星期日 14:34:20 CST
2019-05-26
20190524
2019-05-27
2019-05-25
2019-05-27
========for循环==========
1
2
3
4
5
===========================
45
========while循环==========
hadoop~~~
spark~~~~~
kafka~~~
zookeeper~~~
hive~~
hbase~~
[root@Master scrips-test]#
然后 脚本添加:
echo "========while循环=========="
testfile=/opt/scrips-test/testfile.tang
cat ${testfile} | while read line
do
echo ${line}
done
testfiletest=$(cat "testfile" | sed 's/#.*$//;/^$/d')
echo ${testfiletest}
执行:
========while循环==========
hadoop~~~
spark~~~~~
kafka~~~
zookeeper~~~
hive~~
hbase~~
cat: testfile: 没有那个文件或目录
[root@Master scrips-test]#
报错原因是我cat "testfile" 这里没有加$符号,应改为cat "$testfile"
脚本如下:
#!/bin/bash
echo "hello shell"
echo "hello ${1}"
echo "hello ${1} $2"
echo "========date test=========="
date=$(date)
echo ${date}
date1=$(date +%Y-%m-%d)
echo ${date1}
date2=$(date --date='2 days ago' +%Y%m%d)
echo ${date2}
date3=$(date --date='-1 days ago' +%Y-%m-%d)
echo ${date3}
date4=$(date --date='-1 days' +%Y-%m-%d)
echo ${date4}
date5=$(date --date='1 days' +%Y-%m-%d)
echo ${date5}
echo "========for循环=========="
for var in 1 2 3 4 5
do
echo ${var}
done
echo "==========================="
num=10
s=0
for((i=0;i<${num};i=i+1))
do
s=$((${s}+${i}))
done
echo ${s}
echo "========while循环=========="
testfile=/opt/scrips-test/testfile.tang
cat ${testfile} | while read line
do
echo ${line}
done
testfiletest=$(cat "$testfile" | sed 's/#.*$//;/^$/d')
echo ${testfiletest}
执行效果如下:
[root@Master scrips-test]# ./test.sh
hello shell
hello
hello
========date test==========
2019年 05月 26日 星期日 14:42:23 CST
2019-05-26
20190524
2019-05-27
2019-05-25
2019-05-27
========for循环==========
1
2
3
4
5
===========================
45
========while循环==========
hadoop~~~
spark~~~~~
kafka~~~
zookeeper~~~
hive~~
hbase~~
hadoop~~~ spark~~~~~ kafka~~~ zookeeper~~~ hive~~ hbase~~
[root@Master scrips-test]#