目录
1. 获取当前脚本路径
#!/bin/bash
# 获取本地目录
DIR=`dirname "$0"`
DIR=`cd "${DIR}" > /dev/null;pwd`
# 创建目录保存文件
target_dir=${DIR}/wyh_test
mkdir -p ${target_dir}
echo 'target_dir is ' ${target_dir}
# 删除本地文件
rm -rf ${target_dir}/*.txt
2. 多线程处理任务
#!/bin/bash
# 获取本地目录
DIR=`dirname "$0"`
DIR=`cd "${DIR}" > /dev/null;pwd`
# 创建目录保存文件
target_dir=${DIR}/wyh_test
mkdir -p ${target_dir}
echo 'target_dir is ' ${target_dir}
# 删除本地文件
rm -rf ${target_dir}/*.txt
# 遍历数据库目录
# 指定按行循环赋值
IFS=$'\n'
# 函数传参dbname 通过dbname查询空间及表信息 并写入文件
function demo_function()
{
var_01=$1
echo "开始执行:" $var_01
}
# 多进程执行 table_stat_func
tmp_fifofile="${target_dir}/my_temp_fifo.fifo"
mkfifo ${tmp_fifofile} # 新建一个fifo类型的文件
exec 6<>${tmp_fifofile} # 将fd6指向fifo类型
rm -r ${tmp_fifofile}
thread_num=3 # 最大可同时执行线程数量
# 根据线程总数量设置令牌个数
for ((i=0;i<$thread_num;i++));
do
echo
done >&6
for line in array; # 任务数量
do
# 一个 read -u6命令执行一次,就从fd6中减去一个回车符,然后向下执行,
# fd6中没有回车符的时候就停在这了,从而实现了线程数量控制
read -u6
# 可以把具体需要执行的命令封装成一个函数
{
echo "分配并发任务:"$line
demo_function $line
echo >&6
}&
done
wait ##等待所有子后台进程结束
# 关闭fd6管道
3. 数仓hive任务模板
#!/bin/bash
# 定义环境变量
APP=gmall
db=movie_info
hive=/root/bigdata/hive/bin/hive
# 定义时间和参数
if [ -n "$1" ] ; then
do_date=$1
else
do_date=`date -d "-1 day" +%F`
fi
echo "------------------今天是$do_date-----------------------"
# 定义sql语句
sql="
use $APP;
select * from $db;
"
# hive执行hql
$hive -e "$sql" >> /root/tmp/movie_info.log
4. 集群分发脚本xsync
#!/bin/bash
#1 获取输入参数个数,如果没有参数,直接退出
pcount=$#
if((pcount==0)); then
echo no args;
exit;
fi
#2 获取文件名称
p1=$1
fname=`basename $p1`
echo fname=$fname
#3 获取上级目录到绝对路径
pdir=`cd -P $(dirname $p1); pwd`
echo pdir=$pdir
#4 获取当前用户名称
user=`whoami`
#5 循环
for((host=101; host<109; host++)); do
echo ------------------- hadoop$host --------------
rsync -rvl $pdir/$fname $user@hadoop$host:$pdir
done
(b)修改脚本 xsync 具有执行权限
[wyh@hadoop102 bin]$ chmod 777 xsync
(c)调用脚本形式:xsync 文件名称
[wyh@hadoop102 bin]$ xsync /home/wyh/bin
注意:如果将xsync放到/home/wyh/bin目录下仍然不能实现全局使用,可以将xsync移动到/usr/local/bin目录下。
5.关联数组的定义
一般存储连接串相关信息,用函数(函数内加延,增强安全性)获取账号和密码
#!/bin/bash
declare -A arr
arr=( [name]=wyh [sex]=M
[age]=18
)
echo ${arr[age]}
function getparm(){
key=$1
value=key_${arr[${key}]}
echo ${value}
}
getparm name
6.数据重定向
#在Linux中利用shell实现数据流重定向是非常简单的,下面来介绍3种数据流的重定向。
#1)标准输入(stdin):代码为0,使用<或者<<
#2)标准输出(stdout):代码为1,使用>或者>>
#3)标准错误输出(stderr):代码为2,使用2>或者2>>
#注意在默认情况下,>和>>分别表示1>和1>>, 而<和<<分别表示0<和0<<。
# 例1
find /home -name .bashrc > list_right 2> list_error
# 例2 错误流写入垃圾桶黑洞设备
find /home -name .bashrc > list_right 2> /dev/null
# 例3 正确与错误数据统统写入一个文件中,& 代表转到的意思
find /home -name .bashrc > list 2>&1
or
find /home -name .bashrc &> list
# 介绍& 正确流转到错误流,所以都到错误流了
echo "错误日志" 2> /dev/null 1>&2
# 例4 用文本替换键盘输入
cat > catfile < ~/.bashrc
# 例5 指定输入结束符
cat > catfile << "eof"
7.网络服务检测模板
#!/bin/bash
#program:
# 这里填写用途说明!!!
#development history:
#开发时间 开发人 第几次发布 修改内容
#1.先写一些告知操作
echo "now ,i will detect your linux server's services!"
echo -e "the www,ftp ,ssh, and mail(smtp) will be detect! \n"
#2.开始进行一些测试任务,并且也输出一些信息
testfile=/dev/shm/netstat_checking.txt
netstat -tuln > ${testfile} # 先转存数据到内存中,不用一直执行netstat.
testing=$(grep ":80" ${testfile}) # 检测80端口是否存在?
if [ "${testing}" != ""]; then
echo "WWW is running in your system."
fi
testing=$(grep ":22" ${testfile}) # 检测22端口是否存在.
if [ "${testing}" != ""]; then
echo "SSH is running in your system."
fi
testing=$(grep ":21" ${testfile}) # 检测21端口是否存在.
if [ "${testing}" != ""]; then
echo "FTP is running in your system."
fi
testing=$(grep ":25" ${testfile}) # 检测25端口是否存在.
if [ "${testing}" != ""]; then
echo "Mail is running in your system."
fi
#! /bin/bash
#
# network Bring up/down networking
#
# chkconfig: 2345 10 90
# description: Activates/Deactivates all network interfaces configured to \
# start at boot time.
#
### BEGIN INIT INFO
# Provides: $network
# Should-Start: iptables ip6tables NetworkManager-wait-online NetworkManager $network-pre
# Short-Description: Bring up/down networking
# Description: Bring up/down networking
### END INIT INFO
# Source function library.
. /etc/init.d/functions
if [ ! -f /etc/sysconfig/network ]; then
exit 6
fi
. /etc/sysconfig/network
if [ -f /etc/sysconfig/pcmcia ]; then
. /etc/sysconfig/pcmcia
fi
# Check that networking is up.
[ "${NETWORKING}" = "no" ] && exit 6
# if the ip configuration utility isn't around we can't function.
[ -x /sbin/ip ] || exit 1
CWD=$(pwd)
cd /etc/sysconfig/network-scripts
. ./network-functions
# find all the interfaces besides loopback.
# ignore aliases, alternative configurations, and editor backup files
interfaces=$(ls ifcfg-* | \
LC_ALL=C sed -e "$__sed_discard_ignored_files" \
-e '/\(ifcfg-lo$\|:\|ifcfg-.*-range\)/d' \
-e '{ s/^ifcfg-//g;s/[0-9]/ &/}' | \
LC_ALL=C sort -k 1,1 -k 2n | \
LC_ALL=C sed 's/ //')
rc=0
# See how we were called.
case "$1" in
start)
[ "$EUID" != "0" ] && exit 4
rc=0
# IPv6 hook (pre IPv4 start)
if [ -x /etc/sysconfig/network-scripts/init.ipv6-global ]; then
/etc/sysconfig/network-scripts/init.ipv6-global start pre
fi
apply_sysctl
#tell NM to reload its configuration
if [ "$(LANG=C nmcli -t --fields running general status 2>/dev/null)" = "running" ]; then
nmcli connection reload
fi
# bring up loopback interface
action $"Bringing up loopback interface: " ./ifup ifcfg-lo
case "$VLAN" in
yes)
if [ ! -d /proc/net/vlan ] && ! modprobe 8021q >/dev/null 2>&1 ; then
net_log $"No 802.1Q VLAN support available in kernel."
fi
;;
esac
vlaninterfaces=""
vpninterfaces=""
xdslinterfaces=""
bridgeinterfaces=""
# bring up all other interfaces configured to come up at boot time
for i in $interfaces; do
unset DEVICE TYPE SLAVE NM_CONTROLLED
eval $(LANG=C grep -F "DEVICE=" ifcfg-$i)
eval $(LANG=C grep -F "TYPE=" ifcfg-$i)
eval $(LANG=C grep -F "SLAVE=" ifcfg-$i)
eval $(LANG=C grep -F "NM_CONTROLLED=" ifcfg-$i)
if [ -z "$DEVICE" ] ; then DEVICE="$i"; fi
if [ "$SLAVE" = "yes" ] && ( ! is_nm_running || is_false $NM_CONTROLLED ) ; then
continue
fi
if [ "${DEVICE##cipcb}" != "$DEVICE" ] ; then
vpninterfaces="$vpninterfaces $i"
continue
fi
if [ "$TYPE" = "xDSL" -o "$TYPE" = "Modem" ]; then
xdslinterfaces="$xdslinterfaces $i"
continue
fi
if [ "$TYPE" = "Bridge" ]; then
bridgeinterfaces="$bridgeinterfaces $i"
continue
fi
if [ "$TYPE" = "IPSEC" ] || [ "$TYPE" = "IPIP" ] || [ "$TYPE" = "GRE" ]; then
vpninterfaces="$vpninterfaces $i"
continue
fi
if [ "${DEVICE%%.*}" != "$DEVICE" -o "${DEVICE##vlan}" != "$DEVICE" ] ; then
vlaninterfaces="$vlaninterfaces $i"
continue
fi
if LANG=C grep -EL "^ONBOOT=['\"]?[Nn][Oo]['\"]?" ifcfg-$i > /dev/null ; then
# this loads the module, to preserve ordering
is_available $i
continue
fi
action $"Bringing up interface $i: " ./ifup $i boot
[ $? -ne 0 ] && rc=1
done
# Bring up xDSL and VPN interfaces
for i in $vlaninterfaces $bridgeinterfaces $xdslinterfaces $vpninterfaces ; do
if ! LANG=C grep -EL "^ONBOOT=['\"]?[Nn][Oo]['\"]?" ifcfg-$i >/dev/null 2>&1 ; then
action $"Bringing up interface $i: " ./ifup $i boot
[ $? -ne 0 ] && rc=1
fi
done
# Add non interface-specific static-routes.
if [ -f /etc/sysconfig/static-routes ]; then
if [ -x /sbin/route ]; then
grep "^any" /etc/sysconfig/static-routes | while read ignore args ; do
/sbin/route add -$args
done
else
net_log $"Legacy static-route support not available: /sbin/route not found"
fi
fi
# IPv6 hook (post IPv4 start)
if [ -x /etc/sysconfig/network-scripts/init.ipv6-global ]; then
/etc/sysconfig/network-scripts/init.ipv6-global start post
fi
# Run this again to catch any interface-specific actions
apply_sysctl
touch /var/lock/subsys/network
[ -n "${NETWORKDELAY}" ] && /bin/sleep ${NETWORKDELAY}
;;
stop)
[ "$EUID" != "0" ] && exit 4
# Don't shut the network down if root or /usr is on NFS or a network
# block device.
if systemctl show --property=RequiredBy -- -.mount usr.mount | grep -q 'remote-fs.target' ; then
net_log $"rootfs or /usr is on network filesystem, leaving network up"
exit 1
fi
vlaninterfaces=""
vpninterfaces=""
xdslinterfaces=""
bridgeinterfaces=""
remaining=""
rc=0
# get list of bonding, vpn, and xdsl interfaces
for i in $interfaces; do
unset DEVICE TYPE
eval $(LANG=C grep -F "DEVICE=" ifcfg-$i)
eval $(LANG=C grep -F "TYPE=" ifcfg-$i)
if [ -z "$DEVICE" ] ; then DEVICE="$i"; fi
if [ "${DEVICE##cipcb}" != "$DEVICE" ] ; then
vpninterfaces="$vpninterfaces $i"
continue
fi
if [ "$TYPE" = "IPSEC" ] || [ "$TYPE" = "IPIP" ] || [ "$TYPE" = "GRE" ]; then
vpninterfaces="$vpninterfaces $i"
continue
fi
if [ "$TYPE" = "Bridge" ]; then
bridgeinterfaces="$bridgeinterfaces $i"
continue
fi
if [ "$TYPE" = "xDSL" -o "$TYPE" = "Modem" ]; then
xdslinterfaces="$xdslinterfaces $i"
continue
fi
if [ "${DEVICE%%.*}" != "$DEVICE" -o "${DEVICE##vlan}" != "$DEVICE" ] ; then
vlaninterfaces="$vlaninterfaces $i"
continue
fi
remaining="$remaining $i"
done
for i in $vpninterfaces $xdslinterfaces $bridgeinterfaces $vlaninterfaces $remaining; do
unset DEVICE TYPE
(. ./ifcfg-$i
if [ -z "$DEVICE" ] ; then DEVICE="$i"; fi
if ! check_device_down $DEVICE; then
action $"Shutting down interface $i: " ./ifdown $i boot
[ $? -ne 0 ] && rc=1
fi
)
done
action $"Shutting down loopback interface: " ./ifdown ifcfg-lo
sysctl -w net.ipv4.ip_forward=0 > /dev/null 2>&1
# IPv6 hook (post IPv4 stop)
if [ -x /etc/sysconfig/network-scripts/init.ipv6-global ]; then
/etc/sysconfig/network-scripts/init.ipv6-global stop post
fi
rm -f /var/lock/subsys/network
;;
status)
echo $"Configured devices:"
echo lo $interfaces
echo $"Currently active devices:"
echo $(/sbin/ip -o link show up | awk -F ": " '{ print $2 }')
;;
restart|reload|force-reload)
cd "$CWD"
$0 stop
$0 start
rc=$?
;;
*)
echo $"Usage: $0 {start|stop|status|restart|reload|force-reload}"
exit 2
esac
exit $rc
8.选择语句模板
#!/bin/bash
#program:
# 这里填写用途说明!!!
#development history:
#开发时间 开发人 第几次发布 修改内容
#20210529 wyh 1 选择语句
# 定义函数
function printit(){
echo -n "your choice is " # 加上-n 可以不换行继续在同一行显示
}
case ${1} in
"one")
printit;echo ${1} | tr 'a-z' 'A-Z' # 将参数做大小写转换
;;
"two")
printit;echo ${1} | tr 'a-z' 'A-Z'
;;
"three")
printit;echo ${1} | tr 'a-z' 'A-Z'
;;
*)
echo "Usage ${0} {one|two|three}"
;;
esac
9. shell脚本调试
sh [-nvx] scripts.sh
# -n 不要执行脚本,仅查询语法问题,没有问题,不显示任何信息;
# -v 在执行脚本前,先将脚本文件内容输出到屏幕上;
# -x 将使用的脚本内容显示到屏幕上,很有用的一个参数;
-n
-v
-x
10 dos2unix
dos2unix是将Windows格式文件转换为Unix、Linux格式的实用命令。Windows格式文件的换行符为\r\n ,而Unix&Linux文件的换行符为\n. dos2unix命令其实就是将文件中的\r\n 转换为\n。
而unix2dos则是和dos2unix互为孪生的一个命令,它是将Linux&Unix格式文件转换为Windows格式文件的命令。