脚本最好都放在/usr/local/sbin中

脚本的执行 sh -x 脚本.sh -x可以查看执行过程
1在脚本中使用变量 使用变量的时候,需要使用$符号:
 #!/bin/bash
 ##把命令赋值为变量,需要使用反引号
 d=`date +"%H:%M:%S"`
 echo "The script begin at $d"
 echo "Now we'll sleep 2 seconds"
 sleep 2
 d1=`date +"%H:%M:%S"`
 echo "The script end at $d"
2在脚本中使用数学运算要用[]括起来如下
#! /bin/bash
 
a=1
b=2
##数学运算用[]括起来
sum=$[$a+$b]
echo "$a + $b = $sum"
3在脚本中和控制台交互 使用read命令
#! /bin/bash
 
read -p "Please input a number: " x
read -p "Please input a number: " y
sum=$[$x+$y]
echo "The sum of the tow numbers is : $sum"
4shell中的预设变量
#! /bin/bash
 
echo "$1 $2 $3 $0"
1 2 就是脚本中的预设变量 一个脚本中的预设变量是没有限制的,0表示脚本文件本书
# sh option.sh 1 2 3
执行此命令输出内容如下所示:
1 2 3 option.sh
 
5shell中的逻辑判断
不带else的if 注意if后面的判断语句要用(())否则会报错
#! /bin/bash
 
read -p "Please input your score: " a
if ((a<60)); then
    echo "You didn't pass the exam."
fi
5.1带else 实例代码如下
#! /bin/bash
 
read -p "Please input your score: " a
if ((a<60)); then
    echo "You didn't pass the exam."
else
    echo "Good! You passed the exam."
fi
5.2带有else if(这是c中的说法)在shell中表示为elif
#! /bin/bash
 
read -p "Please input your score: " a
if ((a<60)); then
    echo "You didn't pass the exam."
elif ((a>=60)) && ((a<85)); then
    echo "Good! You passed the exam."
else
    echo "Very good! Your score is very heigh"
fi
5.3case 逻辑判断
#! /bin/bash
 
read -p "Input a number: " n
a=$[$n%2]
case $a in
1)
    echo "The number is odd."
    ;;
0)
    echo "The number is even."
    ;;
*)
    echo "It's not a number"
    ;;
esac
* 表示其他值
6for循环
实例代码如下:
#! /bin/bash
 
for file in `ls`;do
    echo $file
done
7while 循环
#! /bin/bash
 
a=5
while [ $a -ge 1 ]; do
    echo $a
    a=$[$a-1]
done
 
 
 
 
 

linux 学习随笔-shell简单编写的更多相关文章

  1. linux 学习随笔-shell基础知识

    1:用户的shell历史命令保存在home/username/.bash_history中 #!!  执行用户的上一条命令 #!pw  执行命令历史中最近一次以pw开头的命令 2:'*'来匹配零或多个 ...

  2. Linux 学习 (八) Shell

    Linux达人养成计划 I 学习笔记 Shell 是什么: Shell 是一个命令解释器 Shell 还是一个功能相当强大的编程语言,易编写,易调试,灵活性较强 Shell 的分类: Bourne S ...

  3. (零)linux 学习 -- 从 shell 开始

    The Linux Command Line 读书笔记 - 部分内容来自 http://billie66.github.io/TLCL/book/chap02.html 文章目录 前言 什么是 she ...

  4. linux 学习10 shell 基础

    10.1 Shell概述 .Shell是什么 Shell是一个命令行解释器,它为用户提供了一个向Linux内核发送请求以便运行程序的界面系统级程序,用户可以用Shell来启动.挂起.停止甚至是编写一 ...

  5. (原创)鸟哥linux学习script shell相关笔记

    在使用鸟哥linux进行script shell学习的过程中碰到一些不太明白的知识点,在这里进行一些记录 1. [root@www scripts]# vi sh03.sh #!/bin/bash # ...

  6. Linux学习——自定义shell终端提示符

    转自:here 我使用的Linux发行版是LinuxMint 17.2 Rafaela,默认情况下Terminal中的shell提示包括了用户名.主机名.当前目录(绝对路径)和提示符.这样会导致当进入 ...

  7. (六)linux 学习 -- 从 shell 眼中看世界

    The Linux Command Line 读书笔记 - 部分内容来自 http://billie66.github.io/TLCL/book/chap08.html 文章目录 字符展开 `*` 路 ...

  8. Linux学习之Shell编程基础

    转自:http://my.oschina.net/itblog/blog/204410 1 语法基本介绍1.1 开头 程序必须以下面的行开始(必须方在文件的第一行): #!/bin/sh 符号#!用来 ...

  9. linux学习总结----shell编程

    ## 环境变量 ## 全局变量 ``` 常见的全局环境变量 PATH 指令的搜索路径 HOME 用户的家目录 LOGNAME 登录名 SHELL 脚本的类型 使用全局环境变量 echo $PATH 自 ...

随机推荐

  1. How to implement a neural network

    神经网络的实践笔记 link: http://peterroelants.github.io/posts/neural_network_implementation_part01/ 1. 生成训练数据 ...

  2. PHP过滤各种HTML标签

    $str=preg_replace("/<\s*img\s+[^>]*?src\s*=\s*(\'|\")(.*?)\\1[^>]*?\/?\s*>/i&q ...

  3. Java 理论与实践: 正确使用 Volatile 变量--转

    原文地址:http://www.ibm.com/developerworks/cn/java/j-jtp06197.html Java 语言中的 volatile 变量可以被看作是一种 “程度较轻的  ...

  4. 关于 android 的 view.getLeft(), getRight(), getTop(), getBottom() 的一些疑惑(坑)解答

    (原创) 今天在做下滑刷新的时候碰到 view 的四个 get 函数有点特别,具体遇到的问题如下,经反复测试和查找资料,填坑如下: 1,为什么我有时候在使用getLeft(), getRight(), ...

  5. 【SQLServer】DBHelper即C#数据库底层封装

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.C ...

  6. 性能测试工具Locust

    An open source load testing tool. 一个开源性能测试工具. define user behaviour with python code, and swarm your ...

  7. Angularjs CURD

    前言 基于一个手机端的项目使用了angularjs,硬着头皮去用,有很多的疑问还需要一一去验证,刚开始总是感觉找不到北,总是感觉有很多概念,而且似乎ng既夹杂MVC又夹杂MVVM的思想, 忙里偷闲敲了 ...

  8. spring boot 添加拦截器

    构建一个spring boot项目. 添加拦截器需要添加一个configuration @Configuration @ComponentScan(basePackageClasses = Appli ...

  9. Python_Day_05 计数器(counter),有序字典(OrderDict),默认字典(defaultdict),可命名元祖(namedtuple),双向队列(deque),单项队列(deuqe.Queue)

    Counter(计数器) 是一个字典的子类,存储形式同样为字典,其中存储的键为字典的元素,值为元素出现的次数,在使用之前我们需要先导入文件 import collections 初始化一个计数器 im ...

  10. C#遍历文件夹下所有文件

    FolderForm.cs的代码如下: using System; using System.Collections.Generic; using System.Diagnostics; using ...