用 bash shell 编程
简介:
当命令不在命令行执行时,而是从一个文件中执行时,该文件就称为shell脚本,脚本以非交互方式运行。
当bash shell以非交互方式运行时,它现查找环境变量BASH_ENV(ENV),该变量指定了一个环境文件(通常是.bashrc),然后从该文件开始执行。当BASH_ENV文件执行后,shell就开始执行脚本中的命令,shell就开始执行脚本中的命令。bash以非交互方式运行时, 如果带选项-norc或--norc,则不执行BASH_ENV文件。
创建shell脚本的步骤
shell脚本通常是编辑器中编写,由命令和散布其间的注释组成,注释是跟在#号后面的内容,用来对脚本作注解。
第一行
位于脚本左上角第一行会指出要用哪个程序来执行脚本中的行。这一行通常称为sh#行,写作:#!/bin/bash
#!被称为幻数,内核根据它来确定该用哪个程序来翻译脚本中的行。这一行必须是脚本顶端第一行。
注释
注释是跟在#号后的行。
可执行语句与bash shell结构
一个bash shell程序由一组unix命令,bash shell命令,编程结构和注释组成。
使脚本可执行
当你创建文件时,文件并没有被自动授予执行权限,如果要运行脚本就必须给它执行权限。可以用chmod命令来打开脚本的执行权限。
如:chmod +x myscript
下面是一个脚本会话的例子:
#!/bash/bash
#This is the first bash shell program of the day
#scriptname: greetings
echo "hello,$LOGNAME, it's nice talking to you."
echo "your present working directory is $(pwd)."
echo "you are working on a machine called $(uname -n)."
echo "here is a list of your files."
ls
echo "bye for now $LOGNAME, The time is $(datea +%T)!"
$chmode u+x greetings
$./greetings
hello,woods2001, it's nice talking to you.
your present working directory is /home/woods2001/Shell/chapter12.
you are working on a machine called woods2001-desktop.
here is a list of your files.
greetings note.txt note.txt~
bye for now woods2001, The time is 21:02:24!
shell脚本语言简介
转载本文章为转载内容,我们尊重原作者对文章享有的著作权。如有内容错误或侵权问题,欢迎原作者联系我们进行内容更正或删除文章。
上一篇:read命令简介
下一篇:做Manager的18项基本技能
提问和评论都可以,用心的回复会被更多人看到
评论
发布评论
相关文章
-
Shell脚本语言基础
Shell脚本语言基础1 基本介绍Shell 是一个用 C 语言编写的程序,它是用户使用 Linux 的桥梁。Shell 既是一种命令语言,又是一种程序设计语言。2 Hello World2.1 文件
linux 运维 shell desktop bash -
.Net脚本语言Boo简介
对软件工程来说,脚本语言相当于输送管,他们强大的富有表现力的语法是他们能
脚本语言 动态语言 python