- 浏览: 1151124 次
- 性别:
- 来自: 北京
文章分类
- 全部博客 (411)
- Java Foundation (41)
- AI/机器学习/数据挖掘/模式识别/自然语言处理/信息检索 (2)
- 云计算/NoSQL/数据分析 (11)
- Linux (13)
- Open Source (12)
- J2EE (52)
- Data Structures (4)
- other (10)
- Dev Error (41)
- Ajax/JS/JSP/HTML5 (47)
- Oracle (68)
- FLEX (19)
- Tools (19)
- 设计模式 (4)
- Database (12)
- SQL Server (9)
- 例子程序 (4)
- mysql (2)
- Web Services (4)
- 面试 (8)
- 嵌入式/移动开发 (18)
- 软件工程/UML (15)
- C/C++ (7)
- 架构Architecture/分布式Distributed (1)
最新评论
-
a535114641:
LZ你好, 用了这个方法后子页面里的JS方法就全不能用了呀
页面局部刷新的两种方式:form+iframe 和 ajax -
di1984HIT:
学习了,真不错,做个记号啊
Machine Learning -
赵师傅临死前:
我一台老机器,myeclipse9 + FB3.5 可以正常使 ...
myeclipse 10 安装 flash builder 4.6 -
Wu_Jiang:
触发时间在将来的某个时间 但是第一次触发的时间超出了失效时间, ...
Based on configured schedule, the given trigger will never fire. -
cylove007:
找了好久,顶你
Editable Select 可编辑select
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脚本:
http://stackoverflow.com/questions/6331075/why-do-you-need-dot-slash-before-script-name-to-run-it-in-bash
二 通过指定 Shell Interpreter 来执行。如:
$ source shellScriptName
四 使用 dot command(.命令); in BASH the . is a synonym for source,参见 http://ss64.com/bash/period.html
$ . shellScriptName
$ ./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$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.
引用
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$- 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
引用
$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"
$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(\).
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 substitution,你可以在shell中将一个命令的执行结果赋给某个变量,如下:$(command)or like this using backticks:
`command`
# or var=`echo $0` var=$(echo $0) echo $var
Tips:
1 算术运算需要加两重圆括号,如:
$ echo $((2+2))
2 shell中为变量赋值时,切记变量名、等号(=)、所赋值 三者之间不能有任何的空格存在,如下面的写法都是错误的:
fn = $1 fn =$1 fn= $13. 获取当前目录和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:
http://zsh.sourceforge.net/Guide/zshguide02.htmlIn 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 $- imHThe 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.
引用
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
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.
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
发表评论
-
python
2013-03-19 12:15 1279Python documentation, include T ... -
Linux File System | linux文件系统
2013-02-01 17:54 1649Unix Filesystem Hierarchy S ... -
Ubuntu 使用杂摘
2012-10-19 23:51 2141Useful Shortcut Keys In Ubun ... -
Gerrit : Code Review Tool based on Git
2012-09-21 18:52 13029Gerrit Code Review for Git: htt ... -
Git : Distributed version control system
2012-09-20 09:59 6168Srcs : Git Reference http://gi ... -
未完 Regular Expressions | 正则表达式
2011-08-25 11:43 1532Extended Regular Expression ... -
Vim & Emacs
2009-11-23 00:30 1746精读: Learn Vim Progressively ... -
用信号量机制来实现多个进程对临界资源的互斥访问 & PV操作
2009-11-21 13:55 11347进程互斥 定义:两个或两个以上的进程,不能同时进入关于同一组 ... -
Linux启动过程剖析
2009-11-12 22:36 1429http://student.csdn.net/space.p ... -
Linux 与 Java
2009-07-30 09:21 1304Ubuntu 12.04 下安装配置 JDK: htt ... -
Linux Commands 命令
2009-07-08 22:12 2510Linux命令大全: http://ss64.com/bas ... -
crontab & Quartz
2009-06-11 17:36 3244Add Jobs To cron Under ...
相关推荐
根据提供的文件信息,本书《LINUX-UNIX-Shells》是一本详细介绍各种Unix/Linux下的Shell环境的书籍,包括了Bourne Shell、Korn Shell、C Shell、Bash以及Tcsh等主流Shell类型。该书由Helmut Herold编写,并且已经是...
- Shell脚本是Linux操作系统中的一种脚本语言,用于自动化命令执行和任务处理。它基于Bourne shell或其他shell变体,如bash。 - `#!/bin/bash`是脚本的首行,称为shebang,指定该脚本使用bash解释器来执行。 - `...
在Linux系统中,Bash(Bourne-Again SHell)是默认的命令解释器,是用户与操作系统交互的重要工具。Bash shell脚本是一种强大的编程语言,它允许用户编写自动化任务,执行日常管理任务,或者创建复杂的系统服务。...
Linux Shell简易注册登录系统是一种基于命令行界面的用户身份验证工具,主要由Shell脚本语言编写,用于教学或实验环境。这个系统可以帮助学生理解和实践基本的Linux用户管理和认证原理,而不涉及复杂的系统级别的...
Shell脚本,作为Linux环境下的命令解释器,允许用户编写一系列命令来实现自动化操作,极大地提高了工作效率。在“Linux云计算-Shell脚本100例”这个主题中,我们将会深入探讨这两个领域的结合,学习如何通过编写...
Linux Shell 是操作系统与用户交互的重要工具,它是一种命令行解释器,同时也是一种强大的程序设计语言。本文将详细介绍Linux Shell的基础知识,包括其概念、流行种类及基本编程技巧。 **一、什么是Shell** Shell...
Linux/Unix Shell编程是系统管理员和开发者日常工作中不可或缺的一部分,它是一种强大的命令行脚本语言,用于自动化系统任务,管理文件,以及与操作系统进行交互。本文将深入探讨shell编程的基础和高级概念,帮助你...
在Linux环境下模拟实现命令解释器是一项挑战性的操作系统大作业,旨在深入理解操作系统的运作机制以及命令行接口(CLI)的工作原理。在这个项目中,学生需要编写一个程序,该程序能够接收用户输入的命令,解析这些...
Linux中的Shell是一个至关重要的组成部分,它是用户与操作系统交互的接口,扮演着命令解释器的角色。在Linux系统中,默认的Shell通常是Bash(Bourne-Again SHell),它继承了Bourne shell的功能并增加了许多扩展特性...
Shell是Linux操作系统中的一个命令行解释器,它的作用是接收用户输入的命令,解释这些命令,并将它们传递给操作系统内核执行。Shell不仅解释单个命令,还能解析和执行存储在文本文件中的多条命令,这就是所谓的Shell...
Linux Shell编程是Linux系统管理的重要组成部分,它是一种解释器,用于执行命令、脚本和管理系统。在本节中,我们将深入探讨Shell的基础知识,包括它的作用、如何编写和执行脚本,以及各种实用的命令和操作。 首先...
### Linux脚本编写语法基础详解 #### 一、脚本执行与权限设置 ...通过这些基础概念和命令的掌握,你将能够构建出高效、功能丰富的Linux shell脚本,满足自动化运维、数据处理和系统管理等各种需求。
在Shell部分,我们需要注意,Shell不仅仅是一个命令行解释器,它是一个完整的编程语言。用户可以通过编写Shell脚本来自动化任务,实现文件管理、系统监控等多种功能。在Unix和Linux中,有许多种类的Shell,如Bash...
在Linux环境中,shell脚本是一种基于文本的编程语言,它允许用户编写一系列命令,以实现批处理操作。下面我们将深入探讨Linux Shell脚本的基本概念、语法和常用命令。 1. Linux脚本编写基础 1.1 语法基本介绍 - *...
因此,所有给出的选项 A:特殊程序、B:内核和用户的接口、C:命令解释器 和 D:编程语言 都是对 SHELL 正确的描述。 #### CentOS 默认 SHELL - **知识点**:在 CentOS 系统中,默认的 SHELL 是 Bash(Bourne Again...
### Linux 下 Shell 编程详解 #### 一、Shell 的概念与作用 Shell 是一种特殊的程序,它作为用户与 Linux/UNIX 操作系统之间的重要接口。它的存在为用户提供了便捷的操作方式,同时也起到了保护内核免受用户误操作...
"Useful-Linux-Shell-Scripts-开源"这个项目显然集合了一些实用的Linux Shell脚本,旨在帮助用户更高效地管理和优化他们的Linux系统。Shell脚本是Linux环境中的程序编写方式,通过这些脚本,我们可以自动化执行一...
- 了解 shell 的基本概念及其作为 Linux/Unix 系统的用户界面的作用。 - 掌握 `/etc/profile`、`$HOME/.bash_profile`、`$HOME/.bashrc` 和 `$HOME/.bash_logout` 这四个配置文件的基本功能与区别。 - `/etc/...