==>#!/bin/bash
==># Program:
==># This program shows "Hello world!" in your screen.
==># User inout his first name and last name.Program shows his full name.
==># History:
==># 2019/7/12 nexus First release
==># 2019/7/12 nexus Second release
==>
==>
==># echo -e "Hello world! \n"
==># read -p "Please input your first name :" firstname
==># read -p "Please input your first name :" lastname
==># echo -e "\nYour full name is: ${firstname} ${lastname}"
==>
==># if 语句测试
==># if read -t 5 -p "\n请输入您的地址" addr #t---定义输入字符的等待时间 秒
==># then
==># echo -e "\n你输入的地址是${addr}"
==># read -n 1 -p "Are you sure? [Y/N]" ans #n后接输入的字符数
==># case ${ans} in
==># Y|y)
==># echo -e "\nThanks.";;
==># N|n)
==># echo -e "\nSorry.";;
==># *)
==># echo -e "\nErro choice.";; #两个;;
==># esac
==># else
==># echo -e "\nSorry,timeout."
==># fi # 这种语法格式!!!注意
==>
==>
==>
==># 隐藏输入,实质是read命令将文本颜色与背景设置为相同
==># read -s -p "请输入您的密码:" pw
==># echo -e "\n您输入的密码是:${pw}"
==>
==>
==># 1.让使用者输入文件名称,并取得fileuser这个变量。
==># echo -e "I will use 'touch' cimmand 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.txt") # $(command)
==># date2=$(date --date='1 days ago' +"_%Y_%m_%d.txt") # 注意ago' +%之间的空格
==># date3=$(date +"_%Y_%m_%d.txt")
==># file1=${filename}${date1}
==># file2=${filename}${date2}
==># file3=${filename}${date3}
==># # 4.将文件名建立
==># touch "${file1}"
==># touch "${file2}"
==># touch "${file3}"
==>
==># 数值计算
==>
==># echo -e "You should input 2 numbers,I will multiplying them!\n"
==># read -p "first number:" firstnu
==># read -p "second number:" secnu
==># # Total=$((${firstnu}*${secnu})) # var=$((运算内容))默认只支持整数
==># # echo -e "\nThe result of ${firstnu} x ${secnu} is==>${total}"
==># echo -e "\nThe result of ${firstnu} x ${secnu} is==>"
==># echo -e "scale=4; ${firstnu}*${secnu}"|bc # scale设置小数点数
==>
==>
==># test 常用方式
==># 1.文件类型判断
==># test [efd] filename
==># -e 文件名是否存在
==># -f 文件名是否存在且为文件
==># -d 文件名是否存在且为目录
==># 2.文件权限判断
==># test [rwx] filename
==># -r 文件名是否有可读权限
==># -w 文件名是否有可写权限
==># -x 文件名是否有可执行权限
==># 3.文件之间的比较
==># test file1 [nt,ot,ef] file2
==># -nt new than file1比file2新
==># -ot old than file1比file2旧
==># -ef equal file file1与file2为同一文件
==># 4.整数之间的比较
==># test n1 [eq,ne,gt,lt,ge,le] n2
==># -eq equal相等
==># -ne not equal 不等
==># -gt greater than 大于
==># -lt less than 小于
==># -ge 大于等于
==># -le 小于等于
==># 5.判断字符串的数据
==># test -z str 判断字符串是否为空
==># test -n str 判断字符串是否为非空
==># test str1==str2 判断str1是否等于str2
==># test str1!=str2 判断str1是否不等于str2
==># [ str1 == str2 ]
==># 1 2 3 4 一共四个空格,对于这种简单的判断可以用【】
==># [ "${x}" == "${y}" -o "${z}" == "sd" ]
==># [ "${x}" == "${y}" || "${z}" == "sd" ]
==># 一定注意其中的空格!!!!!!!!!!
==>
==># 6.多重条件判断
==># test -r file -a -x file -a(and),同时成立才分会true
==># test -f file -o -x file -0(or),或运算
==># test ! -r file 反相
==>
==>
==># 批量插入步骤
==># step 1:在命令行模式下,将光标固定在第一列,按Ctrl+V快捷键进入VB可视化模式:
==># Step 2:上下移动光标,选择你想要注释的行:
==># Step 3: 选择好之后,按**大写的**I键进入insert模式,输入注释符“//”或“#”:
==># Step 4:最后按ESC键(两次),选择的多行已经注释掉
==>
==># Program:
==># User input a filename,program will check the flowing:
==># 1.)exist? 2.)file/directory? 3.)file permissions
==># History
==># # 1.让使用者输入者输入文件名,并且判断使用者是否为真的有输入字符串?
==># echo -e "Please input a filename, I will check the filename\`s type and permission.\n" #这里`有特殊意义,需要使用转义字符
==># read -p "Input a filename:" filename
==># test -z ${filename} && echo "No 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 -e "The filename:${filename} is a ${filetype}" #echo 自带换行功能
==># echo -e "And the permissions are:${perm}"
==>
==># shell 脚本的默认变量
==># 运行脚本; sh hello.sh opt1 opt2 opt3 opt4
==># ${0} ${1} ${2} ${3} ${4}
==># ${0} 脚本名
==># ${1} 第一个参数
==># ${2} 第二个参数
==># ${3} 第三个参数
==># ${4} 第四个参数
==># $# 参数的个数
==># $@ 代表 "${1}" "${2}" "${3}" "${4}"
==># 对于需要判断输入参数的脚本很有效果!!!
==>echo "脚本名称:${0} 参数个数:$#"
==>
==>
==>
==>exit 0
06-17
“相关推荐”对你有帮助么?
-
非常没帮助
-
没帮助
-
一般
-
有帮助
-
非常有帮助
提交