菜鸟的《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 ...
随机推荐
- Tinghua Data Mining 3
特征选择 男女身高 男女抽烟 先验分布 熵 衡量系统的不确定性 属性的价值 降低了不确定性 降低的幅度越高越好 主成分分析 旋转是的数据间的correlation消失掉 Q是正交阵 七长八短,长宽相关 ...
- C++中this指针的理解
C++中this指针的理解 先要理解class的意思.class应该理解为一种类型,象int,char一样,是用户自定义的类型.用这个类型可以来声明一个变量,比如int x, myclass my等等 ...
- 2016级萌新选拔赛BE题
#include<bits/stdc++.h> using namespace std; #define ll long long ll a[]; ll d[]; int main() { ...
- 1-28Map简介
Map接口概述 除了Collection之外,常用的集合还有Map接口,里面常用的实现类图如下: map中的元素是以键-值的方式存在的,通过键可以获取到值,键是不可以重复的,跟地图比较像,通过一个坐标 ...
- 111 Minimum Depth of Binary Tree 二叉树的最小深度
给定一个二叉树,找出其最小深度.最小深度是从根节点到最近叶节点的最短路径的节点数量.详见:https://leetcode.com/problems/minimum-depth-of-binary-t ...
- JavaScript 事件——“事件类型”中“HTML5事件”的注意要点
contextmenu事件 该事件用以表示何时应该显示上下文菜单,以便开发者取消默认的上下文菜单,转而提供自定义的菜单. 因为该事件属于鼠标事件,所以其事件对象中包含与光标位置有关的所有属性.如: & ...
- [转]Ioc容器Autofac
本文转自:http://www.cnblogs.com/hkncd/archive/2012/11/21/2780041.html Ioc容器Autofac系列(1)-- 初窥 前言 第一次接触A ...
- OC 思维导向图
iOS 扩展思维导向图,如下图所示:
- Android的开机流程及对应源码位置分析
1.系统引导bootloader 1)源码:bootable/bootloader/* 2)说明:加电后,CPU将先执行bootloader程序,此处有三种选择 a)开机按Camera+Power启动 ...
- AS学习系列[1]——初识Android Studio
写在前面的话:由于于方老师的高墙所限,网络成了学习Android第一道“拦路虎”.所以,个人以为,在学习Android之前需要了解下FQ技术(仅仅是为了技术学习). 1.AS AS(Android s ...