shell入门

#新建第一个脚本

#!/bin/bash                  # 使用/bin/sh来解释执行
 
# auto echo hello world!     # 解释这个脚本是干什么的
# by authors cuzz           # 作者和时间一些信息

echo "hello world!"              

#脚本赋权

chmod +x hello.sh  # (+x) 可执行权限

chmod -x hello.sh   #  (-x)  取消可执行权限

Shell变量可以分为两类:局部变量和环境变量

#!/bin/bash

# define path variables
# by authors cuzz

name=cuzz    # 等号两边不能有空格

echo "my name is $name"  # 使用$引用

基本变量

echo $PWD  # 当前路径
echo $0    # 脚本名
echo $1    # 第一个参数
echo $2    # 第二个参数
echo $?    # 判断上一个命令是否正确
echo $*    # 所有参数
echo $#    # 参数的个数

shell 之if 语句

#!/bin/bash

# if test
# by authors cuzz

num=100

# 计算使用两个小括号
if (($num > 10)); then
    echo "this num greater than 10."
else
    echo "this num littler than 10."
fi

逻辑运算

运算符    说明    举例
-eq  检测两个数是否相等,相等返回 true。    [ $a -eq $b ] 返回 false

-ne  检测两个数是否不相等,不相等返回 true。    [ $a -ne $b ] 返回 true-gt  检测左边的数是否大于右边的,如果是,则返回 true。[ $a -gt $b ] 返回 false-lt   检测左边的数是否小于右边的,如果是,则返回 true。    [ $a -lt $b ] 返回 true-ge    检测左边的数是否大于等于右边的,如果是,则返回 true。    [ $a -ge $b ] 返回 false-le    检测左边的数是否小于等于右边的,如果是,则返回 true。    [ $a -le $b ] 返回 true

目录

操作符    说明    举例
-d file    检测文件是否是目录,如果是,则返回 true。    [ -d $file ] 返回 false-f file    检测文件是否是普通文件(既不是目录,也不是设备文件),如果是,则返回 true。    [ -f $file ] 返回 true-p file    检测文件是否是有名管道,如果是,则返回 true。    [ -p $file ] 返回 false-e file    检测文件(包括目录)是否存在,如果是,则返回 true。    [ -e $file ] 返回 true

创建文件目录

#!/bin/bash

DIR=hshde

if [ ! -d $DIR ]; then  # 都有空格
    mkdir $DIR
    echo "this $DIR create success."
else
    echo "this dir is exit."
fi

mysql 备份

#!/bin/bash

# define backup path
BAK_DIR=/data/backup/`date +%Y%m%d` # 反引号可以把里面当作命令来解析 
# mysql
MYSQLDB=test
MYSQLUSER=root
MYSQLPW=123456
MYSQLCMD=/usr/bin/mysqldump # 备份命令

# 判断是否是root
if [ $UID -ne 0 ]; then
    echo "Only root can execute Shell."
    exit
fi


if [ ! -d $BAK_DIR ]; then
    mkdir -p $BAK_DIR      # -p 父目录不存在就创建
    echo "The $BAK_DIR create success."
else
    echo "This $BAK_DIR is exist."
fi



# mysql backup command
$MYSQLCMD -u$MYSQLUSER -p$MYSQLPW -d $MYSQLDB >$BAK_DIR/$MYSQLDB.sql

if [ $? -eq 0 ]; then
    echo "backup success."
else
    echo "backup fail."
fi

shell之for循环

#!/bin/bash

for i in `seq 1 15`
do
    echo "the number is $i."
done


求和

#!/bin/bash

sum=0

for ((i=1; i<=100; i++)) # 双括号用于运算相当与其他语言的单括号
do
    sum=`expr $sum + $i` # expr用于计算
done

echo "$sum"

打包,只能打包到最后一个,后面的会把前面的覆盖了

#!/bin/bash

for file in `find ./ -name "*.sh"`

do
    tar -czf all.tgz $file
done

shell之while循环

#!/bin/bash

i=0
while [[ $i -lt 10 ]]  # (( $i < 10))是一样的
do
    echo "$i"
    ((i++))
done

加入read

#!/bin/bash

while read line   # 把读取的东西赋值给line
do
    echo $line
done </etc/hosts  # 从哪里读取

shell之数组

Shell 数组用括号来表示,元素用"空格"符号分割开,语法格式如下

my_array=(A B "C" D) # 定义数组

array_name[0]=value0 # 使用下标来定义
array_name[1]=value1
array_name[2]=value2

${array_name[0]}     # 读取第一个元素

${my_array[*]}       # 读取所有元素   
${my_array[@]}       # 读取所有元素

${#my_array[*]}      # 读取数组长度
${#my_array[@]}      # 读取数组长度

shell之函数

无返回值得函数

sayHello(){                  # 定义函数一
    echo "hello"
}

function sayHelloWorld(){    # 定义函数二
    echo "hello world"
}
sayhell  # 使用函数


有返回值得,使用return只能返回0-255

function sum()
{
  returnValue=$(( $1 + $2 ))
  return $returnValue
}

sum 22 4

echo $?


可以使用echo来传递参数
function length()
{
  str=$1
  result=0
  if [ "$str" != "" ] ; then
      result=${#str}
  fi
  echo "$result"
}

len=$(length "abc123")  # 调用

echo "The string's length is $len "

shell 之sed用法

把test.txt中的old修改为new,要使用-i才能插入
> sed -i 's/old/new/s' test.txt

在每行行前面添加一个cuzz
> sed -i  sed 's/^/&cuzz/g' test.txt

在每行的末尾添加一个cuzz
> sed  -i 's/$/& cuzz/g' test.txt
查找最大和最小值 number.txt

12 324 56 0034 -23 345
345 349- 245 345 

345 0989 0459 -25


命令
cat number.txt | sed 's/ /\n/g' | grep -v "^$" | sort -nr | sed -n '1p;$p'

sed 's/ /\n/g'  # 把所有空格换成换行
grep -v "^$"    # 去掉所有空格
sort -nr        # 降序排列
sed -n '1p;$p   # 找出第1行和最后一行

shell 之grep 

-a :将 binary 文件以 text 文件的方式搜寻数据

-c :计算找到 '搜寻字符串' 的次数

-i :忽略大小写的不同,所以大小写视为相同

-n :顺便输出行号

-v :反向选择,亦即显示出没有 '搜寻字符串' 内容的那一行

--color=auto :可以将找到的关键词部分加上颜色的显示

shell 之AWK

# 每行按空格或TAB分割
cat test.txt | awk '{print $1}'      # 行匹配语句 awk '' 只能用单引号

# 指定分割
awk -F                               #-F相当于内置变量FS, 指定分割字符
cat test.txt | awk -F: '{print $1}'  # 以分号分割

# 指定添加某些内容
cat test.txt | awk -F: '{print "haha" $1}' # 提前出来再添加haha

shell 之find 

基本命令
find /dir -name "test.txt"          # 在/dir目录下查找
find . -name "test.txt"             # 在当前目录下找 
find . -maxdepth 1 -name "text.txt" # 只遍历一层
find . -type f -name "text"         # 指定类型
find . -name "text" -mtime -1       # 指定时间
find . -size +20M                   # 指定大小

查找并执行其他命令
find . -name "text.txt" -exec rm -rf {} \;  # 后面{} \是固定格式

来源:https://www.jianshu.com/p/096394421740

posted @ 2020-04-17 09:45  快乐的平头哥  阅读(120)  评论(0编辑  收藏  举报