菜鸟的《Linux程序设计》学习—shell script
1. 认识shell script
2. shell script程序编写
[yr@localhost shellscript]$ vim helloworld.sh //在打开的文本中,编辑程序,helloworld.sh #!/bin/bash
#Program:
# This is my first shell script program. It will show "Hello World!" on
# the screen.
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH
echo -e "Hello World! \a \n"
exit 0
程序内容解析:
3. 程序的编译运行
[yr@localhost shellscript]$ sh helloworld.sh
Hello World!
[yr@localhost shellscript]$ chmod +x helloworld.sh
(2)使用source filename 或者. filename 执行—— 父进程执行
[yr@localhost shellscript]$ vim helloworld.sh //在打开的文本中,编辑程序,helloworld.sh #!/bin/bash
#Program:
# This is my first shell script program. It will show "Hello World!" on
# the screen.
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH
echo -e "Hello World! \a \n"
# 删除最后一行 exit 0
下面执行命令:
[yr@localhost shellscript]$ source helloworld.sh Hello World!
<pre code_snippet_id="666753" snippet_file_name="blog_20150514_3_8535676" name="code" class="cpp">[yr@localhost shellscript]$ . helloworld.sh Hello World!
我们看到,以上两种方式,都可以顺利成功的将脚本和helloworld.sh执行完毕。
4. 两种编译方式详解
[yr@localhost shellscript]$ vim test.sh //下面是程序内容
#!/bin/bash
#Program
# User inputs his first name and last name. Program shows his full name.
#History:
# 2015/05/14 shine_yr First release
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" #结果在屏幕输出 # exit 0 我们不用退出命令
首先,利用直接执行的方式来执行脚本:
[yr@localhost shellscript]$ sh test.sh
Please input your first name: shine
Please input your last name: yr Your full name is: shine yr
[yr@localhost shellscript]$ echo $firstname [yr@localhost shellscript]$ echo $lastname [yr@localhost shellscript]$
从上面可以看出,程序顺利执行,然后我利用echo命令打算输出firstname以及lastname的内容,结果却输出为空。
然后,使用source filename 或者 . filename的方式执行脚本:
[yr@localhost shellscript]$ source test.sh
Please input your first name: shine
Please input your last name: yr Your full name is: shine yr
[yr@localhost shellscript]$ echo $firstname
shine
[yr@localhost shellscript]$ echo $lastname
yr
[yr@localhost shellscript]$
我们可以看到,此时变量firstname以及lastname 中是有确切值的。
5. 编写shell script的良好习惯
菜鸟的《Linux程序设计》学习—shell script的更多相关文章
- 菜鸟教程之学习Shell script笔记(上)
菜鸟教程之学习Shell script笔记 以下内容是,学习菜鸟shell教程整理的笔记 菜鸟教程之shell教程:http://www.runoob.com/linux/linux-shell.ht ...
- linux基础之Shell Script入门介绍
本文介绍下,学习shell script编程的入门知识,通过几个入门实例,带领大家走进shell script的神圣殿堂,呵呵,有需要的朋友参考下. 本文转自:http://www.jbxue.com ...
- 菜鸟教程之学习Shell script笔记(下)
菜鸟教程Shell script学习笔记(下) 以下内容是学习菜鸟教程之shell教程,所整理的笔记 菜鸟教程之shell教程:http://www.runoob.com/linux/linux-sh ...
- 菜鸟教程之学习Shell script笔记(中)
菜鸟教程Shell script学习笔记(中) 以下内容是学习菜鸟教程之shell教程,所整理的笔记 菜鸟教程之shell教程:http://www.runoob.com/linux/linux-sh ...
- bash shell学习-shell script基础 (笔记)
A chain no stronger than its weakest link. "一着不慎,满盘皆输" 参考资料:鸟哥的Linux私房菜 基础学习篇(第三版) Linux ...
- Linux程序设计之shell程序设计
看了<linux程序设计(第4版)>(作者:Neil Matthew ,Richard Stones ,陈建 ,宋健建译).做个笔记,以备后面查看. 首先,清楚几个概念. shell编程属 ...
- Linux基础之-shell script(变量,运算符,流程控制,函数)
一.shell script Shell 脚本(shell script),是一种为shell编写的脚本程序.业界所说的shell通常都是指shell脚本,但读者朋友要知道,shell和shell s ...
- Linux程序设计学习笔记(独乐乐版)
在Android的开发过程中经常会遇到Linux相关的问题,为了更彻底的了解Linux准备整点没用的,找到一本 <Linux程序设计>开始系统的学习. 期间记录下自认为重要的内容,本以为是 ...
- Linux08--Shell程序设计03 shell script
第一个Shell脚本——HelloWorld [root@localhost ~]# vi sh01.sh #!/bin/bash #!表明使用哪种shell # this is my first s ...
随机推荐
- ASP.NET经典的、封装好的ADO.NET类包
using System; using System.Collections; using System.Collections.Specialized; using System.Runtime.R ...
- java transient关键字作用,使用场景
transient的作用及使用方法,官方解释为: Variables may be marked transient to indicate that they are not part of the ...
- APICloud手机APP开发
官网 http://docs.apicloud.com/
- 前端之Html元素的分类
一.html元素可分为三大类:块元素.内联元素.可变元素 1.块元素: 常见块状元素:div,p,ul,ol,li,dl,dt,dd,form,hr,table,tr,td,h1-h6,filedse ...
- JS中的回收机制
js的设计者为了让没有必要的变量保存在内存中,(我们写的任何变量都是需要内存空间的),什么叫没有必要的变量?也就是说你不在需要这个变量的时候它就会被销毁?那么你肯定会问js怎么知道那些变量是我们不需要 ...
- bin&sbin 命令作用
最近需要了解sbin与bin的功能,需要整理一下.一下全部为Ubuntu14里面默认安装的.在这里收集一下,转载请注明出处! bin bash shell bunzip2 .bz2文件的解压缩程序. ...
- Android adb命令,linux中各种命令
常用的ADB命令 1. 显示系统中全部Android平台: android list targets 2. 显示系统中全部AVD(模拟器): android list avd 3. 创建AVD(模拟器 ...
- 【读书笔记】构建之法(CH4~CH6)
从chapter4至chapter6,围绕着构建过程的合作讨论构建之法,而合作与个人工作的区别却以一个微妙的问题为开端:阅读别人的代码有多难? 两人合作:(驾驶员与领航员) 合作要注意代码风格规范与设 ...
- webpack前端构建工具学习总结(三)之webpack.config.js配置文件
Webpack 在执行的时候,除了在命令行传入参数,还可以通过指定的配置文件来执行.默认情况下,会搜索当前目录的 webpack.config.js 文件,这个文件是一个 node.js 模块,返回一 ...
- 使用javap分析Java的字符串操作
我们看这样一行简单的字符串赋值操作的Java代码. String a = "i042416"; 使用命令行将包含了这行代码的Java类反编译查看其字节码: javap -v con ...