当前位置:首页 » 操作系统 » LinuxShell

LinuxShell

发布时间: 2022-02-05 23:40:45

‘壹’ linux中的shell是什么意思

ll程序呢? 简单的说shell程序就是一个包含若干行
shell或者linux命令的文件.
象编写高级语言的程序一样,编写一个shell程序需要一个文本编辑器.如VI等.
在文本编辑环境下,依据shell的语法规则,输入一些shell/linux命令行,形成一个完整
的程序文件.
执行shell程序文件有三种方法
(1)#chmod +x file(在/etc/profile中,加入export PATH=$:~/yourpath,就可以在命令行下直接运行,像执行普通命令一样)
(2)#sh file
(3)# . file
(4)#source file
在编写shell时,第一行一定要指明系统需要那种shell解释你的shell程序,如:#! /bin/bash,
#! /bin/csh,/bin/tcsh,还是#! /bin/pdksh .
2.shell中的变量
(1)常用系统变量
$ # :保存程序命令行参数的数目
$ ? :保存前一个命令的返回码
$ 0 :保存程序名
$ * :以("$1 $2...")的形式保存所有输入的命令行参数
$ @ :以("$1""$2"...)的形式保存所有输入的命令行参数
(2)定义变量
shell语言是非类型的解释型语言,不象用C++/JAVA语言编程时需要事先声明变量.给一
个变量赋值,实际上就是定义了变量.
在linux支持的所有shell中,都可以用赋值符号(=)为变量赋值.
如:
abc=9 (bash/pdksh不能在等号两侧留下空格 )
set abc = 9 (tcsh/csh)
由于shell程序的变量是无类型的,所以用户可以使用同一个变量时而存放字符时而存放
整数.
如:
name=abc (bash/pdksh)
set name = abc (tcsh)
在变量赋值之后,只需在变量前面加一个$去引用.
如:
echo $abc
(3)位置变量
当运行一个支持多个命令行参数的shell程序时,这些变量的值将分别存放在位置变量里.
其中第一个参数存放在位置变量1,第二个参数存放在位置变量2,依次类推...,shell保留
这些变量,不允许用户以令外的方式定义他们.同别的变量,用$符号引用他们.

3.shell中引号的使用方法
shell使用引号(单引号/双引号)和反斜线("\")用于向shell解释器屏蔽一些特殊字符.
反引号(")对shell则有特殊意义.
如:
abc="how are you" (bash/pdksh)
set abc = "how are you" (tcsh)
这个命令行把三个单词组成的字符串how are you作为一个整体赋值给变量abc.
abc1='@LOGNAME,how are you!' (bash/pdksh)
set abc1='$LOGNAME,how are you!' (tcsh)
abc2="$LOGNAME,how are you!" (bash/pdksh)
set abc2="$LOGNAME,how are you!" (tcsh)
LOGNAME变量是保存当前用户名的shell变量,假设他的当前值是:wang.执行完两条命令后,
abc1的内容是:$LOGNAME, how are you!.而abc2的内容是;wang, how are you!.
象单引号一样,反斜线也能屏蔽所有特殊字符.但是他一次只能屏蔽一个字符.而不能屏蔽
一组字符.
反引号的功能不同于以上的三种符号.他不具有屏蔽特殊字符的功能.但是可以通过他将
一个命令的运行结果传递给另外一个命令.
如:
contents=`ls` (bash/pdksh)
set contents = `ls` (tcsh)
4.shell程序中的test命令
在bash/pdksh中,命令test用于计算一个条件表达式的值.他们经常在条件语句和循环
语句中被用来判断某些条件是否满足.
test命令的语法格式:
test expression
或者
[expression]

在test命令中,可以使用很多shell的内部操作符.这些操作符介绍如下:
(1)字符串操作符 用于计算字符串表达式
test命令 | 含义
-----------------------------------------
Str1 = str2 | 当str1与str2相同时,返回True
Str1! = str2| 当str1与str2不同时,返回True
Str | 当str不是空字符时,返回True
-n str | 当str的长度大于0时,返回True
-z str | 当str的长度是0时,返回True
-----------------------------------------
(2)整数操作符具有和字符操作符类似的功能.只是他们的操作是针对整数
test表达式 | 含义
---------------------------------------------
Int1 -eq int2|当int1等于int2时,返回True
Int1 -ge int2|当int1大于/等于int2时,返回True
Int1 -le int2|当int1小于/等于int2时,返回True
Int1 -gt int2|当int1大于int2时,返回True
Int1 -ne int2|当int1不等于int2时,返回True
-----------------------------------------
(3)用于文件操作的操作符,他们能检查:文件是否存在,文件类型等
test表达式 | 含义
------------------------------------------------
-d file |当file是一个目录时,返回 True
-f file |当file是一个普通文件时,返回 True
-r file |当file是一个刻读文件时,返回 True
-s file |当file文件长度大于0时,返回 True
-w file |当file是一个可写文件时,返回 True
-x file |当file是一个可执行文件时,返回 True
------------------------------------------------
(4)shell的逻辑操作符用于修饰/连接包含整数,字符串,文件操作符的表达式
test表达式 | 含义
----------------------------------------------------------
! expr |当expr的值是False时,返回True
Expr1 -a expr2|当expr1,expr2值同为True时,返回True
Expr1 -o expr2|当expr1,expr2的值至少有一个为True时,返回True
-----------------------------------------------------------
注意:
tcsh shell 不使用test命令,但是tcsh中的表达式同样能承担相同的功能.tcsh
支持的表达式于C中的表达式相同.通常使用在if和while命令中.
tcsh表达式 | 含义
-------------------------------------------------------
Int1 <= int2 |当int1小于/等于int2时,返回True
Int1 >= int2 |当int1大于/等于int2时,返回True
Int1 < int2 |当int1小于int2时,返回True
Int1 > int2 |当int1大于int2时,返回True
Str1 == str2 |当str1与str2相同时,返回True
Str1 != str2 |当str1与str2不同时,返回True
-r file |当file是一个可读文件时,返回True
-w file |当file是一个可写文件时,返回True
-x file |当file是一个可执行文件时,返回True
-e file |当file存在时,返回True
-o file |当file文件的所有者是当前用户时,返回True
-z file |当file长度为0时,返回True
-f file |当file是一个普通文件时,返回True
-d file |当file是一个目录时,返回True
Exp1 || exp2 |当exp1和exp2的值至少一个为True时,返回True
Exp1 && exp2 |当exp1和exp2的值同为True时,返回True
! exp |当exp的值为False时,返回True

‘贰’ linux下的shell是什么

什么是shell
shell是用户和Linux
操作系统
之间的
接口
。Linux中有多种shell,其中缺省使用的是Bash。本章讲述了shell的工作原理,shell的种类,shell的一般操作及Bash的
特性

什么是shell
Linux系统的shell作为操作系统的
外壳
,为用户提供使用操作系统的接口。它是
命令语言
、命令
解释程序

程序设计语言
的统称。
shell是用户和
Linux内核
之间的接口
程序
,如果把Linux内核想象成一个
球体
的中心,shell就是围绕
内核
的外层。当从shell或其他程序向Linux传递命令时,内核会做出相应的反应。
shell是一个命令语言
解释器
,它拥有自己内建的shell命令集,shell也能被系统中其他
应用程序
所调用。用户在提示符下输入的命令都由shell先解释然后传给Linux核心。
Shell是一种具备
特殊功能
的程序,
它是介于使用者和
UNIX/linux
操作系统之核心
程序(kernel)间的一个接口。为什么我们说
shell
是一种介于系统核心程序与使用者
间的中介者呢?读过操作系统概论的读者们都知道操作系统是一个
系统资源
的管理者与分
配者,当您有需求时,您得向系统提出;从操作系统的角度来看,它也必须防止使用者因
为错误的操作而造成系统的伤害?众所周知,对
计算机
下命令得透过命令(command)

是程序(program);程序有编译器(compiler)将程序转为
二进制代码
,可是命令呢?
其实shell
也是一支程序,它由
输入设备
读取命令,再将其转为计算机可以了解的机械码,
然后执行它。
各种操作系统都有它自己的
shell,以
DOS
为例,它的
shell
就是
command.com文
件。如同
DOS
下有
NDOS,4DOS,DRDOS
等不同的命令解译程序可以取代标准的
command.com
,UNIX
下除了
Bourne
shell(/bin/sh)
外还有
C
shell(/bin/csh)、
Korn
shell(/bin/ksh)、Bourne
again
shell(/bin/bash)、Tenex
C
shell(tcsh)
等其它的
shell。UNIX/linux将
shell
独立于核心程序之外,
使得它就如同一般的应用
程序,
可以在不影响操作系统本身的情况下进行修改、更新版本或是添加新的功能。
有一些命令,比如改变工作目录命令cd,是包含在shell
内部
的。还有一些命令,例如
拷贝
命令cp和移动命令rm,是存在于
文件系统
中某个目录下的单独的程序。对用户而言,不必关心一个命令是建立在shell内部还是一个单独的程序。
shell首先检查命令是否是
内部命令
,若不是再检查是否是一个应用程序(这里的应用程序可以是Linux本身的
实用程序
,如ls和rm,也可以是购买的商业程序,如xv,或者是
自由软件
,如emacs)。然后shell在搜索
路径
里寻找这些应用程序(搜索路径就是一个能找到可执行程序的目录列表)。如果键入的命令不是一个内部命令并且在路径里没有找到这个
可执行文件
,将会显示一条错误信息。如果能够成功找到命令,该内部命令或应用程序将被分解为
系统调用
并传给Linux内核。
shell的另一个重要特性是它自身就是一个解释型的程序设计语言,shell程序设计语言支持绝大多数在
高级语言
中能见到的程序
元素
,如
函数

变量

数组
和程序控制结构。shell
编程语言
简单易学,任何在提示符中能键入的命令都能放到一个可执行的shell程序中。
当普通用户成功登录,系统将执行一个称为shell的程序。正是shell进程提供了命令行提示符。作为默认值(TurboLinux系统默认的shell是BASH),对普通用户用“$”作提示符,对超级用户(root)用“#”作提示符。
一旦出现了shell提示符,就可以键入
命令名称
及命令所需要的
参数
。shell将执行这些命令。如果一条命令花费了很长的时间来运行,或者在屏幕上产生了大量的输出,可以从
键盘
上按ctrl+c发出中断
信号
来中断它(在正常结束之前,中止它的执行)。
当用户准备结束登录对话进程时,可以键入logout命令、exit命令或文件结束符(EOF)(按ctrl+d实现),结束登录。

‘叁’ linux下的 shell到底是什么啊

在计算机科学中,是指“提供用户使用界面”的软件,通常指的是命令行界面的解析器。一般来说,shell是指操作系统中,提供访问内核所提供之服务的程序。

‘肆’ linux shell 中&>是什么意思

& 后台执行
> 输出到
不过联合使用也有其他意思,比如nohup输出重定向上的应用
例子:nohup abc.sh > nohup.log 2>&1 &
其中2>&1 指将STDERR重定向到前面标准输出定向到的同名文件中,即&1就是nohup.log

‘伍’ Linux shell #* 是什么意思

这是 变量扩展表达式。
"#* " 是指从变量host中删除空格前面的所有字符
一下摘取自 bash manual

${parameter#word}
${parameter##word}
The word is expanded to proce a pattern just as in filename expansion (see section 3.5.8 Filename Expansion). If the pattern matches the beginning of the expanded value of parameter, then the result of the expansion is the expanded value of parameter with the shortest matching pattern (the `#' case) or the longest matching pattern (the `##' case) deleted. If parameter is `@' or `*', the pattern removal operation is applied to each positional parameter in turn, and the expansion is the resultant list. If parameter is an array variable subscripted with `@' or `*', the pattern removal operation is applied to each member of the array in turn, and the expansion is the resultant list.
${parameter%word}
${parameter%%word}
The word is expanded to proce a pattern just as in filename expansion. If the pattern matches a trailing portion of the expanded value of parameter, then the result of the expansion is the value of parameter with the shortest matching pattern (the `%' case) or the longest matching pattern (the `%%' case) deleted. If parameter is `@' or `*', the pattern removal operation is applied to each positional parameter in turn, and the expansion is the resultant list. If parameter is an array variable subscripted with `@' or `*', the pattern removal operation is applied to each member of the array in turn, and the expansion is the resultant list.
${parameter/pattern/string}
${parameter//pattern/string}

The pattern is expanded to proce a pattern just as in filename expansion. Parameter is expanded and the longest match of pattern against its value is replaced with string. In the first form, only the first match is replaced. The second form causes all matches of pattern to be replaced with string. If patternbegins with `#', it must match at the beginning of the expanded value of parameter. If pattern begins with `%', it must match at the end of the expanded value of parameter. If string is null, matches of pattern are deleted and the / following pattern may be omitted. If parameter is `@' or `*', the substitution operation is applied to each positional parameter in turn, and the expansion is the resultant list. If parameter is an array variable subscripted with `@' or `*', the substitution operation is applied to each member of the array in turn, and the expansion is the resultant list.

${parameter:-word}
If parameter is unset or null, the expansion of word is substituted. Otherwise, the value of parameter is substituted.
${parameter:=word}
If parameter is unset or null, the expansion of word is assigned to parameter. The value of parameter is then substituted. Positional parameters and special parameters may not be assigned to in this way.
${parameter:?word}
If parameter is null or unset, the expansion of word (or a message to that effect if word is not present) is written to the standard error and the shell, if it is not interactive, exits. Otherwise, the value of parameter is substituted.
${parameter:+word}
If parameter is null or unset, nothing is substituted, otherwise the expansion of word is substituted.
${parameter:offset}
${parameter:offset:length}
Expands to up to length characters of parameter starting at the character specified by offset. If length is omitted, expands to the substring of parameter starting at the character specified by offset. length and offset are arithmetic expressions (see section 6.5 Shell Arithmetic). This is referred to as Substring Expansion.

length must evaluate to a number greater than or equal to zero. If offset evaluates to a number less than zero, the value is used as an offset from the end of the value of parameter. If parameter is `@', the result is length positional parameters beginning at offset. If parameter is an array name indexed by `@' or`*', the result is the length members of the array beginning with ${parameter[offset]}. Substring indexing is zero-based unless the positional parameters are used, in which case the indexing starts at 1.
${!prefix*}
Expands to the names of variables whose names begin with prefix, separated by the first character of the IFS special variable.
${#parameter}
The length in characters of the expanded value of parameter is substituted. If parameter is `*' or `@', the value substituted is the number of positional parameters. If parameter is an array name subscripted by `*' or `@', the value substituted is the number of elements in the array.

‘陆’ Linux shell 脚本中, $@ 和$# 分别是什么意思

直接看示例:
[root@localhost xly]# cat t.sh
#!/bin/bash
echo $#
echo $@
[root@localhost xly]# sh t.sh
0
[root@localhost xly]# sh t.sh a b c
3
a b c
说明:
$@表示所有参数
$#表示所有参数的个数

‘柒’ linux中的shell究竟是什么

【一】
shell的含义:
首先shell的英文含义是“壳”;
它是相对于内核来说的,因为它是建议在核的基础上,面向于用户的一种表现形式,比如我们看到一个球,见到的是它的壳,而非核。
Linux中的shell,是指一个面向用户的命令接口,表现形式就是一个可以由用户录入的界面,这个界面也可以反馈运行信息;
【二】shell在Linux中的存在形式:
由于Linux不同于Windows,Linux是内核与界面分离的,它可以脱离图形界面而单独运行,同样也可以在内核的基础上运行图形化的桌面。
这样,在Linux系统中,就出现了两种shell表现形式,一种是在无图形界面下的终端运行环境下的shell,另一种是桌面上运行的类型Windows
的MS-DOS运行窗口,前者我们一般习惯性地简称为终端,后者一般直接称为shell
【三】shell如何执行用户的指令
shell有两种执行指令的方式,一种方法是用户事先编写一个sh脚本文件,内含shell脚本,而后使用shell程序执行该脚本,这种方式,我们习惯称为shell编程。
第二种形式,则是用户直接在shell界面上执行shell命令,由于shell界面的关系,大家都习惯一行行的书写,很少写出成套的程序来一起执行,所以也称命令行。
总结:shell可以说只是为用户与机器之间搭建成的一个桥梁,让我们能够通过shell来对计算机进行操作和交互,从而达到让计算机为我们服务的目的。
以上。
参考资料:
【shell的定义】http://ke..com/view/849.htm
【shell学习】http://wiki.ubuntu.org.cn/Shell%E7%BC%96%E7%A8%8B%E5%9F%BA%E7%A1%80

‘捌’ linux shell中'""和`的区别

和现在的开发语言一样,语法上有些差异!

三种主要的 Shell 与其分身

在大部份的UNIX系统,三种着名且广被支持的shell 是Bourne shell(AT&T shell,在 Linux 下是BASH)、C shell(Berkeley shell,在 Linux 下是TCSH)和 Korn shell(Bourne shell的超集)。这三种 shell 在交谈(interactive)模式下的表现相当类似,但作为命令文件语言时,在语法和执行效率上就有些不同了。

Bourne shell 是标准的 UNIX shell,以前常被用来做为管理系统之用。大部份的系统管理命令文件,例如 rc start、stop 与shutdown 都是Bourne shell 的命令档,且在单一使用者模式(single user mode)下以 root 签入时它常被系统管理者使用。Bourne shell 是由 AT&T 发展的,以简洁、快速着名。 Bourne shell 提示符号的默认值是 $。

C shell 是柏克莱大学(Berkeley)所开发的,且加入了一些新特性,如命令列历程(history)、别名(alias)、内建算术、档名完成(filename completion)、和工作控制(job control)。对于常在交谈模式下执行 shell 的使用者而言,他们较喜爱使用 C shell;但对于系统管理者而言,则较偏好以 Bourne shell 来做命令档,因为 Bourne shell 命令档比 C shell 命令档来的简单及快速。C shell 提示符号的默认值是 %。

Korn shell 是Bourne shell 的超集(superset),由 AT&T 的 David Korn 所开发。它增加了一些特色,比 C shell 更为先进。Korn shell 的特色包括了可编辑的历程、别名、函式、正规表达式万用字符(regular expression wildcard)、内建算术、工作控制(job control)、共作处理(coprocessing)、和特殊的除错功能。Bourne shell 几乎和 Korn shell 完全向上兼容(upward compatible),所以在 Bourne shell 下开发的程序仍能在 Korn shell 上执行。Korn shell 提示符号的默认值也是 $。在 Linux 系统使用的 Korn shell 叫做 pdksh,它是指 Public Domain Korn Shell。

除了执行效率稍差外,Korn shell 在许多方面都比 Bourne shell 为佳;但是,若将 Korn shell 与 C shell 相比就很困难,因为二者在许多方面都各有所长,就效率和容易使用上看,Korn shell 是优于 C shell,相信许多使用者对于 C Shell 的执行效率都有负面的印象。

在shell 的语法方面,Korn shell 是比较接近一般程序语言,而且它具有子程序的功能及提供较多的资料型态。至于 Bourne shell,它所拥有的资料型态是三种 shell 中最少的,仅提供字符串变量和布尔型态。在整体考量下 Korn shell 是三者中表现最佳者,其次为 C shell,最后才是 Bourne shell,但是在实际使用中仍有其它应列入考虑的因素,如速度是最重要的选择时,很可能应该采用 Bourne shell,因它是最基本的 shell,执行的速度最快。
作者: benny_feng 发布日期: 2006-10-17
tcsh 是近几年崛起的一个软件(Linux 下的C shell 其实就是使用 tcsh)执行,它虽然不是UNIX 的标准配备,但是从许多地方您都可以下载到它。如果您是 C shell 的拥护者,笔者建议不妨试试 tcsh,因为您至少可以将它当作是 C shell 来使用。如果您愿意花点时间学习,您还可以享受许多它新增的优越功能,例如:

1. tcsh 提供了一个命令列(command line)编辑程序。

2. 提供了命令列补全功能。

3. 提供了拼字更正功能。它能够自动检测并且更正在命令列拼错的命令或是单字。

4. 危险命令侦测并提醒的功能,避免您一个不小心执行了rm* 这种杀伤力极大的命令。

5. 提供常用命令的快捷方式(shortcut)。

bash 对 Bourne shell 是向下兼容(backward compatible),并融入许多C shell 与Korn shell 的功能。这些功能其实 C shell(当然也包括了tcsh)都有,只是过去 Bourne shell 都未支持。以下笔者将介绍 bash 六点重要的改进(详细的使用说明笔者会在以后的章节介绍):

1. 工作控制(job contorl)。bash 支持了关于工作的讯号与指令,本章稍后会提及。

2. 别名功能(aliases)。alias 命令是用来为一个命令建立另一个名称,它的运作就像一个宏,展开成为它所代表的命令。别名并不会替代掉命令的名称,它只是赋予那个命令另一个名字。

3. 命令历程(command history)。BASH shell 加入了C shell 所提供的命令历程功能,它以 history 工具程序记录了最近您执行过的命令。命令是由 1 开始编号,默认值为500。history 工具程序是一种短期记忆,记录您最近所执行的命令。要看看这些命令,您可以在命令列键入 history,如此将会显示最近执行过之命令的清单,并在前方加上编号。

这些命令在技术上每个都称为一个事件。事件描述的是一个已经采取的行动(已经被执行的命令)。事件是依照执行的顺序而编号,越近的事件其编号码越大,这些事件都是以它的编号或命令的开头字符来辨认的。history 工具程序让您参照一个先前发生过的事件,将它放在命令列上并允许您执行它。最简单的方法是用上下键一次放一个历程事件在您的命令列上;您并不需要先用 history 显示清单。按一次向上键会将最后一个历程事件放在您的命令列上,再按一次会放入下一个历程事件。按向下键则会将前一个事件放在命令列上。

‘玖’ linux中的shell是什么有什么作用

Shell是人机交互用的一个程序。
用户有shell了就可以登陆系统并且可以用命令和系统交互,肯定不安全。
没有shell就无法用命令去和系统对话,自然安全多了。
最“土”的办法就是用用户名和密码登陆一下,能进去就是有用户shell,进不去就是没有用户shell。

热点内容
跳转页源码 发布:2024-09-17 03:13:05 浏览:542
html文件上传表单 发布:2024-09-17 03:08:02 浏览:783
聊天软件编程 发布:2024-09-17 03:00:07 浏览:725
linuxoracle安装路径 发布:2024-09-17 01:57:29 浏览:688
两个安卓手机照片怎么同步 发布:2024-09-17 01:51:53 浏览:207
cf编译后没有黑框跳出来 发布:2024-09-17 01:46:54 浏览:249
安卓怎么禁用应用读取列表 发布:2024-09-17 01:46:45 浏览:524
win10设密码在哪里 发布:2024-09-17 01:33:32 浏览:662
情逢敌手迅雷下载ftp 发布:2024-09-17 01:32:35 浏览:337
安卓如何让软件按照步骤自动运行 发布:2024-09-17 01:28:27 浏览:197