菜鸟的《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 ...
随机推荐
- 用apache commons-pool2建立thrift连接池
Apache Thrift 是 Facebook 实现的一种高效的.支持多种编程语言的远程服务调用的框架.具体的介绍可以看Apache的官方网站:http://thrift.apache.org/ . ...
- YII报错笔记:<pre>PHP Notice 'yii\base\ErrorException' with message 'Uninitialized string offset: 0' in /my/test/project/iot/vendor/yiisoft/yii2/base/Model.php:778
YII常见报错笔记 报错返回的代码如下: <pre>PHP Notice 'yii\base\ErrorException' with message 'Uninitialized str ...
- HTML表格的基本结构标记
<table>标记 1.基本格式:<table 属性1="属性值1" 属性2="属性值2" ... ...>表格内容</table ...
- Haproxy常见用法
简介 HAProxy 提供高可用性.负载均衡以及基于 TCP 和 HTTP 应用的代理,支持虚拟主机, 它是免费.快速并且可靠的一种解决方案. HAProxy 特别适用于那些负载特大的 web 站点, ...
- android开发学习 ------- 【转】 android中的单例模式 (详解)
https://blog.csdn.net/u011418943/article/details/60139644 这篇文章 前因后果 都说出来了 ,值得学习. https://blog.cs ...
- {ubuntu}不能挂载windows
sudo apt-get install ntfs-3g sudo ntfsfix /dev/sda?
- HDU 4291 A Short problem 短问题 (递推,找规律)
题意: 给出递推式 g(n) = 3g(n - 1) + g(n - 2),且g(1) = 1,g(0) = 0.求g( g( g(n))) mod 109 + 7. 思路: 要求的g( g( g(n ...
- SQLite - WHERE子句
SQLite - WHERE子句 SQLite WHERE子句用于指定一个条件同时抓取数据从一个表或多个表. 如果给定的条件满意,意味着true,然后从表中返回特定值.你会使用WHERE子句来筛选记录 ...
- Codeforces Round #316 (Div. 2) D Tree Requests
官方题解是离线询问,dfs树形转线性,然后二分找区间. 还有一种比较好的做法是直接dfs,将当前访问这个结点u相关的询问之前的状态存起来,然后访问完以后利用异或开关性,得到这颗子树上的答案. 代码是学 ...
- 简单shell执行脚本
#!/bin/bash source /etc/profile APPLICATIONS_HOME="/opt/cpic_analy" APPLICATION_NAME=" ...