猫宁~~~

建议全程在centos7中进行,我在kali linux中有些命令无法执行。

1~家目录下创建bin文件,test.sh文件在bin目录

下面的shell代码打印Hello World!

#!/bin/bash
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH
echo -e "Hello China! \a \n"
exit 0

2~输入名称,变量前空格一定要严格空出来哦

#!/bin/bash
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH
read -p "Please input your first name:" firstname
read -p "Please input your last name:" lastname
echo -e "\nYour full name is:${firstname} ${lastname}"

sh test.sh

echo ${firstname} ${lastname},空

source test.sh

echo ${firstname} ${lastname},不空

3~批量创建文件,自定义名称之后添加时间,设置大量变量

不定义文件名前缀,默认是filename,直接回车即可

#!/bin/bash
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH
echo -e "I will use 'touch' command to create 3 files."
read -p "Please input your filename:" fileuser
filename=${fileuser:-"filename"}
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}
touch "${file1}"
touch "${file2}"
touch "${file3}"

4~两个数相乘运算

#!/bin/bash
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH
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}))
echo "\nThe result of ${firstnu}*${secnu} is ==> ${total}"

echo $((120*120))   乘积,整数计算

echo $((13%3))   取余

echo "1.2*1.2" | bc   小数计算,添加bc,bc在centos7上可以用,但是在kali linux上不可以

5~计算π的值,4*a(1)是bc提供的计算π的函数

#!/bin/bash
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH
echo -e "This program will calculate pi value. \n"
echo -e "You should input a float number to calculate pi value. \n"
read -p "The scale number (10~10000):" checking
num=${checking:-"10"}
echo "Starting calcuate pi value. Be patient."
time echo "scale=${num}; 4*a(1)" | bc -lq

6~test判断

目录是否存在

在/root下创建test目录

test -e /test && echo "exist" || echo "Not exist"

打印exist

test -e /root/test && echo "exist" || echo "Not exist"

打印Not exist

-e   文件或目录

-f   针对文件存在与否

-d   针对目录存在与否

文件权限相关

test -s /root/test.html && echo "exist" || echo "Not exist"

-r   可读

-w   可写

-x   可执行

-s   文件是否为空

文件比较

test1.html更新,打印exist

test test1.html -nt test.html && echo "exist" || echo "Not exist"

-nt    更新

-ot   更旧

-ef   是否为同一个文件

整数间判定

test 1 -eq 1 && echo "exist" || echo "Not exist"   打印exist

-eq  相等

-ne   不等

-gt   前者大于后者

-lt   前者小于后者

-ge   前者大于等于后者

-le   前者小于等于后者

字符串判定

test -z 111 && echo "exist" || echo "Not exist"   打印Not exist

-z   字符串为空显示exist,其他显示Not exist

-n   字符串为空显示Not exist,其他显示exist

test 111 == 111 && echo "exist" || echo "Not exist"

111 == 111   相等,打印exist,不相等,打印Not exist

111 != 111   相等,打印Not exist,不相等,打印exsit

多重条件判定

-rw-r--r--. 1 root root 5556 Oct 11 20:49 test.html

test -r test.html -a -x test.html && echo "exist" || echo "Not exist"   打印Not exist

test ! -x test.html && echo "exist" || echo "Not exist"   没有执行权限,打印exist

-a   and关系

-o   or关系

!   相反

#!/bin/bash
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH
echo -e "Please input a filename,I wiil check the filename's type and permission. \n\n"
read -p "Input a filename:" filename
test -z ${filename} && echo "You must input a filename." && exit 0
test ! -e ${filename} && echo "The filename '${filename}' DO NOT exist" && exit 0
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"
echo "The filename:${filename} is a ${filetype}"
echo "And the permissions for you are:${perm}"

文件名为空显示

You must input a filename.

目录

Input a filename:test
The filename:test is a directory
And the permissions for you are:readable writable executable

文件不存在

Input a filename:120
The filename '120' DO NOT exist

文件存在

Input a filename:test.html
The filename:test.html is a regulare file
And the permissions for you are:readable writable

7~判断符号

[ "abcd" == "abcd" ] ; echo $?   打印0

[ "abcd" == "abc" ] ; echo $?   打印1

#!/bin/bash
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH
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 0
echo "I don't know what your choice is" && exit 0

 8~默认变量

[root@pjzhang ~]# file /etc/init.d/network
/etc/init.d/network: Bourne-Again shell script, ASCII text executable

文件是可执行脚本

/etc/init.d/network   stop,start,status

$0   脚本文件名

$#   后接参数个数

$@   参数全部内容,加双引号

${1}   第一个参数

#!/bin/bash
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH
echo "The script name is ==>${0}"
echo "Total parameter number is ==>$#"
[ "$#" -lt 2 ] && echo "The number of parameter is less than 2. Stop here." && exit 0
echo "Your whole parameter is ==>'$@'"
echo "The 1st parameter ==>${1}"
echo "The 2st parameter ==>${2}"

第一次偏移掉1个,第二次在第一次基础上偏移掉3个,从前向后

#!/bin/bash
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH
echo "Total parameter number is ==>$#"
echo "Your whole parameter is ==>'$@'"
shift
echo "Total parameter number is ==>$#"
echo "Your whole parameter is ==>'$@'"
shift 3
echo "Total parameter number is ==>$#"
echo "Your whole parameter is ==>'$@'"

over~~~

PJzhang:鸟哥的linux私房菜-shell脚本-上的更多相关文章

  1. 鸟哥的 Linux 私房菜Shell Scripts篇(一)

    参考: http://linux.vbird.org/linux_basic/0340bashshell-scripts.php#script_be http://www.runoob.com/lin ...

  2. 鸟哥的 Linux 私房菜Shell Scripts篇(四)

    12.4 条件判断式 只要讲到『程式』的话,那么条件判断式,亦即是『 if then 』这种判别式肯定一定要学习的!因为很多时候,我们都必须要依据某些资料来判断程式该如何进行.举例来说,我们在上头的a ...

  3. 鸟哥的 Linux 私房菜Shell Scripts篇(三)

    参考: http://linux.vbird.org/linux_basic/0340bashshell-scripts.php#script_be http://www.runoob.com/lin ...

  4. 鸟哥的 Linux 私房菜Shell Scripts篇(二)

    参考: http://linux.vbird.org/linux_basic/0340bashshell-scripts.php#script_be http://www.runoob.com/lin ...

  5. 《鸟哥的linux私房菜》 - linux命令温故而知新

    在公司的某角落里,看到了<鸟哥的linux私房菜>,顿时想看看是什么鬼. 其他时候还要自己去买才有,现在正好,比图书馆方便.看完了,写点啥! 编辑器很重要,一个vim就主要是我的使用方向: ...

  6. 鸟哥的Linux私房菜笔记第四章

    前言 对着<鸟哥的Linux私房菜-基础版>做了简化笔记.不想让自己知其然而不知其所然.所以写个博客让自己好好巩固一下,当然不可能把书中的内容全部写下来.在这里就简化一点把命令写下来. 让 ...

  7. 《鸟哥的Linux私房菜》Chapter11 20180726~20180806

    目录 1.认识Bash这个shell 1.1.硬件.核心与shell 1.2.系统的合法shell和/etc/shells功能 1.3.Bash shell的功能 1.3.1.命令修编功能 1.3.2 ...

  8. 【鸟哥的Linux私房菜】笔记1

    Linux是什么 从操作系统与cpu架构关系到linux  Richard Mathew Stallman GPL 关于GNU计划 Linux的发展 Linux的核心版本 Linux的特色 Linux ...

  9. linux学习书籍推荐《鸟哥的Linux私房菜》下载

    下载地址:点我 <鸟哥的Linux私房菜:基础学习篇>是具有知名度的Linux入门书<鸟哥的Linux私房菜基础学习篇>的最新版,全面而详细地介绍了Linux操作系统.< ...

随机推荐

  1. windows-服务器-配置一个及多个-Apache-Tomcat

    问题:如何在一台服务器上发布了几个Tomcat的系统???怎么配置环境变量?怎么设置Tomcat? 2020/8月更新,由于之前的java的环境变量有点绕,此次新加一个流程设计图 一.需求: 一台wi ...

  2. P2414 [NOI2011]阿狸的打字机 AC自动机

    题意 给定n个模式串,有m个询问,每次询问第X个模式串在第Y个模中出现了多少次 解题思路 以fail树相反的方向建一棵树T,问题转化为X的子树中有多少个y的终止节点.跑出T的dfs序,X的子树就可以表 ...

  3. BasicInterpreter1.00 运行简单Basic脚本 打印变量及字符串

    源码下载:https://files.cnblogs.com/files/heyang78/basicInterpreter-20200529-1.rar 脚本: count= print(count ...

  4. 在Python程序中执行linux命令

    import commands print commands.getstatusoutput('ls') 输出: (0, '1.py\nwork.nfs') 参考文档: https://blog.cs ...

  5. Zookeeper高级

    1.1. 一致性协议概述 前面已经讨论过,在分布式环境下,有很多不确定性因素,故障随时都回发生,也讲了CAP理论,BASE理论 我们希望达到,在分布式环境下能搭建一个高可用的,且数据高一致性的服务,目 ...

  6. matplotlib | Python强大的作图工具,让你从此驾驭图表

    今天是数据处理专题的第9篇文章,在之前的8篇文章当中我们已经介绍完了pandas这个库的一些基本用法,我们先把一些冷门的高级用法放一放,先来给大家介绍一下另外一个很有用的数据分析库--matplotl ...

  7. 5分钟掌握企业LVM磁盘划分

    逻辑卷管理LVM是一个多才多艺的硬盘系统工具.无论在Linux或者其他类似的系统,都是非常的好用.传统分区使用固定大小分区,重新调整大小十分麻烦.但是,LVM可以创建和管理“逻辑”卷,而不是直接使用物 ...

  8. Python 3 列表

    列表:是可变的序列,也是一种可以存储各种数据类型的集合,用中括号([])表示列表的开始和结束,元素之间用逗号(,)分隔.列表中每个元素提供一个对应的下标. 1.列表的基本格式表示: 2.列表的不同数据 ...

  9. ctfhub 报错注入

    payload   1 Union select count(*),concat((查询语句),0x26,floor(rand(0)*2))x from information_schema.colu ...

  10. 【开发总结】order by 为什么没有走索引?

    1.  现象 表结构如下 CREATE TABLE `ACT_HI_INST` ( `ID` varchar(64) COLLATE utf8_bin NOT NULL COMMENT '主键', ` ...