码迷,mamicode.com
首页 > 系统相关 > 详细

shell编程笔记

时间:2018-05-07 00:47:54      阅读:239      评论:0      收藏:0      [点我收藏+]

标签:查看   for   empty   span   $1   shell编程   pass   file   tab   

查看目录

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

 颜色

danger="\033[31m"
success="\033[32m"
primary="\033[34m"
warning="\033[33m]"
violet="\033[35m"
info="\033[36m"
default="\033[37m"
cls="\033[0m"

目录文件统计

#!/bin/bash
line=`tree -i -f .|wc -l`
files=`tree -i -f .| head -n  $(($line-1))`

fileCount=0
dirCount=0

for file in $files; do
    if [ -d $file ]; then
        dirCount=`expr $dirCount + 1` ;
        echo $file
    else
        fileCount=`expr $fileCount + 1`
    fi
done

echo "Dir:"$dirCount
echo "Files:"$fileCount

 

shell编程笔记

标签:查看   for   empty   span   $1   shell编程   pass   file   tab   

原文地址:https://www.cnblogs.com/demonxian3/p/9000423.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!