生信linux入门(二)shell脚本编程
shell 是操作系统的最外层,可以合并编程语言以控制进程和文件,以及启动和控制其他程序。最常用的shell是bash,易用和免费。
shell脚本 == 命令的集合
1、认识shell脚本
// 登陆服务器后查看bash
(base) mordredll@DESKTOP-1L2C7BR:~$ echo $BASH
/bin/bash
// 创建一个shell脚本.sh是标准格式
(base) mordredll@DESKTOP-1L2C7BR:~$ vi first_shell.sh
// 进入first_shell.sh文件中
// 表明这是一个shell脚本
#!/bin/bash
// 三行注释:文件名、内容描述、作者
#Filename : first_shell.sh
#auto echo hello world
#by Mordredll 2023-12-13
// echo打印,esc退出插入模式,shift+:输入wq保存内容
echo "Hello World!"
// 添加执行权限运行
(base) mordredll@DESKTOP-1L2C7BR:~$ chmod +x first_shell.sh
(base) mordredll@DESKTOP-1L2C7BR:~$ ./first_shell.sh
Hello World!
// 在自己环境中去运行
(base) mordredll@DESKTOP-1L2C7BR:~$ /bin/bash first_shell.sh
Hello World!
// sh执行
(base) mordredll@DESKTOP-1L2C7BR:~$ sh first_shell.sh
Hello World!
2、shell编程之变量
// 定义变量,引用变量加 $
name = mordredll
echo "My name is $name"
(base) mordredll@DESKTOP-1L2C7BR:~$ /bin/bash var.sh
My name is mordredll
// shell常见系统变量解析
$0 当前程序名称
$n 当前程序第n个参数
$* 当前程序的所有参数(不包括程序本身)
$# 当前程序的参数个数(不包括程序本身)
// 常用来判断上一个任务是否执行成功
$? 命令和程序执行完后的状态,一般返回0表示执行成功
$UID 当前用户的UID
$PWD 当前所在目录
// 系统变量不需要定义直接调用
echo $PWD
(base) mordredll@DESKTOP-1L2C7BR:~$ /bin/bash var.sh
/root
// 传参数mordredl进去
(base) mordredll@DESKTOP-1L2C7BR:~$ cat var.sh
echo $1
(base) mordredll@DESKTOP-1L2C7BR:~$ sh var.sh mordredl
mordredl
// 两位数变量的正确表示
echo ${12}
// 错误表示及输出
(base) mordredll@DESKTOP-1L2C7BR:~$ cat var.sh
echo $12
(base) mordredll@DESKTOP-1L2C7BR:~$ sh var.sh P1 P2 1 2 3 4 5 6 7 8 9 0
// 输出$1的变量P1,加上字符串2
P12
// 显示颜色
echo -e '\033[32m---------------\033[0m'
// 0m改为1m之后下面所有的颜色都会变化
echo -e '\033[32m---------------\033[1m'
3、shell编程之if语句
// 判断大小用(( ))
(base) mordredll@DESKTOP-1L2C7BR:~$ cat if.sh
#! /bin/bash
#auto if test
#by mordredl 2023-12-13
num1=100
num2=200
if (( $num1 > $num2 ));then
echo "$num1 greater $num2"
else
echo "$num1 smaller $num2"
fi
(base) zhanglei@DESKTOP-1L2C7BR:~$ /bin/bash if.sh
100 smaller 200
// 逻辑运算符
// if [ -f filename ] 判断文件是否存在
// if [ -d dir ] 判断目录是否存在
// -eq 等于 应用于 整型比较
// -ne 等于 应用于 整型比较
// -lt 小于 应用于 整型比较
// -gt 大于 应用于 整型比较
// -le 小于或等于 应用于 整型比较
// -ge 大于或等于 应用于 整型比较
// -a 双方都成立(and)
// -o 单方成立(or)
// -z 空字符串
if [ ! -d /data/20231213 ];then
mkdir -p /data/20231213
else
echo "the dir is aleardy exist"
fi
// 多条件测试判断
#!/bin/bash
score=$1
if [ -z $score ];then
echo "usage: { $0 60:80.}"
exit
fi
if [[ $score -gt 85]];then
echo "very good";
elif [[ $score -gt 75]];then
echo "good";
elif [[ $score -gt 60]];then
echo "pass";
else
echo "no pass"
fi
(base) zhanglei@DESKTOP-1L2C7BR:~$ /bin/bash if.sh 90
very good
(base) zhanglei@DESKTOP-1L2C7BR:~$ /bin/bash if.sh 60
pass
4、shell编程之for语句
// 1-100之间求和,变量赋值不能有空格,加法应该用反引号
(base) mordredll@DESKTOP-1L2C7BR:~$ vi for1.sh
#!/bin/bash
j=0
for ((i=1;i<=100;i++))
do
j=`expr $i + $j`
done
echo $j
// 看脚本的执行过程
sh -x for1.sh
// for循环读取文件
(base) mordredll@DESKTOP-1L2C7BR:~$ vi for2.sh
#!/bin/bash
for i in `cat list.txt`
do
scp -r /tmp/test.txt root@$line:/tmp
done <list.txt
5、shell编程之while语句
// while循环
(base) mordredll@DESKTOP-1L2C7BR:~$ vi while1.sh
#!/bin/bash
i=0
while [[ $i -lt 10 ]]
do
echo "the number is $i"
((i++))
done
// while循环
(base) mordredll@DESKTOP-1L2C7BR:~$ vi while2.sh
#!/bin/bash
i=0
while (( $i < 10 ))
do
echo "the number is $i"
((i++))
done
// read 命令
(base) mordredll@DESKTOP-1L2C7BR:~$ read -p "please input number:" input
please input number:123
(base) mordredll@DESKTOP-1L2C7BR:~$ echo $input
123
// while 读取文件内容
(base) mordredll@DESKTOP-1L2C7BR:~$ vi while3.sh
#!/bin/bash
while read line
do
scp -r /tmp/test.txt root@$line:/tmp
done <list.txt
// while 读取第二列内容
(base) mordredll@DESKTOP-1L2C7BR:~$ vi while3.sh
#!/bin/bash
while read line
do
ip=`cat $line |awk '{print $2}'`
scp -r /tmp/test.txt root@$ip:/tmp
done <list.txt