bash shell基本编程

这里主要讲bash shell

变量

等号之间不能有空格,直接定义变量

追加字符串yes:name=me=${name}yes

特殊变量:环境变量

通过env命令查找,其中一个path环境变量,例如执行ls命令,系统就会去找path下/usr/bin下的ls命令(其中有很多命令)。
把普通变量变成环境变量:export CAT_HOME="blue house"

bash shell操作环境

login shell : 取得bash时需要完整的登陆流程
non-login shell : 取得bash接口时不需要完整的登陆
login shell只会读取的配置文件:

  1. /etc/profile: 这是系统整体设置,你最好不要修改这个文件
  2. ~/.bash_profile或者~/.bash_login或者~/.profile:属于用户个人设置,你要改自己的数据,就在这改

non-login shell会读取的配置文件:

  1. ~/.bashrc

一般配置Java的环境变量就在.bashrc或者.bash_profile中,系统启动都会读到

alias/history/clear

ls -al /etc | less 效果等于 ls -al | less /etc ,令alias ll2='ls -al | less' 则效果等于 ll2 /etc/
使用alias命令查看系统的命令别名
使用history命令查看刚才执行了那些命令

写bash脚本

要求:编写一个bash脚本并用三种方式执行,并体会三种方式之间的不同
脚本内容:

#!/bin/bash
# Program:
# This program shows "Hello World!" in your screen.
# History:
# 2018/03/09 twq
echo "Hello World!"
exit 0

执行流程:

[root@localhost ~]# mkdir scripts
[root@localhost ~]# cd scripts/
[root@localhost scripts]# ls
[root@localhost scripts]# vi sh01.sh
[root@localhost scripts]# ls
sh01.sh
[root@localhost scripts]# sh sh01.sh 
Hello World!
[root@localhost scripts]# bash sh01.sh //与上面sh命令执行效果相同
Hello World!
[root@localhost scripts]# source sh01.sh //执行后调到user1用户了
Hello World!
[user1@localhost ~]$ ./sh01.sh //user1没有此脚本执行权限,要回到root
-bash: ./sh01.sh: No such file or directory
[user1@localhost ~]$ su -
Password: 
Last login: Tue Jan 29 14:06:08 CST 2019 on pts/0
[root@localhost ~]# cd scripts/
[root@localhost scripts]# chmod a+x sh01.sh //付给此文件执行权限
[root@localhost scripts]# ls -al
total 4
drwxr-xr-x. 2 root root  21 Jan 29 15:57 .
dr-xr-x---. 3 root root 150 Jan 29 15:56 ..
-rwxr-xr-x. 1 root root 131 Jan 29 15:57 sh01.sh
[root@localhost scripts]# ./sh01.sh //可以执行了
Hello World!
[root@localhost scripts]# source sh01.sh //执行过后还是调到user1,
Hello World!
[user1@localhost ~]$

原因:
其中使用source跳到user1中,是因为脚本中含有echo 0命令

利用sh/bash/.等是在子进程bash中执行脚本
而source是在父进程bash上执行脚本的

再举个例子,执行脚本:

#繁杂省略
read -p "Please input your first name: " firstname #提示使用者输入
read -p "Please input your last name: " lastname #提示使用者输入
echo -e "\nYour full name is: ${firstname} ${lastname}" #屏幕结果输出

执行:

[root@localhost scripts]# vi showname.sh 
[root@localhost scripts]# sh showname.sh 
Please input your first name: zhang
Please input your last name: minghui

Your full name is: zhang minghui
[root@localhost scripts]# echo $firstname //没有输入zhang,是因开辟了新进程,只在新进程执行完了

[root@localhost scripts]# echo $listname

[root@localhost scripts]# source showname.sh 
Please input your first name: zhang
Please input your last name: minghui

Your full name is: zhang minghui
[root@localhost scripts]# echo $firstname //仍有输出,因为是在原来的父进程执行的
zhang
[root@localhost scripts]# echo $lastname
minghui

脚本参数

eg:bash how_paras.sh one two tree

test命令、条件判断结构、循环控制结构以及date命令


注意:test -e /root && echo "exist" || echo "Not exist" 命令等价于[ -e /root ] && echo "exist" || echo "Not exist" 命令

条件判断结构:

执行iftest1.sh:bash iftest1.sh

read -p "Please input (Y|N) : " yn
if [ "${yn}" == "Y" ] || [ "${yn}" == "y" ]; then
echo "OK,continue"
exit 0
fi //结束if
if [ "${yn}" == "N" ] || [ "${yn}" == "n" ]; then
echo "Oh,interrupt!"
exit 0
fi
echo "I don't know what your choice is" && exit 0

执行iftest2.sh:bash iftest2.sh

read -p "Please input (Y|N) : " yn
if [ "${yn}" == "Y" ] || [ "${yn}" == "y" ]; then
echo "OK,continue"
elif [ "${yn}" == "N" ] || [ "${yn}" == "n" ]; then
echo "Oh,interrupt!"
else
echo "I don't know what your choice is"
fi

执行casetest.sh: bash casetest.sh one

echo "This program will print your selection !"
# read -p "Input your choice : " choice # 暂时取消,可以替换!
# case ${choice} in # 暂时取消,可以替换!
case ${1} in # 现在使用,可以用上面两行替换!
"one")
echo "Your choice is ONE"
;;
"two")
echo "Your choice is TWO"
;;
"three")
echo "Your choice is THREE"
;;
*)
echo "Usage ${0} {one|two|three}"
;;
esac

执行functiontest.sh:bash functionit.sh one

function printit() {
        echo "Your choice is ${1}" # 这个$1必须参考下面指令的下达
}
echo "This program will print your selection !"
case ${1} in
"one")
printit 1 #请注意,printit指令后面还有接参数
;;
"two")
printit 2
;;
"three")
printit 3
;;
*)
echo "Usage ${0} {one|two|three}"
;;
esac

循环控制结构:

执行:bash whiletest.sh

执行:bash untiltest.sh

执行:bash fortest.sh
三种写法:

date命令:

输出今天日期:date
输出今天格式化的日期:date +%Y%m%d
输出今天格式化日期精确到秒:date +%Y%m%d%H%M%S
输出前一天日期:date --date='1 days ago',1可以改成别的数字对应几天
输出前一天日期格式化date --date='1 days ago' +%Y%m%d
输出明天日期date --d='1 days' +%Y%m%d--date--d效果一样

往文件中追加内容以及crontab命令

往文件中追加内容除了用vi命令,还可以使用命令往test.txt文件中追加内容“zhanhui”,即echo "zhanhui" >> test.txt,文件存不存在没关系。注意:if是echo "zhanhui" > test.txt,则是将文件内容替换掉,改为zhanhui。
输入crontab -e命令,进入并输入*/1 * * * * /root/scripts/sh01.sh 意思就是每个一分钟执行一次sh01.sh文件脚本,为了方便显示,把它执行的结果打印到一个日志中:*/1 * * * * /root/scripts/sh01.sh >> /root/logs
查看正在执行的任务:crontab -l
删除正在执行的任务:crontab -r 工作中慎用!!!

posted @ 2019-02-27 17:07  AntarcticPenguin  阅读(224)  评论(0编辑  收藏  举报
//开启礼花特效的js