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 各种 $:
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