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]#
如何使用呢?举例如下:
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]#
将上面的脚本改为:
#!/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]#
新建文件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]#