Linux shell script 1 basic scripts
来源:互联网 发布:微传单软件 编辑:程序博客网 时间:2024/06/07 02:07
linux shell script的功能:
通过整合连续命令,实现自动化管理。并进行简单的数据处理。
执行方法:
- 标明绝对路径or相对路径
-把shell.sh放在环境变量PATH制定路径下
bash shell.shsh shell.sh
第一个shell script:
[root@www scripts]# vi sh01.sh#!/bin/bash#声明script使用的shell#Program:# This program shows "Hello World!" in the screen.#History#2017/08/29 writer first releasePATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/binexport PATHecho -e "Hello World! \a \n"exit 0
为了保证良好的使用,在script前标明如下内容:
-script 的功能;
- script 的版本资讯;
- script 的作者与联络方式;
- script 的版权宣告方式;
- script 的 History (历史纪录);
- script 内较特殊的命令,使用『绝对路径』的方式来下达;
- script 运行时需要的环境变量预先宣告与配置。
从键盘取字:read
[root@www scripts]# vi sh02.sh#!/bin/bash# Program:# User inputs his first name and last name. Program shows his full name.# History:# 2017/08/30 salarydate First releasePATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/binexport PATHread -p "Please input your first name: " firstname # 提示使用者输入read -p "Please input your last name: " lastname # 提示使用者输入echo -e "\nYour full name is: $firstname $lastname" # 结果由萤幕输出
利用date创建文件(根据日期命名文件,帮助备份数据)
空格对bash的影响非常大,一定要注意空格
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/binexport PATH# 1. 让使用者输入文件名称,并取得 fileuser 这个变量;echo -e "I will use 'touch' command to create 3 files." # 纯粹显示资讯read -p "Please input your filename: " fileuser # 提示使用者输入# 2. 为了避免使用者随意按 Enter ,利用变量功能分析档名是否有配置?filename=${fileuser:-"filename"} # 开始判断有否配置档名# 3. 开始利用 date 命令来取得所需要的档名了;date1=$(date --date='2 days ago' +%Y%m%d) # 前两天的日期date2=$(date --date='1 days ago' +%Y%m%d) # 前一天的日期date3=$(date +%Y%m%d) # 今天的日期file1=${filename}${date1} # 底下三行在配置档名file2=${filename}${date2}file3=${filename}${date3}# 4. 将档名创建吧!touch "$file1" # 底下三行在创建文件touch "$file2"touch "$file3"
数值运算
-declare 定义变量类型为整数以后,可以进行加减运算
-$((计算式))进行数值运算
-bash shell默认仅支持到整数的数据
#!/bin/bash# Program:# User inputs 2 integer numbers; program will cross these two numbers.# History:# 2017/08/23 let bless for salary First releasePATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/binexport PATHecho -e "You SHOULD input 2 numbers, I will cross them! \n"read -p "first number: " firstnuread -p "second number: " secnutotal=$(($firstnu*$secnu))echo -e "\nThe result of $firstnu x $secnu is ==> $total"
script 运行差异(source, sh script, ./script)
直接方式运行script
$sh sh02.sh
利用bash或sh下达命令时,script会使用一个新的bash环境来运行脚本内的命令。子程序中的环境变量不会export到父程序中。
所以执行完脚本后,环境变量并不会在父环境中实现
用source执行脚本
$source sh02.sh
shell 判断
用test判断
$ test -e/mejiyabakunei && echo "exist" || echo "not exist"#判断是否存在名为majiyabakunei的文件
利用test可以检查
- 某个文档是否存在 -e
- 某个文档是否存在并且为某个类型 -f(文件) -d(目录)
#!/bin/bash# 1. 让使用者输入档名,并且判断使用者是否真的有输入字串?echo -e "Please input a filename, I will check the filename's type and \permission. \n\n"read -p "Input a filename : " filenametest -z $filename && echo "You MUST input a filename." && exit 0# 2. 判断文件是否存在?若不存在则显示信息并结束脚本test ! -e $filename && echo "The filename '$filename' DO NOT exist" && exit 0# 3. 开始判断文件类型与属性test -f $filename && filetype="regulare file"test -d $filename && filetype="directory"test -r $filename && perm="readable"test -w $filename && perm="$perm writable"test -x $filename && perm="$perm executable"# 4. 开始输出资讯!echo "The filename: $filename is a $filetype"echo "And the permissions are : $perm"
判断符号[]
[ "$HOME" == "$MAIL" ]
- 每一个组件都需要空白键隔开
- 每个变量都需要双引号括起来
- 常数也需要用单或双引号括起来
read -p "Please input (Y/N): " yn[ "$yn" == "Y" -o "$yn" == "y" ] && echo "OK, continue" && exit 0[ "$yn" == "N" -o "$yn" == "n" ] && echo "Oh, interrupt!" && exit 0echo "I don't know what your choice is" && exit 0
-o用来连接两个判断,表示无论大小写的Y都可以识别
shell script的默认变量
echo "The script name is ==> $0"# $0:表示文档名echo "Total parameter number is ==> $#"# $#:表示脚本运行时,后接的参数的个数[ "$#" -lt 2 ] && echo "The number of parameter is less than 2. Stop here." \ && exit 0echo "Your whole parameter is ==> '$@'"# $@:输出脚本运行时后面接的所有参数echo "The 1st parameter ==> $1"# $1:输出第一个参数echo "The 2nd parameter ==> $2"# $2:输出第二个参数
运行脚本后会输出脚本中相关变量参数
[root@www scripts]# sh sh07.sh theone haha quotThe script name is ==> sh07.sh <==档名Total parameter number is ==> 3 <==果然有三个参数Your whole parameter is ==> 'theone haha quot' <==参数的内容全部The 1st parameter ==> theone <==第一个参数The 2nd parameter ==> haha <==第二个参数
阅读全文
0 0
- Linux shell script 1 basic scripts
- Linux & Shell (2) Write basic shell scripts
- linux shell scripts
- 【Linux】bash shell scripts
- LINUX(4) shell scripts
- linux shell script 例子1
- 【Unix/Linux.Shell Script】Shell & Shell Script
- shell scripts -1
- LINUX shell script 打包
- linux shell script 入门
- linux shell script 函式
- linux shell script
- Linux shell script
- [linux]shell script 随记
- Linux Shell Script Study
- FTP linux shell script
- linux-shell script
- Linux Shell Script目录
- CAS实现单点登录SSO执行原理探究(终于明白了)
- 栈,队列和双端队列的操作
- Spring Boot入门教程-FreeMarker模板
- Android Studio教程:启动时不自动打开项目
- 我如何从一个异步调用返回响应?
- Linux shell script 1 basic scripts
- Oracle之快速复制表-yellowcong
- 修改kafka_run_class.bat并增加JRE_HOME判断
- HTML5客户端数据存储Web Storage——localStorage与sessionStorage
- Hibernate与 MyBatis的比较
- MySQL性能利器:innotop工具
- replace与replaceAll的区别
- JS实现加密以及cookie保存(不经过后台,直接在页面操作)
- php对象传值、对象克隆