常用shell命令

#!/usr/bin/env bash(推荐)
#!/bin/bash
# shell 注释
# author: test
echo "Hello,World!"
echo "hello, \"fay\""
name=test
echo "hello,\"${name}\""
echo "YES\nNO"  
echo -e "YES\nNO"

# \c 不换行
echo -e "YES\c"
echo "NO"

#重定向
echo  "test" > test.txt

#输出执行结果
echo `pwd`

printf "%d %s\n" 1 "abc"

# 声明变量 ${var}(推荐)  $var
word = "hello"
echo ${word}

# 使变量不可更改
readonly word
# 删除变量 只读变量不能删除
unset word

# 常用的环境变量
$HOME
$PATH
$PWD
$PS1

# 单引号中不能使用变量  推荐使用双引号

#字符串拼接
name2=jack
str = "hello,${name2}"
echo ${str}

# 获取字符串长度  #
text="12345"
echo ${#text}

#截取字符串
echo ${text:2:2}

#查找字符串
text="hello"
echo `expr index "${text}" ll`

## 数组
nums = ([2]=2 [0]=0 [1]=1)
colors=(red yellow "dark blue")

# 访问数组中的元素
echo ${nums[1]}

#访问数组中的所有元素
echo ${colors[*]}
echo ${colors[@]}

#遍历输出
printf "+ %s\n" "${colors[@]}"

echo ${nums[@]:0:2}

#数组长度
echo ${#nums[*]}

#向数组中添加元素
colors=(white "${colors[*]}" green black)
echo ${colors[@]}
#删除元素
unset colors[1]

# 算数运算符  必须要有空格
x=10
y=20
echo "x=$x, y=${y}"
var=`expr ${x} + ${y}`

if [[ ${x} == ${y} ]]
then
	echo "${x} = ${y}"
fi
if [[ ${x} != ${y} ]]
then
	echo "${x} != ${y}"
fi

# 关系运算符 只支持数字(字符串是数字也可以)
if [[ $x -le $y ]]; then
	echo "${x} le ${y}: x<=y"
else 
	echo "${x} -le ${y}: x>y"
fi

if [[ $x -lt 5 && $y -gt 100 ]]; then
	echo ""
else
	echo ""
fi

# 字符串运算符 -z 为长度为0, -n 长度不为零  
x="abc"
if [[ -z ${x}]];then
	echo "长度为0"
else
	echo "不为0"
fi

if [[ ${x} ]]; then
	echo "字符串不为空"
else 
	echo "字符串为空"
fi

# 文件测试运算符 -rwx   f d s e
file="/home/fay/test.txt"

if [[ -e $file ]]; then
	echo "文件存在"
else
	`touch "${file}"`
fi

if [[ -s $file ]]; then
	echo "文件不为空"
fi

if [[ -r ${file} ]]; then
	echo "文件可读"
	
# if elif else
x=10
y=20

if [[ $x > $y ]]; then
	echo "x>y"
elif [[ $x < $y ]]; then
	echo "x<y"
else
	echo "x=y"
fi

# case
oper="*"
case ${oper} in
	"+")
		val=`expr $x + $y`
		echo "${val}"
		;;
	"*")
		val=`expr ${x} * ${y}`
		echo "${val}"
		;;
	*)
		echo "Unknown oper!"
		;;
esac

# for
dir=/home/test
for file in ${dir}/*.sh; do
	mv "${file}" "${dir}/scripts"
done

x=0
while [[ $x -lt 10 ]]; do
	echo $((x * x))
	x = $((x + 1))
done

# select
ps3="Choose the package manager: "
select ITEM in bower npm gem pip
do
echo -n "Enter the package name: " && read PACKAGE
exec
case ${ITEM} in
	bower) bower install ${PACKAGE} ;;
	npm) npm install ${PACKAGE} ;;
	gem)
		gem install ${PACKAGE}
		;;
	pip)
		pip install ${PACKAGE}
		;;
esac
break
done

# break
i=1
while [[ ${i} -lt 10 ]]; do
	if [[ $((i % 3)) -eq 0 ]] && [[ $((i % 2)) -eq 0 ]]; then
		echo ${i}
		break;
	fi
	i = `expr ${i} + 1`
done

# continue
for ((i=0;i<=10;i++)); do
	if [[ $((i % 2)) -eq 0 ]]; then
		continue
	fi
	echo ${i}
done

# 函数
calc (){
	PS3="choose the oper: "
	select oper in + - \* /
	do
		echo -n "enter first num: " && read x
		echo -n "enter second num: " && read y
	exec
	case ${oper} in
	 "+")
		return $((${x} + ${y}))
		;;
	 "-")
		return $((${x} + ${y}))
		;;
	 *)
		echo "${oper} is not support!"
		return 0
		;;
	esac
	break
	done
}
calc
echo "the result is: $?"


# 位置参数 $0:脚本名称 $1-$n:1到n个参数列表   $* or $@:所有参数  $#:参数个数 $FUNCNAME:函数名称

echo "所有参数:$*"
echo "参数个数:$#"

x=0
if [[ -n $1 ]]; then
	echo "one param: $1"
	x=$1
else
	echo "one param is empty"
fi

y=0
if [[ -n $2 ]]; then
  echo "第二个参数为:$2"
  y=$2
else
  echo "第二个参数为空"
fi

paramsFunction() {
	echo "函数第一个入参:$1"
	echo "函数第二个入参:$2"
}

paramsFunction $x $y

# 函数处理参数
# $#:个数  $*:所有参数  $$:脚本运行的进程id  $!:后台运行的最后一个进程id 
# $-:shell使用的当前选项 $?:函数返回值

runner () {
	return 0
}

name=zp
paramsFuncion(){
	echo "函数第一个入参:$1"
	echo "函数第二个入参:$2"
	echo "传递到脚本的参数个数:$#"
	echo "所有参数:"
	printf "+ %s\n" "$*"
	echo "脚本运行的当前进程ID号: $$"
	echo "后台运行的最后一个进程的ID号:$!"
	echo "所有参数:"
	printf "+ %s\n" "$@"
	echo "Shell 使用的当前选项: $-"
	runner
	echo "runner 函数的返回值:$?"
}
paramsFunction 1 "abc" "hello, \"zp\""


# Shell 扩展
echo beg{i,a,u}n
echo {0..5}

now=`date + %T`
now=$(date + %T)

result= $(( ((10+5*3) - 7 ) / 2 ))
echo $result

# 在算数表达式中,使用变量无需带上$前缀
x=4
y=7
echo $(( ++x + y++ ))

# 如果输入 可能 包含空格,务必要用引号把表达式包起来
input = "A string with  strange      whitespace"
echo $input
echo "$input"

# 管道 重定向  012   > &> &>>  <  <<  <<<
ls -l > list.txt
ls -a >> list.txt
grep da * 2>error.txt

command > /dev/null
command > /dev/null 2>&1

# Debug   -x(debug)  -n 语法检查
set -x
for ((i=0;i<10;i++)); do
	printf $i
done
set +x


1.获取随机字符
echo $RANDOM | md5sum |cut -c 1-8

随机数字
echo $RANDOM|cksum|cut -c 1-8

检查软件是否安装

isInstall(){
	if [[ rpm -q $1 &>/dev/null ]]; then
		return true
	else
		return false
	fi
}

ping ip,是否失败三次

IP_LIST="test1 test2 test3"
for ip in IP_LIST ;do
	num=1
	while [ $num -le 3 ] ; do
		if ping -c 1 $ip >/dev/null ; then
			echo "$ip is successful"
			break
		else
			FAIL_COUNT[$num]=$ip
			let num++
		fi
	done
	
	if [ ${#FAIL_COUNT[*]} -eq 3 ]; then
		echo "${FAIL_COUNT[1] Ping is failure!}"
		unset FAIL_COUNT[*]
	fi
done
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值