Linux-shell编程

一、shell编程

1.简介

把可以在终端中执行的命令保存到文件中,该文件就被称为shell程序

本质:shell编程就是对一堆Linux命令的逻辑化处理

bash:是大多数Linux默认的shell

/bin/bash

命令演示:

lichongchong@ubuntu01:~$ cd Desktop/
lichongchong@ubuntu01:~/Desktop$ mkdir shellText
lichongchong@ubuntu01:~/Desktop$ cd shellText/
lichongchong@ubuntu01:~/Desktop/shellText$ touch hello.sh
lichongchong@ubuntu01:~/Desktop/shellText$ vim hello.sh 
lichongchong@ubuntu01:~/Desktop/shellText$ cat hello.sh 
#!/bin/bash
echo "hello world"
lichongchong@ubuntu01:~/Desktop/shellText$ cd ../..
lichongchong@ubuntu01:~$ ls
Desktop    Downloads  Pictures  PycharmProjects  Templates
Documents  Music      Public    Software         Videos
lichongchong@ubuntu01:~$ cd Software/
lichongchong@ubuntu01:~/Software$ ls
pycharm-2017.3.4
lichongchong@ubuntu01:~/Software$ cd pycharm-2017.3.4/
lichongchong@ubuntu01:~/Software/pycharm-2017.3.4$ ls
bin        debug-eggs  helpers  Install-Linux-tar.txt  lib      plugins
build.txt  help        index    jre64                  license
lichongchong@ubuntu01:~/Software/pycharm-2017.3.4$ cd bin/
lichongchong@ubuntu01:~/Software/pycharm-2017.3.4/bin$ ls
format.sh       idea.properties         log.xml              pycharm.sh
fsnotifier      inspect.sh              printenv.py          pycharm.vmoptions
fsnotifier64    libyjpagent-linux64.so  pycharm64.vmoptions  restart.py
fsnotifier-arm  libyjpagent-linux.so    pycharm.png
lichongchong@ubuntu01:~/Software/pycharm-2017.3.4/bin$ pwd
/home/lichongchong/Software/pycharm-2017.3.4/bin
lichongchong@ubuntu01:~/Software/pycharm-2017.3.4/bin$ cd
lichongchong@ubuntu01:~$ sh /home/lichongchong/Software/pycharm-2017.3.4/bin
lichongchong@ubuntu01:~$ sh /home/lichongchong/Software/pycharm-2017.3.4/bin/pycharm.sh 
lichongchong@ubuntu01:~$ cd Desktop/
lichongchong@ubuntu01:~/Desktop$ cd shellText/
lichongchong@ubuntu01:~/Desktop/shellText$ ls
hello.sh
lichongchong@ubuntu01:~/Desktop/shellText$ ls -l
总用量 4
-rw-rw-r-- 1 lichongchong lichongchong 31 12月 21 15:02 hello.sh
lichongchong@ubuntu01:~/Desktop/shellText$ chmod u+x hello.sh 
lichongchong@ubuntu01:~/Desktop/shellText$ ls -l
总用量 4
-rwxrw-r-- 1 lichongchong lichongchong 31 12月 21 15:02 hello.sh
lichongchong@ubuntu01:~/Desktop/shellText$ sh hello.sh 
hello world
lichongchong@ubuntu01:~/Desktop/shellText$ hello.sh
hello.sh:未找到命令
lichongchong@ubuntu01:~/Desktop/shellText$ ./hello.sh
hello world
lichongchong@ubuntu01:~/Desktop/shellText$ /bin/sh hello.sh
lichongchong@ubuntu01:~/Desktop/shellText$ /bin/bash hello.sh
2.使用
2.1定义变量

变量名=值

$变量名  或者 ${变量名}
#注意:花括号可写可不写,加上是为了解释器识别变量的边界

删除变量:unset 变量名【如果一个变量被删除,再次使用,不会报错,只是没有任何的输出,unset命令不能删除只读变量】

变量的重新赋值 变量名=新值

只读变量:readonly 变量名

代码演示:
#!/bin/bash
echo "hello world"
name="zhangsan"
echo $name
num=10
echo ${num}
echo "your name is ${name}"
name="lisi"
echo ${name}
myname="abc"
readonly myname
#myname="def"


myurl="www.baidu.com"
unset myurl
echo ${myurl}
2.2字符串
双引号:可以包含除了$,`,\等之外的任意字符【在shell中有特殊含义】
单引号:其中的任意字符都不会被解析,都会原样输出
反引号:会将其中的内容作为命令执行

代码演示:

!/bin/bash

#定义字符串
name="zhangsan"
s1="hello ${name}"
echo ${s1}

echo $name $s1

#获取字符串的长度
echo ${#name}

#提取字符串
echo ${name:1:4}

#查找子字符串
s2="today is a good day"
echo `expr index "$s2" is`

#转义字符
#通过\转义特定的字符
s3="your name is  \"$name\""
echo ${s3}
2.3数组

bash只支持一维数组,并且没有限制数组的大小

数组的元素下标从0开始,获取数组中的元素使用下标了,下标可以是整数或者表达式

定义:数组名=(值1 值2 值3 。。。。)

代码演示:

!/bin/bash

#定义数组
#数组名默认指向数组中的第0个元素
arr1=(10 20 30 40)
echo ${arr1}

arr2=(
10
20
30
40
)
echo ${arr2}

#获取数组中的元素
echo ${arr1[2]}
echo ${arr1[@]}

#获取数组的长度
echo ${#arr1[@]}
echo ${#arr1[*]}
2.4运算符
1>基本用算符
2>文件检测运算符

代码演示:

#!/bin/bash
#算术运算符
val=`expr 2 + 2`
echo ${val}


val=`expr 2 \* 2`
echo ${val}


val=`expr 2 / 2`
echo ${val}


val=`expr 2 % 2`
echo ${val}


#关系运算符
a=10
b=20
if [ $a -eq $b ]
then
   echo "a等于b"
else
   echo "a不等于b"
fi

#逻辑运算符
if [ 1 -lt 3 -a 2 -lt 3 ]
then
   echo "ok"
fi

if [ 1 -lt 3 ] && [ 2 -lt 3 ]
then
  echo "ok~~~"
fi
2.5echo printf test

代码演示:

!/bin/bash

echo "hello"

#-e表示开启转义,\n表示换行,\c表示不换行
#echo -e "ok,\n"
#echo "test~~~~"

echo -e "ok,\c"
echo "test~~~~"

echo `date`

echo `cal`


printf "%-20s %-8s %-4s\n" 姓名 性别 体重
printf "%-10s %-8s %-4s\n" zhangsan nan 66.0543
printf "%-10s %-8s %-4.3s\n" lisi nv 50.634343
printf "%-10s %-8s %-4.2s\n" jack nan 75.0434


num1=10
num2=20
if [ $num1 -eq $num2 ]
then
   echo "相等"
else
  echo "不"
fi


if test $[num1] -eq $[num2]
then
   echo "相等"
else
  echo "不"
fi

num1="hello"
num2="hello11"
if test $num1 = $num2
then
   echo "相等"
else
  echo "不"
fi
2.6语句

代码演示:

!/bin/bash

a=10
b=20
if [ $a -eq $b ]
then
  echo "="
elif [ $a -gt $b ]
then
  echo "》"
else
  echo "<"
fi


#case
echo "请输入一个数字"
read num
case $num in
  1) echo "你选择了1"
  ;;
  2) echo "你选择了2"
  ;;
  *) echo ""
  ;;
esac


#循环语句
for num in 1 2 3 4
do
  echo $num
done

for s in "hello"
do
  echo $s
done


arr=(34 54 54 54)
for x in ${a[*]}
do
  echo $x
done
2.6函数

代码演示:

!/bin/bash

#无参无返回值
demo(){
  echo "hello"
}
demo

#返回值
add()
{ 
 
  echo "请输入第一个数:"
  read num
  echo "请输入第二个数:"
  read otherNum
  
  return $(($num+$otherNum))
  
}
add
echo "得到的结果为$?"

#参数
arg()
{
  echo $1
  echo $2
  echo $#
  echo $*

  return 23
}
arg 10 20
echo $?      
2.7函数

代码演示:

!/bin/bash

#午餐无返回值
demo(){
  echo "hello"
}
demo

#返回值
add()
{
  echo "请输入第一个数:"
  read num
  echo "请输入第二个数:"
  read otherNum

  return $(($num+$otherNum))

}
add
echo "得到的结果为$?"

#参数
arg()
{
  echo $1
  echo $2
  echo $#
  echo $*

  return 23
}
arg 10 20
echo $?
                                         
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值