shell编程笔记

查看目录

tree -i -f | awk '{if(!system("test -d "$1))print $1}'

批量快速创建user

for i in user{0..10}; do
    useradd $i
    echo 123456 | passwd --stdin $i
done

使用if语句判断

#!/bin/bash
#文件判断
if [ -e $1 ]; then
    echo "file exists"
fi

if [ -r $1 ]; then
  echo "readable"
fi

if [ -w $1 ]; then
  echo "writable"
fi

if [ -x $1 ]; then
  echo "executeable"
fi

if [ -s $1 ]; then
  echo "have contents"
fi

if [ -d $1 ]; then
  echo "directory"
fi

if [ -f $1 ]; then
  echo "file"
fi

if [ -c $1 ]; then
  echo "charactor device"
fi

if [ -b $1 ]; then
    echo "block device"
fi

#字符串判断
if [ $1 = "admin" ]; then
    echo "you are admin"
fi

if [ $1 != "demon" ]; then
    echo "you are not demon"
fi

if [ -z $1 ]; then
    echo "zero string"
fi

if [ -n $1 ]; then
    echo "not empty"
fi


#数字判断
if test $1 -eq 4; then
    echo "$1=4"
fi
if test $1 -ne 4; then
    echo "$1!=4"
fi
if test $1 -gt 4; then
    echo "$1>4"
fi
if test $1 -ge 4; then
    echo "$1>=4"
fi
if test $1 -lt 4; then
    echo "$1<4"
fi
if test $1 -le 4; then
    echo "$1<=4"
fi


#C语言语法
if (( $1 != 'demon' )); then
    echo "[C]not demon"
elif (( $1 > 5 )); then
    echo "[C]$1 > 5"
fi

猜你喜欢

转载自www.cnblogs.com/demonxian3/p/9000423.html