Shell文件包含

shell也可以包含外部脚本,语法格式如下:

. filename

或

source filename

例如创建两个shell脚本。

脚本1:test1.sh

url = "www.baidu.com"

脚本2:test2.sh

. ./test1.sh

echo "$url"

执行test2.sh,即可看到结果。

Shell输入输出重定向

命令 说明
command>file 将输出重定向到file
command<file 将输入重定向到file
command>>file 将输出以追加的方式重定向到file
n>file 将文件描述符为n的文件重定向到file
n>>file 将文件描述符为n的文件以追加的方式重定向到file
n>&m 将输出文件m和n合并
n<&m 将输入文件m和n合并
<<tag 将开始标记tag和结束tag之间的内容作为输入

注意:文件描述符0通常是标准输入(STDIN),1是标准输出(STDOUT),2是标准错误输出(STDERR)

重定向深入讲解

一般情况下,每个Unix/Linux命令运行时都会打开三个文件:

  • 标准输入文件(stdin):stdin的文件描述符为0,Unix程序默认从stdin读取数据。
  • 标准输出文件(stdout):stdout的文件描述符为1,Unix程序默认向stdout输出数据
  • 标准错误文件(stderr):stderr的文件描述符为2,Unix程序会向stderr流中写入错误信息。

默认情况下,command>file将stdout重定向到file,command<file将stdin重定向到file。

如果希望stderr重定向到file,可以这样写:

command 2>file  #2表示标准错误文件

command 2>>file  #追加 2表示标准错误文件

如果希望将stdout和stderr合并后重定向到file,可以这样写

command > file 2>&1

command >> file 2>&1

Shell函数

格式

[function] funname [()]

{

  action;

  [return int;]  #return后跟数值n(0-255)

}

例子

demoFun(){

  echo "这是我的第一个shell函数"

}

echo "--start--"

demoFun

echo "--end--"

例子:带有return

funWithRetun(){

  echo "输入第一个数字"

  return aNum

  echo "输入第二个数字"

  return bNum

  return $(($aNum+$bNum))

}

funWithReturn

echo "输入的两个数字之和为$?"  #函数返回值在调用该函数后通过$?来获得

例子:函数参数

funWithParam(){

  echo "第一个参数为$1"  #$1表示第一个参数,$2表示第二个参数,获取大于等于10个参数需用${n}

  echo "第五个参数为$5"

  echo "第十个参数为${10}"

  echo "第十四个参数为${14}"

  echo "所有参数$*"

}

funWithParam 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

参数处理 说明
$# 传递到脚本的参数个数
$* 以一个单字符串显示所有向脚本传递的参数
$$ 脚本运行的当前进程ID号
$! 后台运行的最后一个进程的ID号
$@ 与$*相同,但是使用时加引号,并在引号中返回每个参数
$- 显示shell使用的当前选项,与Set命令功能相同
$? 显示最后命令的退出状态。0表示没有错误,其他任何值表明有错误

Shell流程控制

if else

格式:

if condition

then

  command1

  command2

fi

if else-if else

格式

if condition1

then

  command1

elif condition2

then

  command2

else

  commandN

fi

for循环

格式

for var in item1 item2 ... itemN

do

  command1

...

  commandN

done

例子

for loop in 1 2 3 4 5

do

  echo "the value is : $loop"

done

while

格式

while condition

do

  command

done

例子

int=1

while(($int<=5))

do

  echo $int

  let "int++"

done

无限循环

while :

do

  command

done

while true

do

  command

done

until循环

until condition

do

  command

done

case

case 值 in

模式1)

  command1

  command2

  ...

  commandN

  ;;

模式2)  

  command1

  command2

  ...

  commandN

  ;;

esac

跳出循环

break

continue

Shell脚本学习第二课·的更多相关文章

  1. shell脚本学习第一课

    shell是一种程序设计语言,是访问操作系统内核的服务. Linux的shell种类常见的有: Bourne Shell(/usr/bin/sh或/bin/sh) Bourne Again Shell ...

  2. 转 shell脚本学习指南

    shell脚本学习指南 以下八点不敢说就能成为你shell脚本学习指南de全部,至少可以让你编写出可靠的shell脚本. 1. 指定bashshell 脚本的第一行,#!之后应该是什么?如果拿这个问题 ...

  3. Shell脚本学习指南笔记

    Shell脚本学习指南 作者:Danbo 2015-8-3 脚本编程语言与编译型语言的差异 许多中型.大型的程序都是用编译型语言写的,例如:C.C+.Java等.这类程序只要从源代码(Source C ...

  4. shell脚本学习之6小时搞定(1)

    shell脚本学习之6小时搞定(1) 简介 Shell是一种脚本语言,那么,就必须有解释器来执行这些脚本. Unix/Linux上常见的Shell脚本解释器有bash.sh.csh.ksh等,习惯上把 ...

  5. 笔记——shell脚本学习指南

    <shell脚本学习指南>机械工业出版 ISBN 987-7-111-25504-8 第2章 2.4 初级陷阱 1.当今的系统,对#!这一行的长度限制从63到1024个字符都有,尽量不要超 ...

  6. Shell 脚本学习资料搜集

    Shell文档 ChinaUnix上大神“網中人”总结的Shell十三问,强烈推荐,这本书讲得比较精炼,而且都是一些Shell学习中容易把握不住的一些细节难点.每一问都写得非常精彩.ChinaUnix ...

  7. 学习笔记之Shell脚本学习指南 & sed与awk & 正则表达式

    正则表达式_百度百科 http://baike.baidu.com/link?url=ybgDrN2WQQKN64_gu-diCqdeDqL8LQ-jiQ-ftzzPaNUa9CmgBRDNnyx50 ...

  8. shell脚本学习之$0,$?,$!等的特殊用法

    变量说明: $$ Shell本身的PID(ProcessID) $! Shell最后运行的后台Process的PID $? 最后运行的命令的结束代码(返回值) $- 使用Set命令设定的Flag一览 ...

  9. Shell脚本学习 - 运算符

    继续shell脚本学习.上一篇是基本数据类型和语法的总结,这一篇是运算相关的操作. 运算符 bash不支持简单的数学计算,需要依赖其他命令实现. expr可以代为实现. # 表达式一般这么写 ` + ...

随机推荐

  1. rsync同步

    本地同步: rsync -avz /boot /test C/S架构: 远程同步:rsync+ssh 远程浏览器目录文件:rsync  用户@192.168.0.250:/boot 下行:rsync ...

  2. What's new in Windows 10 Enterprise with Microsoft Edge.(Windows 10 新功能)

    What's new in Windows 10 Enterprise with Microsoft Edge --带有Edge浏览器的Windows 10 企业版的新功能 本文摘录自公司群发邮件, ...

  3. 刚接触Linux,菜鸟必备的小知识点(一)

    身为一个将要大四的学生,而且还是学计算机的没有接触过linux简直是羞愧难当.这个假期做了一个软件测试员,必须要熟悉linux的操作,所以对于我这个菜鸟我也就说几点比较重要的小知识点吧. 第一.cd指 ...

  4. JS和JSON的区别

    JSON(JavaScript Object Notation)是一种轻量级的数据交换格式,JSON格式的数据,主要是为了跨平台交流数据用的.但JSON和JavaScript确实存在渊源,可以说这种数 ...

  5. pcDuino-V2利用madplay播放音乐

    在pcDuino的UBUNTU系统下,打开控制台,利用apt-get来下载madplay软件. sudo apt-get install madplay 播放音乐: madplay xxx.mp3 x ...

  6. [LeetCode] Number of Segments in a String 字符串中的分段数量

    Count the number of segments in a string, where a segment is defined to be a contiguous sequence of ...

  7. [LeetCode] Find Leaves of Binary Tree 找二叉树的叶节点

    Given a binary tree, find all leaves and then remove those leaves. Then repeat the previous steps un ...

  8. [LeetCode] Number of 1 Bits 位1的个数

    Write a function that takes an unsigned integer and returns the number of ’1' bits it has (also know ...

  9. 一个简单oop的changeTab

    好多地方都会用到这样一个效果“点击tab切换内容页”,根据自己的想法实现了一下,写了个简单的插件.以前写代码都是标准的函数式编程,现在觉得面向对象编程看起来比较爽,并且更容易维护,于是就用oop的思想 ...

  10. Javascript身份证号码验证

    "来来来,坐这儿". "什么?我可是有身份的人,怎么能和你坐一块儿".沛笠晃了晃手里的身份证,不屑说道. "你咋不上天呢?有身份还喝油条吃豆浆&quo ...