Linux 下 解释性的语言 - shell

[b]Shell Scripts are collections of commands that are stored in a file.[/b]


[b]Official site:
[url]http://www.gnu.org/software/bash/[/url]
Bash Reference Manual:
[url]http://www.gnu.org/software/bash/manual/[/url][/b]


[b][url]http://linuxcommand.org/lc3_learning_the_shell.php#contents[/url][/b]


第二讲 Linux编程入门之 脚本编程:
[b][url]http://www.aka.org.cn/Lectures/002/Lecture-2.1.2/index.html[/url]
[/b]
bash stand for Bourne Again SHell.


[b]4 Ways of Executing a Shell Script:[/b]
[url]http://www.thegeekstuff.com/2010/07/execute-shell-script/[/url]
[quote][b]一[/b] 指定绝对或相对路径后,直接运行shell脚本文件,如通过 ./ 来执行当前目录下的shell脚本:

$ ./shellScriptName
既然shell脚本就在terminal中的当前目录下,为什么想要执行它,还必须加 ./(dot-slash) 这个前缀来明确指明脚本的路径那(当前路径)?原因是为了安全,当前路径并不在环境变量 $PATH 中:
[url]http://stackoverflow.com/questions/6331075/why-do-you-need-dot-slash-before-script-name-to-run-it-in-bash[/url]
[b]二[/b] 通过指定 Shell Interpreter 来执行。如:

$ sh shellScriptName
$ bash shellScriptName
[b]三[/b] 使用 source 命令:
$ source shellScriptName
[b]四[/b] 使用 dot command(.命令); in BASH the . is a synonym for source,参见 [url]http://ss64.com/bash/period.html[/url]
$ . shellScriptName
[/quote]


[b]Shell 各种 $:[/b]
[url]http://stackoverflow.com/questions/3206312/unix-shell-programming-special-variables[/url][quote]$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.[/quote][url]http://stackoverflow.com/questions/5163144/what-are-the-special-dollar-sign-shell-variables[/url][quote]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
[/quote][url]http://unixhelp.ed.ac.uk/scrpt/scrpt2.2.2.html[/url][quote]$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"[/quote]


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


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


关于shell中single quote ' 和 double quote " 的区别:
[url]http://zurlinux.com/?tag=weak-quote[/url]
[url]http://pubs.opengroup.org/onlinepubs/009695399/utilities/xcu_chap02.html[/url][quote]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 [b]dollar sign($), backquote(`), and backslash(\)[/b].[/quote]


[b]Command substitution:[/b]
[url]http://tldp.org/LDP/Bash-Beginners-Guide/html/sect_03_04.html#sect_03_04_04[/url][quote]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`
[/quote]使用 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文件所在目录:
[url]http://stackoverflow.com/questions/242538/unix-shell-script-find-out-which-directory-the-script-file-resides[/url]

current_dir=$(pwd)
script_dir=$(dirname $0)

echo $current_dir
echo $script_dir



[b]interactive shell / non-interactive shell、login shell / non-login shell(aka. Sub shell):[/b]
[url]http://docstore.mik.ua/orelly/unix3/upt/ch03_04.htm[/url][quote]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.[/quote][url]http://zsh.sourceforge.net/Guide/zshguide02.html[/url][quote]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.[/quote][url]http://unix.stackexchange.com/questions/38175/difference-between-login-shell-and-non-login-shell[/url]


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


[b]test Command:[/b]
语法:test expr,[ expr ] 或 [[ expr ]] (注意 [后 和 ]前 必须有空格)
查看帮助:

$ man test
---or
$ help test
三种方式的区别:
[url]http://mywiki.wooledge.org/BashFAQ/031[/url]


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


srcs:
shell 编程
[url]http://www.cnblogs.com/fnng/archive/2012/06/09/2542774.html[/url]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值