`

Linux 下 解释性的语言 - shell

阅读更多
  
Shell Scripts are collections of commands that are stored in a file.


Official site:
http://www.gnu.org/software/bash/
Bash Reference Manual:
http://www.gnu.org/software/bash/manual/





http://linuxcommand.org/lc3_learning_the_shell.php#contents


第二讲 Linux编程入门之 脚本编程:
http://www.aka.org.cn/Lectures/002/Lecture-2.1.2/index.html

bash stand for Bourne Again SHell.


4 Ways of Executing a Shell Script:
http://www.thegeekstuff.com/2010/07/execute-shell-script/
引用
指定绝对或相对路径后,直接运行shell脚本文件,如通过 ./ 来执行当前目录下的shell脚本:
$ ./shellScriptName
既然shell脚本就在terminal中的当前目录下,为什么想要执行它,还必须加 ./(dot-slash) 这个前缀来明确指明脚本的路径那(当前路径)?原因是为了安全,当前路径并不在环境变量 $PATH 中:
http://stackoverflow.com/questions/6331075/why-do-you-need-dot-slash-before-script-name-to-run-it-in-bash
通过指定 Shell Interpreter 来执行。如:
$ sh shellScriptName
$ bash shellScriptName
使用 source 命令:
$ source shellScriptName
使用 dot command(.命令); in BASH the . is a synonym for source,参见 http://ss64.com/bash/period.html
$ . shellScriptName



Shell 各种 $:
http://stackoverflow.com/questions/3206312/unix-shell-programming-special-variables
引用
$1 - $9 these variables are the positional parameters.
$0 the name of the command currently being executed.
$# the number of positional arguments given to this invocation of the shell.
$? the exit status of the last command executed is given as a decimal string. When a command completes successfully, it returns the exit status of 0 (zero), otherwise it returns a non-zero exit status.
$$ the process number of this shell - useful for including in filenames, to make them unique.
$! the process id of the last command run in the background.
$- the current options supplied to this invocation of the shell.
$* a string containing all the arguments to the shell, starting at $1.
$@ same as above, except when quoted.
http://stackoverflow.com/questions/5163144/what-are-the-special-dollar-sign-shell-variables
引用
Positional parameters $1,$2,$3… and their corresponding array representation, count and IFS expansion $@, $#, and $*.
$- current options set for the shell.
$$ pid of the current shell (not subshell)
$_ most recent parameter (or the abs path of the command to start the current shell immediately after startup)
$IFS the (input) field separator
$? most recent foreground pipeline exit status
$! PID of the most recent background command
$0 name of the shell or shell script
http://unixhelp.ed.ac.uk/scrpt/scrpt2.2.2.html
引用
$1 - $9       these variables are the positional parameters.
$0            the name of the command currently being executed.
$#            the number of positional arguments given to this
              invocation of the shell.
$?            the exit status of the last command executed is
              given as a decimal string.  When a command
              completes successfully, it returns the exit status
              of 0 (zero), otherwise it returns a non-zero exit
              status.
$$            the process number of this shell - useful for
              including in filenames, to make them unique.
$!            the process id of the last command run in
              the background.
$-            the current options supplied to this invocation
              of the shell.
$*            a string containing all the arguments to the
              shell, starting at $1.
$@            same as above, except when quoted.

$* and $@ when unquoted are identical and expand into the arguments.
"$*" is a single word, comprising all the arguments to the shell, joined together with spaces. For example '1 2' 3 becomes "1 2 3".
"$@" is identical to the arguments received by the shell, the resulting list of words completely match what was given to the shell. For example '1 2' 3 becomes "1 2" "3"



Shell built-in Commands:
http://www.gsp.com/cgi-bin/man.cgi?topic=builtin



关于shell中的特殊符号:
http://docstore.mik.ua/orelly/unix3/korn/ch01_09.htm
Meaning of bash parameter used in Unix script:
http://javarevisited.blogspot.com/2011/06/special-bash-parameters-in-script-linux.html



关于shell中single quote ' 和 double quote " 的区别:
http://zurlinux.com/?tag=weak-quote
http://pubs.opengroup.org/onlinepubs/009695399/utilities/xcu_chap02.html
引用
Enclosing characters in single-quotes ( '' ) shall preserve the literal value of each character within the single-quotes. A single-quote cannot occur within single-quotes.
Enclosing characters in double-quotes ( "" ) shall preserve the literal value of all characters within the double-quotes, with the exception of the characters dollar sign($), backquote(`), and backslash(\).




Command substitution:
http://tldp.org/LDP/Bash-Beginners-Guide/html/sect_03_04.html#sect_03_04_04
引用
Command substitution allows the output of a command to replace the command itself. Command substitution occurs when a command is enclosed like this:
$(command)
or like this using backticks:
`command`
使用 Command substitution,你可以在shell中将一个命令的执行结果赋给某个变量,如下:
# or var=`echo $0`
var=$(echo $0)
echo $var



Tips:
1 算术运算需要加两重圆括号,如:
$ echo $((2+2))

2 shell中为变量赋值时,切记变量名、等号(=)、所赋值 三者之间不能有任何的空格存在,如下面的写法都是错误的:
fn = $1
fn =$1
fn= $1
3. 获取当前目录和shell文件所在目录:
http://stackoverflow.com/questions/242538/unix-shell-script-find-out-which-directory-the-script-file-resides
current_dir=$(pwd)
script_dir=$(dirname $0)

echo $current_dir
echo $script_dir



interactive shell / non-interactive shell、login shell / non-login shell(aka. Sub shell):
http://docstore.mik.ua/orelly/unix3/upt/ch03_04.htm
引用
Each Unix shell (sh, csh, etc.) can be in interactive mode or noninteractive mode. A shell also can act as a login shell or a nonlogin shell. A shell is a shell is a shell -- e.g., a login bash shell is the same program (like /bin/bash) as a nonlogin bash shell. The difference is in the way that the shell acts: which setup files it reads, whether it sets a shell prompt, and so on.
In general, shells are used for two jobs. Sometimes, a shell handles commands that you type at a prompt. These are interactive shells. Other times, a shell reads commands from a file -- a shell script (Section 35.2). In this case, the shell doesn't need to print a prompt, to handle command-line editing, and so on. These shells can be noninteractive shells. (There's no rule that only noninteractive shells can read shell scripts or that only interactive shells can read commands from a terminal. But this is generally true.)
One other difference between interactive and noninteractive shells is that interactive shells tie STDOUT and STDERR to the current terminal, unless otherwise specified.
It's usually easy to see whether a particular invocation of your shell is interactive. In C shells, the prompt variable will be set. In the Korn shell and bash, the -i flag is set. Your current flags may be displayed using the $- variable:
prompt$ echo $-
imH
The previous example, from an interactive bash shell, shows that the flags for an interactive shell (i), monitor mode (m), and history substitution (H) have been set.
http://zsh.sourceforge.net/Guide/zshguide02.html
引用
First, you need to know what is meant by an interactive and a login shell. Basically, the shell is just there to take a list of commands and run them; it doesn't really care whether the commands are in a file, or typed in at the terminal. In the second case, when you are typing at a prompt and waiting for each command to run, the shell is interactive; in the other case, when the shell is reading commands from a file, it is, consequently, non-interactive. A list of commands used in this second way --- typically by typing something like zsh filename, although there are shortcuts --- is called a script, as if the shell was acting in a play when it read from it (and shells can be real hams when it comes to playacting). When you start up a script from the keyboard, there are actually two zsh's around: the interactive one you're typing at, which is waiting for another, non-interactive one to finish running the script. Almost nothing that happens in the second one affects the first; they are different copies of zsh.
Remember that when I give examples for you to type, I often show them as they would appear in a script, without prompts in front. What you actually see on the screen if you type them in will have a lot more in front.
When you first log into the computer, the shell you are presented with is interactive, but it is also a login shell. If you type `zsh', it starts up a new interactive shell: because you didn't give it the name of a file with commands in, it assumes you are going to type them interactively. Now you've got two interactive shells at once, one waiting for the other: it doesn't sound all that useful, but there are times when you are going to make some radical changes to the shell's settings temporarily, and the easiest thing to do is to start another shell, do what you want to do, and exit back to the original, unaltered, shell --- so it's not as stupid as it sounds.
However, that second shell will not be a login shell. How does zsh know the difference? Well, the programme that logs you in after you type your password (called, predictably, login), actually sticks a `-' in front of the name of the shell, which zsh recognises. The other way of making a shell a login shell is to run it yourself with the option -l; typing `zsh -l' will start a zsh that also thinks it's a login shell, and later I'll explain how to turn on options within the shell, which you can do with the login option too. Otherwise, any zsh you start yourself will not be a login shell. If you are using X-Windows, and have a terminal emulator such as xterm running a shell, that is probably not a login shell. However, it's actually possible to get xterm to start a login shell by giving it the option -ls, so if you type `xterm -ls &', you will get a window running a login shell (the & means the shell in the first window doesn't wait for it to finish).
The first main difference between a login shell and any other interactive shell is the one to do with startup files, described below. The other one is what you do when you're finished. With a login shell you can type `logout' to exit the shell; with another you type `exit'. However, `exit' works for all shells, interactive, non-interactive, login, whatever, so a lot of people just use that. In fact, the only difference is that `logout' will tell you `not login shell' if you use it anywhere else and fail to exit. The command `bye' is identical to `exit', only shorter and less standard. So my advice is just to use `exit'.
As somebody pointed out to me recently, login shells don't have to be interactive. You can always start a shell in the two ways that make it a login shell; the ways that make it an interactive shell or not are independent. In fact, some start-up scripts for windowing systems run a non-interactive login shell to incorporate definitions from the appropriate login scripts before executing the commands to start the windowing session.
http://unix.stackexchange.com/questions/38175/difference-between-login-shell-and-non-login-shell



shell 中 parentheses (, brackets [, curly braces { 的区别:
http://stackoverflow.com/questions/2188199/bash-double-or-single-bracket-parentheses-curly-braces



test Command:
语法:test expr,[ expr ] 或 [[ expr ]]   (注意 [后 和 ]前 必须有空格)
查看帮助:
$ man test
---or
$ help test
三种方式的区别:
http://mywiki.wooledge.org/BashFAQ/031


How to Read a File Line by Line in a Shell Script:
http://www.bashguru.com/2010/05/how-to-read-file-line-by-line-in-shell.html



srcs:
shell 编程
http://www.cnblogs.com/fnng/archive/2012/06/09/2542774.html
分享到:
评论

相关推荐

    Linux下Shell编程

    Shell是一种具备特殊功能的程序, 它是介于使用者和 UNIX/Linux 操作系统之核心程序(kernel)间的一个接口。 为了对用户屏蔽内核的复杂性,也为了保护内核以免用户误操作造成损害,在内核的周围建了一个外壳(shell)...

    linux下shell编程

    Shell是一种具备特殊功能的程序, 它是介于使用者和 UNIX/Linux 操作系统之核心程序(kernel)间的一个接口。 为了对用户屏蔽内核的复杂性,也为了保护内核以免用户误操作造成损害,在内核的周围建了一个外壳(shell)...

    Power Shell

    借助.NET架构及支持Perl,Python等解释性语言,Power Shell号称从CLI上试图追赶Linux(Unix)的终端(如BASH等)。尽管我认为这不可能,但是终于微软也有了一款能拿的出手的Shell。本版本仅能在XP下使用,没有2k版,...

    Linux系统故障诊断与排除--James Kirkland

    3.1.2 解释输出 55 3.1.3 保存自定义 56 3.1.4 批处理模式 57 3.2 sar 58 3.2.1 sar数据收集器 58 3.2.2 CPU统计数据 59 3.2.3 磁盘I/O统计数据 62 3.2.4 网络统计数据 63 3.3 vmstat 65 3.4 ...

    Linux中使用自由软件Rexx来编写脚本

    但是在 Linux 中仍然有一个重要的领域,在这里,相对于“过轻量级”的 bash 或 ksh shell,或者“过重量级”的解释性编程语言例如 Python、Perl、Ruby、TCL,或者可能还有 Scheme,Rexx 是更好的脚本解决方案。...

    入门学习Linux常用必会60个命令实例详解doc/txt

    前面介绍了Linux下有多种Shell,一般缺省的是Bash,如果想更换Shell类型可以使用chsh命令。先输入账户密码,然后输入新Shell类型,如果操作正确系统会显示“Shell change”。其界面一般如下: Changing fihanging ...

    docopts:命令行界面描述语言 docopt 的 Shell 解释器

    for shell - 轻松制作漂亮的 CLI。 状态:工作。 docopts :bash 的命令行包装器。 大多数概念都记录在docopt (不带 S)手册中 - 请参阅 。 许多示例在 bash 4.x 中使用关联数组,但在 macOS (OS X) 或旧版 ...

    RED HAT LINUX 6大全

    本书全面系统地介绍了Red Hat Linux 6。全书共分为五个部分,包括35章和四个附录。第一部分为Red Hat Linux的介绍和安装;第二部分为服务配置;第三部分为系统管理;第四部分为Linux编程;第五部分为附录。本书内容...

    bash-2.04-2.05.diff.gz

    Bash 是一种广泛使用的 Unix shell,它是一个命令语言解释器,允许用户通过命令行界面与操作系统交互。Bash 同时是许多类 Unix 系统(如 Linux 和 macOS)的默认 shell,并且支持多种脚本编程功能。 Bash 作为一个...

    bash-1.14.0-1.14.1.diff.gz

    Bash 是一种广泛使用的 Unix shell,它是一个命令语言解释器,允许用户通过命令行界面与操作系统交互。Bash 同时是许多类 Unix 系统(如 Linux 和 macOS)的默认 shell,并且支持多种脚本编程功能。 Bash 作为一个...

    bash-2.02.1-2.03.diff.gz

    Bash 是一种广泛使用的 Unix shell,它是一个命令语言解释器,允许用户通过命令行界面与操作系统交互。Bash 同时是许多类 Unix 系统(如 Linux 和 macOS)的默认 shell,并且支持多种脚本编程功能。 Bash 作为一个...

    bash-2.02-2.02.1.diff.gz

    Bash 是一种广泛使用的 Unix shell,它是一个命令语言解释器,允许用户通过命令行界面与操作系统交互。Bash 同时是许多类 Unix 系统(如 Linux 和 macOS)的默认 shell,并且支持多种脚本编程功能。 Bash 作为一个...

    bash-1.14.3-1.14.4.diff.gz

    Bash 是一种广泛使用的 Unix shell,它是一个命令语言解释器,允许用户通过命令行界面与操作系统交互。Bash 同时是许多类 Unix 系统(如 Linux 和 macOS)的默认 shell,并且支持多种脚本编程功能。 Bash 作为一个...

    bash-2.0-2.01.diff.gz

    Bash 是一种广泛使用的 Unix shell,它是一个命令语言解释器,允许用户通过命令行界面与操作系统交互。Bash 同时是许多类 Unix 系统(如 Linux 和 macOS)的默认 shell,并且支持多种脚本编程功能。 Bash 作为一个...

    bash-1.14.6-1.14.7.diff.gz

    Bash 是一种广泛使用的 Unix shell,它是一个命令语言解释器,允许用户通过命令行界面与操作系统交互。Bash 同时是许多类 Unix 系统(如 Linux 和 macOS)的默认 shell,并且支持多种脚本编程功能。 Bash 作为一个...

    bash-1.14.5-1.14.6.diff.gz

    Bash 是一种广泛使用的 Unix shell,它是一个命令语言解释器,允许用户通过命令行界面与操作系统交互。Bash 同时是许多类 Unix 系统(如 Linux 和 macOS)的默认 shell,并且支持多种脚本编程功能。 Bash 作为一个...

    bash-5.2-beta.tar.gz

    Bash 是一种广泛使用的 Unix shell,它是一个命令语言解释器,允许用户通过命令行界面与操作系统交互。Bash 同时是许多类 Unix 系统(如 Linux 和 macOS)的默认 shell,并且支持多种脚本编程功能。 Bash 作为一个...

    bash-5.2-alpha.tar.gz

    Bash 是一种广泛使用的 Unix shell,它是一个命令语言解释器,允许用户通过命令行界面与操作系统交互。Bash 同时是许多类 Unix 系统(如 Linux 和 macOS)的默认 shell,并且支持多种脚本编程功能。 Bash 作为一个...

    bash-5.2-rc2.tar.gz

    Bash 是一种广泛使用的 Unix shell,它是一个命令语言解释器,允许用户通过命令行界面与操作系统交互。Bash 同时是许多类 Unix 系统(如 Linux 和 macOS)的默认 shell,并且支持多种脚本编程功能。 Bash 作为一个...

    bash-5.1-rc2.tar.gz

    Bash 是一种广泛使用的 Unix shell,它是一个命令语言解释器,允许用户通过命令行界面与操作系统交互。Bash 同时是许多类 Unix 系统(如 Linux 和 macOS)的默认 shell,并且支持多种脚本编程功能。 Bash 作为一个...

Global site tag (gtag.js) - Google Analytics