Linux Shell 用法

查看shell的环境变量

export显示导出为用户环境变量的环境变量
set显示shell的所有设置的环境变量
env列出当前用户的环境变量 

Shell test 命令

Shell中的 test 命令用于检查某个条件是否成立,它可以进行数值、字符和文件三个方面的测试。

数值测试

参数 说明
-eq 等于则为真
-ne 不等于则为真
-gt 大于则为真
-ge 大于等于则为真
-lt 小于则为真
-le 小于等于则为真

用例

num1=100
num2=100
if test $[num1] -eq $[num2]
then
    echo '两个数相等!'
else
    echo '两个数不相等!'
fi

字符串测试

= 等于则为真
!= 不相等则为真
-z 字符串 字符串的长度为零则为真
-n 字符串 字符串的长度不为零则为真

Linux shell if [ -n ] 正确使用方法

shell 中利用 -n 来判定字符串非空。

错误用法:

ARGS=$*
if [ -n $ARGS  ]
then
   print "with argument"
fi
print " without argument"

不管传不传参数,总会进入if里面。
原因:因为不加“”时该if语句等效于if [ -n ],shell 会把它当成if [ str1 ]来处理,-n自然不为空,所以为正。
正确用法:需要在$ARGS上加入双引号,即"$ARGS".

num1="ru1noob"
num2="runoob"
if test $num1 = $num2
then
    echo '两个字符串相等!'
else
    echo '两个字符串不相等!'
fi

文件测试

-e 文件名 如果文件存在则为真
-r 文件名 如果文件存在且可读则为真
-w 文件名 如果文件存在且可写则为真
-x 文件名 如果文件存在且可执行则为真
-s 文件名 如果文件存在且至少有一个字符则为真
-d 文件名 如果文件存在且为目录则为真
-f 文件名 如果文件存在且为普通文件则为真
-c 文件名 如果文件存在且为字符型特殊文件则为真
-b 文件名 如果文件存在且为块特殊文件则为真

if test -e ./notFile -o -e ./bash
then
    echo '至少有一个文件存在!'
else
    echo '两个文件都不存在'
fi
if   test  ! -r ss.axf -o ! -r aa.axf 
then

	echo ' file can not read'
	exit 1
fi

Shell还提供了与( -a )、或( -o )、非( ! )三个逻辑操作符用于将测试条件连接起来,其优先级为:"!"最高,"-a"次之,"-o"最低。

函数返回值

sh a.sh
ret=$?
echo "res:$ret"
if [ $ret != 0 ]; then 
	echo "exit"
	exit 1
fi

cat多行输入到文件

$ cat >user-data <<EOF
#cloud-config
password: password
chpasswd:
  expire: False
ssh_pwauth: True
ssh_authorized_keys:
  - ssh-rsa AAAA...UlIsqdaO+w==
EOF

jq格式化json

$jq<<EE
> {
> "data":{
> "id":1,
> "name":"hi"
> }
> }
> EE

腾讯云把网卡ip保存到ini

#!/bin/bash

wait_if_ip()
{
	local net_interface="eth0"
	
	ifconfig eth0
	for i in $(seq 5 -1 1)
	do
		result=`ifconfig $net_interface|grep "inet\>"|awk '{print $2}'`
		if [ -n "$result" ]; then
			break
		fi
	     	sleep 1
	done
}

save_ip_info() 
{ 
set -x
	net_arr=`ls /sys/class/net` 
	net_arr=(${net_arr[@]/lo}) 
	net_arr=(${net_arr[@]/vpntun})
	for net in ${net_arr[@]}
	do
		local ip=`ifconfig $net|grep "inet\>"|awk  '{ print $2 }'`
		rwini -f $ip_cfg -s $net -k ip -v $ip -w
		local net_mask=`ifconfig $net|grep "inet\>"|awk  '{ print $4 }'`
		rwini -f $ip_cfg -s $net -k net_mask -v $net_mask -w
		local ether=`ifconfig $net|grep "ether\>"|awk  '{ print $2 }'`
		rwini -f $ip_cfg -s $net -k mac -v $ether -w
		if [ $plat_info = Tencent ];then
			curl -s $tencent_gw_api/macs/$ether/local-ipv4s/$ip/gateway |grep $ip_pattern
			local gateway=`curl -s $tencent_gw_api/macs/$ether/local-ipv4s/$ip/gateway |grep $ip_pattern`
		elif [ $plat_info = Alibaba ];then
			local gateway=`curl -s $ali_gw_api/macs/$ether/gateway |grep $ip_pattern`
		fi
		rwini -f $ip_cfg -s $net -k gateway -v $gateway -w
	done
}

rename_if()
{
	net_arr=`ls /sys/class/net`
	net_arr=(${net_arr[@]/lo})
	net_arr=(${net_arr[@]/vpntun})
	for net in ${net_arr[@]}
	do
		ifconfig $net down
		udevadm test -a add /sys/class/net/$net 2>/var/udeverr.log 1>/var/udevstd.log
	done
}

complete_netif()
{
	local origin_name=$1
	rwini -f $gateway_file -s LAN_INTERFACE -k Ifname -v "ge0" -w
	rwini -f $gateway_file -s DMZ_INTERFACE -k Ifname -v "ge1 ge2 ge3 ge4 ge5 ge6 ge7 manage" -w

	local ipv4=`rwini -f $ip_cfg -s $origin_name -k ip -r`
	rwini -f $gateway_file -s LAN_INTERFACE -k IP -v $ipv4 -w

	local mask=`rwini -f $ip_cfg -s $origin_name -k net_mask -r`
	rwini -f $gateway_file -s LAN_INTERFACE -k Mask -v $mask -w

	local if_gateway=`rwini -f $ip_cfg -s $origin_name -k gateway -r`
	rwini -f $gateway_file -s LAN_INTERFACE -k Gateway -v $if_gateway -w
}
# var define
ip_cfg=/etc/sinfor/cloud_ip.ini
ip_pattern="[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}"
tencent_gw_api="http://metadata.tencentyun.com/2017-09-19/meta-data/network/interfaces"
ali_gw_api="http://100.100.100.200/latest/meta-data/network/interfaces"
gateway_file="/etc/sinfor/gateway.conf"
plat_file="/usr/hardwares/cloud_plat_info"

#  main
plat_info=`cat $plat_file`
gateway_mode=`rwini -f $gateway_file -s GATEWAY_MODE -k Mode -r`
echo i:$plat_info
cat /dev/null >$ip_cfg
dhclient -v
wait_if_ip

save_ip_info 
rename_if
set -x
if [ $gateway_mode = 1 ];then
	complete_netif eth0
fi 

modify version


curr_path=$(cd `dirname $0`; pwd)
code_root=$(cd $curr_path/../../;pwd)
echo $code_root

root_makefile=$code_root/Makefile
jq=$curr_path/jq

if [ $# -lt 1 ];then
    echo -e "usage$ bash $0  new-version-name\n\
	参数 new-version-name 版本号是3位或者4位点分十进制"
    exit 1
fi
ver_pattern="^[0-9]\+\.[0-9]\+\.[0-9]\+\(\.[0-9]\+\)\?$"
new_ver=`echo $1|grep $ver_pattern`
if [ -z "$new_ver" ];then
	echo "参数格式不对,版本号是3位或者4位点分十进制"
	exit
fi
#is_three_bit=`echo $new_ver|grep "^[0-9]\+\.[0-9]\+\.[0-9]\+$"`
#if [ -n "$is_three_bit" ];then
#	new_ver="$new_ver.0"
#fi

echo new version:$new_ver
new_major=`echo $new_ver|awk -F '.' '{print $1}'`
new_minor=`echo $new_ver|awk -F '.' '{print $2}'`
new_micro=`echo $new_ver|awk -F '.' '{print $3}'`

major=`grep "\<VERSION_MAJOR\>\s*=" $root_makefile |awk -F '=' '{print $2}'`
minor=`grep "\<VERSION_MINOR\>\s*=" $root_makefile |awk -F '=' '{print $2}'`
micro=`grep "\<VERSION_MICRO\>\s*=" $root_makefile |awk -F '=' '{print $2}'`
#build=`grep "\<VERSION_BUILD\>\s*=" $root_makefile |awk -F '=' '{print $2}'`

old_ver=$major.$minor.$micro
#echo $old_ver
if [ $new_ver = $old_ver ];then
	echo "新旧版本相同无须修改:$new_ver"
	exit
fi

# modify pkginfo.txt apppre
dir_list="x86 arm bbc sp"
for dir in $dir_list
do
	pkginfo_file=$code_root/package/$dir/pkginfo.txt
	exist_ver_num=`cat $pkginfo_file|$jq .support_version|grep $new_ver`
	if [ -z "$exist_ver_num"  ];then
		arg=`jq -M --arg arg "$new_ver" '.support_version + [$arg]' $pkginfo_file|jq .[]`
		$jq --arg arg "$arg" '.support_version=[$arg]' $pkginfo_file |sed -e 's/\\n/,/g' -e 's/\\"/"/g' -e 's/\"\"/"/g'|jq >$pkginfo_file.tmp
		if [ $? != 0  ];then
			echo "jq proc $pkginfo_file error";exit
		fi
		#modify version
		$jq --arg arg "$new_ver" '.version=$arg' $pkginfo_file.tmp >$pkginfo_file
		if [ $? != 0  ];then
			rm -f $pkginfo_file.tmp
			echo "jq proc $pkginfo_file error";exit
		fi
		rm -f $pkginfo_file.tmp
	fi
	apppre_file=$code_root/package/$dir/apppre
	if [ -w "$apppre_file" ];then
		sed -i "/SUPPORT_UPDVERSION_LIST=/aSDW-R${old_ver}\\\\" $apppre_file 
	fi
done


bcc_cfg_dir=$code_root/source/bbc/bbcrestgate/bbc_config_sync
bbc_cfg_ver_file=$bcc_cfg_dir/config_version.lua
grep -n "config_version\[$new_micro\]" $bbc_cfg_ver_file
if [ $? = 0  ];then
	echo "error: variable config_version[$new_micro] has exist in $bbc_cfg_ver_file";exit
fi
func_line=`grep "function\s*getMatchVersion" -n $bbc_cfg_ver_file|awk -F ':' '{print $1}'`
insert_point=`expr $func_line - 7`

sed -i "${insert_point}{x;p;x;}" $bbc_cfg_ver_file 
sed -i "${insert_point}i}" $bbc_cfg_ver_file 
sed -i "${insert_point}i\\\tdir_index = $new_micro" $bbc_cfg_ver_file 
sed -i "${insert_point}i\\\tversion=\"SDW-R${new_ver}\"" $bbc_cfg_ver_file 
sed -i "${insert_point}i{" $bbc_cfg_ver_file 
sed -i "${insert_point}iconfig_version[$new_micro] =" $bbc_cfg_ver_file 
sed -i "${insert_point}{x;p;x;}" $bbc_cfg_ver_file 

#add bbc config file
if [ ! -d $bcc_cfg_dir/config_dirs/$new_micro  ];then
	mkdir -p $bcc_cfg_dir/config_dirs/$new_micro 
	cp $bcc_cfg_dir/config_dirs/$micro/config_sync.json  $bcc_cfg_dir/config_dirs/$new_micro/config_sync.json
fi
echo "tip:$bcc_cfg_dir/config_dirs/$new_micro/config_sync.json 需要检查是否要改"

bbc_docker_img=$code_root/package/bbc/docker_image_api.json
$jq --arg arg "$new_ver" '.product.version=$arg' $bbc_docker_img > $bbc_docker_img.tmp
if [ $? = 0  ];then
	mv $bbc_docker_img.tmp $bbc_docker_img
else
	echo "error:jq proc $bbc_docker_img";exit
fi

sed -i "s/\(VERSION_MAJOR\)[[:space:]]*\(=\)[[:space:]]*\([0-9]*\)/\1\2$new_major/" $root_makefile 
sed -i "s/\(VERSION_MINOR\)[[:space:]]*\(=\)[[:space:]]*\([0-9]*\)/\1\2$new_minor/" $root_makefile 
sed -i "s/\(VERSION_MICRO\)[[:space:]]*\(=\)[[:space:]]*\([0-9]*\)/\1\2$new_micro/" $root_makefile 

find 反向过滤

echo `basename $0`
invert_array=(/demo /test)

for i in ${invert_array[*]}
do
    cmd_array[${#cmd_array[*]}]=-path
    cmd_array[${#cmd_array[*]}]=$i
    cmd_array[${#cmd_array[*]}]=-o
done
cmd_array[${#cmd_array[*]}-1]=""
echo ${invert_array[*]}
echo ${cmd_array[*]}

find / \( ${cmd_array[*]} \)  -prune -o -print

posted on 2019-04-19 21:29  ims-  阅读(292)  评论(0编辑  收藏  举报

导航