Linux程式设计-11.ShellScript(bash)--(2)教学例
ReadyGo!技术成就梦想
网络搜索
efish
2008-2-24 2:50:13
"Helloworld"ShellScript
照传统程式教学例,这一节介绍ShellScript的"HelloWorld"如何撰写。
--------------------------------------------------------------------------------
#!/bin/sh
#Filename:hello
echo"Helloworld!"
--------------------------------------------------------------------------------
大家应该会注意到第一行的"#!/bin/sh"。在UNIX下,所有的可执行Script,不管是那一种语言,其开头都是"#!",例如Perl是"#!/usr/bin/perl",tcl/tk是"#!/usr/bin/wish",看您要执行的Script程式位置在那里。您也可以用"#!/bin/bash"、"#!/bin/tcsh"等等,来指定使用特定的Shell。
echo是个bash的内建指令。
--------------------------------------------------------------------------------
接下来,执行hello这个script:
要执行一个Script的方式有很多种。
--------------------------------------------------------------------------------
第一种:将hello这个档案的权限设定为可执行。
#chmod755hello
执行
#./hello
helloworld
--------------------------------------------------------------------------------
第二种:使用bash内建指令"source"或"."。
#sourcehello
helloworld
或
#.hello
helloworld
--------------------------------------------------------------------------------
第三种:直接使用sh/bash/tcsh指令来执行。
#shhello
helloworld
或
#bashhello
helloworld
--------------------------------------------------------------------------------
Bash执行选项
--------------------------------------------------------------------------------
-cstring:读取string来当命令。
-i:互动介面。
-s:由stdin读取命令。
-:取消往後选项的读取。
-norc:不要读~/.bashrc来执行。
-noprofile:不要读/etc/profile、~/.bash_profile、~/.bash_login、~/.profile等等来执行。
-rcfilefilename:执行filename,而非~/.bashrc
-version:显示版本。
-quiet:启动时不要哩唆。
-login:确保bash是个loginshell。
-nobraceexpansion:不要用curlybraceexpansion({}符号展开)。
-nolineediting:不用readline来读取命令列。
-posix:改采Posix1003.2标准。
照传统程式教学例,这一节介绍ShellScript的"HelloWorld"如何撰写。
--------------------------------------------------------------------------------
#!/bin/sh
#Filename:hello
echo"Helloworld!"
--------------------------------------------------------------------------------
大家应该会注意到第一行的"#!/bin/sh"。在UNIX下,所有的可执行Script,不管是那一种语言,其开头都是"#!",例如Perl是"#!/usr/bin/perl",tcl/tk是"#!/usr/bin/wish",看您要执行的Script程式位置在那里。您也可以用"#!/bin/bash"、"#!/bin/tcsh"等等,来指定使用特定的Shell。
echo是个bash的内建指令。
--------------------------------------------------------------------------------
接下来,执行hello这个script:
要执行一个Script的方式有很多种。
--------------------------------------------------------------------------------
第一种:将hello这个档案的权限设定为可执行。
#chmod755hello
执行
#./hello
helloworld
--------------------------------------------------------------------------------
第二种:使用bash内建指令"source"或"."。
#sourcehello
helloworld
或
#.hello
helloworld
--------------------------------------------------------------------------------
第三种:直接使用sh/bash/tcsh指令来执行。
#shhello
helloworld
或
#bashhello
helloworld
--------------------------------------------------------------------------------
Bash执行选项
--------------------------------------------------------------------------------
-cstring:读取string来当命令。
-i:互动介面。
-s:由stdin读取命令。
-:取消往後选项的读取。
-norc:不要读~/.bashrc来执行。
-noprofile:不要读/etc/profile、~/.bash_profile、~/.bash_login、~/.profile等等来执行。
-rcfilefilename:执行filename,而非~/.bashrc
-version:显示版本。
-quiet:启动时不要哩唆。
-login:确保bash是个loginshell。
-nobraceexpansion:不要用curlybraceexpansion({}符号展开)。
-nolineediting:不用readline来读取命令列。
-posix:改采Posix1003.2标准。
-
相关文章
Linux程式设计-11.ShellScript(bash)--(3)用於自动备…
Linux程式设计-11.ShellScript(bash)--(3)用於自动备份的ShellScript
Linux程式设计-11.ShellScript(bash)--(4)档案系统检…
Linux程式设计-11.ShellScript(bash)--(4)档案系统检查
Linux程式设计-11.ShellScript(bash)--(5)控制圈for
Linux程式设计-11.ShellScript(bash)--(5)控制圈for
Linux程式设计-11.ShellScript(bash)--(6)流程控制c…
Linux程式设计-11.ShellScript(bash)--(6)流程控制case
Linux程式设计-11.ShellScript(bash)--(7)流程控制s…
Linux程式设计-11.ShellScript(bash)--(7)流程控制select
Linux程式设计-11.ShellScript(bash)--(8)返回状态E…
Linux程式设计-11.ShellScript(bash)--(8)返回状态Exit
Linux程式设计-11.ShellScript(bash)--(9)流程控制if
Linux程式设计-11.ShellScript(bash)--(9)流程控制if
Linux程式设计-11.ShellScript(bash)--(10)控制圈wh…
Linux程式设计-11.ShellScript(bash)--(10)控制圈while/until
Linux程式设计-11.ShellScript(bash)--(11)参数与变数
Linux程式设计-11.ShellScript(bash)--(11)参数与变数
Linux程式设计-11.ShellScript(bash)--(13)Bash内建…
Linux程式设计-11.ShellScript(bash)--(13)Bash内建指令集
2秒记住本站域名
玩过泡泡龙吗?Readygo?Go! 再加上.Com.Cn的后缀,那就是大名小顶的ReadyGo.com.cn
