shell编程笔记

Posted Dëm0n

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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

 颜色

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编程笔记的主要内容,如果未能解决你的问题,请参考以下文章

Shell编程笔记

Linux自学笔记——shell脚本编程

linux shell脚本编程笔记: 重定向

Linux学习笔记-Shell教程

一文详解shell编程(shell编程笔记)

shell if 语句