linux bash 笔记

Bash的简单使用笔记:

1- 命令行参数(调用脚本后面跟的参数)

2- 命令行选项(修改命令行为的单字符串)

3- 获取键盘输入

4- 读文件

5- 函数

1、命令行参数(调用脚本后面跟的参数)

xxx.sh 参数1 参数2

脚本里面获取参数的变量:

*    $0 : ./xxx.sh

*    `basename $0`: xxx.sh
*    $1 : 参数1
*    $2 : 参数2
*    ${10} 参数10
*    $#  参数的个数,不包括$0 
*    $@ :参数本身的列表数组,不包括$0 
*    $* :将所有的参数解释成一个字符串

条件表达式和判断参数1是否存在 迭代输出所有的参数 移位。(将每个参数变量左移一位。$2的值移动给变量$1,变量$1的值被丢弃。 $0程序名称不变

#!/bin/bash

if [ -n "$1" ]
then
  echo "参数1的值为: $1"
else
  echo "参数1不存在"
fi

for arg in "$@"
do
 echo $arg
done

xxx.sh 1 2 3 4

while [ -n "$1" ]
do
 echo "参数 #$count = $1"
 count=$[ $count + 1 ]
 shift
done

shift 3  $1--> 4

2、命令行选项(修改命令行为的单字符串)

getopts 是由bash内置的,支持短选项,不支持长选项; getopt 是独立的可执行文件,支持长选项,较复杂,功能也更强大。

* ./xxx.sh -a -b -c  : 短选项,各选项不需参数

* ./xxx.sh -abc   : 短选项,和上一种方法的效果一样,只是将所有的选项写在一起。
* ./xxx.sh -a args -b -c :短选项,其中-a需要参数,而-b -c不需参数。
* ./xxx.sh --a-long=args --b-long :长选项

getopts 部分标准化选项 处理简单选项 用(--)分离选项与参数,--后面的作为参数对待
while getopts "a:bc" arg #选项后面的冒号表示该选项需要参数
do
        case $arg in
             a)
                echo "a's arg:$optarg";; #参数存在$optarg中

             b)
                echo "b";;

             c)
                echo "c";;

             ?)  #当有不认识的选项的时候arg为?
            echo "unkonw argument";;
        esac
done
选项                  描述
 -a             实现所有对象
 -c             生成计数
 -d             指定目录
 -e             展开对象
 -f             指定读取数据的文件
 -h             显示命令的帮助信息
 -i             忽略大小写
 -l             生成长格式的输出 
 -n             使用非交互式(批量)模式
 -o             指定一个输出文件来重定向输出
 -q             以quite模式退出
 -r             递归处理目录和文件
 -s             以silent模式执行
 -v             生成verbose模式
 -x             排除和拒绝
 -y             设置所有提问回答为yes
while [ -n "$1" ]
do
 case "$1" in
 -a) echo "the -a option exists";;
 -b) echo "the -b option exists";;
 -c) echo "the -c option exists";;
 *) echo "the '$1' is not an option ";;
 esac
 shift
done
while [ -n "$1" ]
do
 case "$1" in
 -a) echo "the -a option exists";;
 -b) echo "the -b option exists";;
 -c) echo "the -c option exists";;
 --) shift 
     break;;
 *) echo "the '$1' is not an option ";;
 esac
 shift
done
count=1
for param in $@
do
  echo "parameter #$count-->$param"
  count=$[ $count + 1 ]
done

3、获取键盘输入

read -p "please input sth:" value  #输入的东西将附加给value变量

read -n1 -p "please input sth:" value  #接收到第一个字符给value变量后,直接继续执行下面的语句

read -s -p "please input your passwd:" pwd #不显示输入的内容,把输入的内容赋给变量pwd

4、读文件:

指定换行符读取 文件重定向给read读取 用read读取文件重定向
IFS="  "   
for line in `cat /xxx.txt`  
do   
        echo $line
done
#! /bin/bash  
  
cat /xxx.txt | while read line
do
        echo $line
done
#! /bin/bash  
  
while read line
do
        echo $line
done < /xxx.txt

5、函数(转载自:http://www.jb51.net/article/57951.htm)

1. 在shell文件内部定义函数并引用:

复制代码代码如下:

[~/shell/function]# cat factorial.sh 
#!/bin/bash
function factorial
{
factorial=1
for (( i=1;i <= $1;i++ ))
        do
        factorial=$[ $factorial * $i ]
        done
echo $1的阶乘是:$factorial
}
echo '程序名':$0,用于求阶乘
factorial $1
[~/shell/function]# ./factorial.sh 10

程序名:./factorial.sh,用于求阶乘
10的阶乘是:3628800

2.返回值

函数返回码是指函数最后一条命令的状态码,可以用于函数返回值
使用return命令手动指定返回值:

复制代码代码如下:

[~/shell/function]# cat return.sh 
#!/bin/bash
function fun1 {
  read -p "enter a: " a
  echo -n "print 2a: "
  return $[ $a * 2 ]
}
fun1
echo "return value $?"
[~/shell/function]# ./return.sh 
enter a: 100
print 2a: return value 200

由于shell状态码最大是255,所以当返回值大于255时会出错。

复制代码代码如下:

[~/shell/function]# ./return.sh 
enter a: 200
print 2a: return value 144

3.函数输出

为了返回大于255的数、浮点数和字符串值,最好用函数输出到变量:

复制代码代码如下:

[~/shell/function]# cat ./fun_out.sh 
#!/bin/bash
function fun2 {
  read -p "enter a: " a
  echo -n "print 2a: "
  echo $[ $a * 2 ]
}
result=`fun2`
echo "return value $result"
[~/shell/function]# ./fun_out.sh     
enter a: 400
return value print 2a: 800

4.向函数传递参数(使用位置参数):

复制代码代码如下:

[~/shell/function]# cat ./parameter.sh 
#!/bin/bash
if [ $# -ne 3 ]
then
    echo "usage: $0 a b c"
    exit
fi
fun3() {
    echo $[ $1 * $2 * $3 ]
}
result=`fun3 $1 $2 $3`
echo the result is $result
[~/shell/function]# ./parameter.sh  1 2 3
the result is 6
[~/shell/function]# ./parameter.sh  1 2
usage: ./parameter.sh a b c

5.全局变量与局部变量

默认条件下,在函数和shell主体中建立的变量都是全局变量,可以相互引用,当shell主体部分与函数部分拥有名字相同的变量时,可能会相互影响,例如:

复制代码代码如下:

[~/shell/function]# cat ./variable.sh    
#!/bin/bash
if [ $# -ne 3 ]
then
    echo "usage: $0 a b c"
    exit
fi
temp=5
value=6
echo temp is: $temp
echo value is: $value
fun3() {
    temp=`echo "scale=3;$1*$2*$3" | bc -ql`   
    result=$temp
}
fun3 $1 $2 $3
echo "the result is $result"
if [ `echo "$temp > $value" | bc -ql` -ne 0 ]
then 
    echo "temp is larger"
else
    echo "temp is still smaller"
fi
[~/shell/function]# ./variable.sh  12 3 2
temp is: 5
value is: 6
the result is 72
temp is larger

在这种情况下,在函数内部最好使用局部变量,消除影响。

复制代码代码如下:

[~/shell/function]# cat ./variable.sh 
#!/bin/bash
if [ $# -ne 3 ]
then
    echo "usage: $0 a b c"
    exit
fi
temp=5
value=6
echo temp is: $temp
echo value is: $value
fun3() {
    local temp=`echo "scale=3;$1*$2*$3" | bc -ql`   
    result=$temp
}
fun3 $1 $2 $3
echo "the result is $result"
if [ `echo "$temp > $value" | bc -ql` -ne 0 ]
then 
    echo "temp is larger"
else
    echo "temp is still smaller"
fi
[~/shell/function]# ./variable.sh  12 3 2
temp is: 5
value is: 6
the result is 72
temp is still smaller

6.向函数传递数组变量:

复制代码代码如下:

[~/shell/function]# cat array.sh 
#!/bin/bash
a=(11 12 13 14 15)
echo ${a[*]}
function array(){
  echo parameters : "$@" 
  local factorial=1
  for value in "$@"
  do
    factorial=$[ $factorial * $value ]
  done
  echo $factorial
}
array ${a[*]}
[~/shell/function]# ./array.sh 
11 12 13 14 15
parameters : 11 12 13 14 15
360360

7.函数返回数组变量

复制代码代码如下:

[~/shell/function]# cat array1.sh 
#!/bin/bash
a=(11 12 13 14 15)
function array(){
  echo parameters : "$@" 
  local newarray=(`echo "$@"`)
  local element="$#"
  local i
  for (( i = 0; i < $element; i++ ))
  {
    newarray[$i]=$[ ${newarray[$i]} * 2 ]    
  }
  echo  new value:${newarray[*]}
}
result=`array ${a[*]}`
echo ${result[*]}
[~/shell/function]# ./array1.sh 
parameters : 11 12 13 14 15 new value:22 24 26 28 30
原文地址:https://www.cnblogs.com/cade/p/6169447.html