[command]脚本基本知识
来源:互联网 发布:云计算为什么这么火 编辑:程序博客网 时间:2024/06/10 05:55
脚本基本知识
#! /bin/zshexit 0 #退出并传出一个值,可以echo $?显示
说明使用的bash。
根据时间创建文件
#! /bin/zshecho -e "input file name"read fileuserfilename=${fileuser:-"filename"}date1=$(date -v+2d +%Y%m%d)date2=$(date +%Y%m%d)file1=${filename}_${date1}file2=${filename}_${date2}touch "$file1"touch "$file2"
数值计算
只支持整数运算
$(( 计算式 ))ex:echo $((3 + 4))
执行方式的区别
1)直接运行命令行
相当于在父进程中运行程序,变量会保留
2)./script.sh
相当于在子进程中运行程序,变量不会保留。
3)source script.sh
在父进程中运行程序,变量会保留。
test
测试命令
test -e script.sh && echo "exist" || echo "not exist"
-e: 文件名是否存在-f: 文件名是否存在且为file-d: 文件名是否存在且为directory-z string: 判断字符串是否为空-r: 可读-w:可写-x:可执行-a: and-o: or!: negativetest -r filename -a -w filename
还有使用[]来进行判断,注意空格!
# [空格。。。空格op空格。。。空格][ "$name" = "name" ]
file:说明一个文件的类型(是否可执行)
运行参数
$x (x = 1, 2, 3) 表示第几个参数$0 第一个参数前的命令$# 总参数个数$* 所有参数都输出,默认以空格分割。
#! /bin/zshecho "$0"echo "$#"echo "$1"echo "$*"exit 0
./script.sh 1 2 3./script.sh311 2 3
条件判断
在[]中判断,
-eq: 相等-ne: 不想的-gt:大于-lt:小于-ge:大于等于-le:小于等于
if [ $1 -eq 1 ]; then echo -e "1"elif [ "$1" -gt "2" ]; then echo -e "2"fi
case
case $1 in "one") echo "one" ;; "two") echo "two" ;;esac
function
在函数中$1表示第一个参数,$2表示第二个参数,注意这里的$1和运行脚本的$1是不一样的。
function print() { echo -n "print $(($1 + 1))"}case $1 in "1") print 1 ;; "2") print 2 ;;esac
循环的各种做法
#! /bin/zshage=10while [ $age -lt 20 ]do age=$(( age + 1 ))doneecho "$age"until [ $age -eq 25 ]do age=$(( $age + 1))doneecho "$age"for animal in dog cat mousedo echo "$animal"donefor times in $(seq 1 100)do echo "$times"donefilelist=$(ls './')for filename in $filelistdo echo "$filename"donefor ((i=0;i <= 10; i = i + 1))do echo "$i"done
调试
-n:不执行,检查语法错误-v: 在执行前,把内容输出-x:将使用到的内容输出。
sh -n script.sh
阅读全文
0 0
- [command]脚本基本知识
- SHELL 脚本基本知识
- Shell脚本的基本知识
- Shell脚本常识(基本知识)
- Shell脚本常识(基本知识)
- Recovery command 升级命令脚本
- shell基本知识(1)--脚本参数
- shell脚本知识(一) 基本知识预备
- 转载整理-shell脚本的基本知识
- Command Prompt + Perl 脚本编写笔记
- linux bash command bash脚本命令
- shell 脚本 “bc: command not found ” 解决
- jenkins shell脚本执行nvm command失败
- 基本知识
- 基本知识
- 基本知识
- 基本知识
- 基本知识
- Hive on Tez部署及验证测试
- mac终端操作mysql
- xheditor struts2 上传文件
- 二叉查找树(通过外排思想处理大数据)
- (OK) Sample IPv6 Quagga config
- [command]脚本基本知识
- Android Studio中各种jdk和sdk的类找不到,提示setup jdk
- asdas
- AOJ-problem-874
- MyFlag Step9 :Flag功能相关的设计
- 文章标题
- 贪吃蛇游戏的java源码
- 简单的文本查询和替换小例子
- 兔子问题