5、shell 简介


一、shell 的定义及种类

  • shell 是用户和linux 操作系统之间的接口,它基本上是一个命令解释器,接收用户命令(如 ls 等),然后调用相应的应用程序。
  • 系统提供的 shell 种类如下图所示,不同的 shell 具备不同的功能,ubuntu 中默认使用 bash shell
    shell
  • 更改系统指定的 shell
    • usermod –s(hell) shellname username
    • chsh –s(hell) shellname username

二、shell 中的变量

1. shell 中变量的种类

  • 环境变量(全局变量):
    • 常用的环境变量如 PATH(可执行文件路径)、PYTHONPATH、LD_LIBRARY_PATH(程序加载运行期间查找动态链接库时指定除了系统默认路径之外的其他路径),使用 env 命令查看系统中所有的环境变量,亦可用 echo $PATH来查看某一环境变量
    • 注意:LIBRARY_PATH 是程序编译期间查找动态链接库时指定查找共享库的路径
    • 环境变量是全局的,它适用于所有当前 shell 以及其派生出来的任意子进程(包括编辑器、脚本等)
    • 环境变量对 bash 的影响:能不能执行某些命令和 PATH 有关,如果在 PATH 中找不到相关命令,屏幕会出现command not found。
  • 本地变量(局部变量):
    • 包括一些用户自定义的变量等
    • 局部变量只适用于当前 shell,即用户的当前shell环境,当用户注销,或者启用子 shell、子进程时不起作用

2. shell 环境变量的初始化

  • 系统级
    • /etc/profile:为系统的每一个用户设置环境变量信息,和 bash.bashrc 的区别是 profile 中的设置对所有类型的 shell 都是有效的
    • /etc/bash.bashrc:此文件是每个用户运行 Bash Shell 的时候都要执行的配置文件,其中设置了一部分的环境变量和命令提示符。因此此文件的配置信息影响着每一个用户的 shell 环境
    • /etc/environment:此文件定义了系统的环境变量,默认情况下只有 PATH 变量
  • 用户级
    • ~/profile:专用于当前用户的shell 信息设置,默认情况下,其设置一些环境变量,执行用户的~/.bashrc文件
    • ~/.bashrc:专用于当前用户的 Bash shell 信息,用户可以在此文件中设置自己的 shell 变量
    • alias:查看当前用户的别名设置
    • vim ~/.bashrc alias ll='ls -alF':在当前用户系统中设置命令别名,若想对系统的每个用户都是用此设置,则需要使用管理员权限修改sudo vim /etc/bashrc alias ll='ls -alF'

3. shell 中的变量设置及读取

  • 变量的设置
    • 变量与变量内容通过=号来连接,且=两边不能包含空格,变量内容包含空格则需要使用单引号或者双引号,变量内容由其他命令提供时使用$()或者代码符 ``
    • 取消变量的定义:unset variable
    • 将普通变量变为环境变量:export variable=variable_value,在命令行执行此命令的话,只能在当前的 Session 中有效;要想永久生效,需要修改系统或者用户的 profile 文件
    • 数组定义:array_name=(v1 v2 … vn),元素间用空格隔开
    • 数组引用:${array_name[index]}
  • 变量的读取
    • echo $variable,$ 取得变量的值,echo 将变量的值打印到屏幕上。
    • echo ${variable} # 变量读取的另一种格式。
    • read -p "str:" varible_name: 从标准输入中读取一行,并把输入行的每个字段的值指定给 shell 变量 varible_name(注意:字符串 str 和变量名 varible_name 间有一个空格)

三、shell 中的基本运算符

1. 算术运算符(+ - * / %)

  • 基本语法:result=$((运算内容)),变量内容由其它运算命令提供;其它非运算命令可使用 result=$(命令行)
  • eg:两数相乘result=$(($first_num*$sec_num)) or result=$(dirname $0)

2. 条件判断表达式

bash 的条件判断表达式中有三个几乎等效的符号和命令:test,[] 和 [[]][[]]是兼容性强(支持> < && || 等),而且性能比较快,在做条件运算时候,应优先考虑它!

  • i. 比较运算符

    比较运算符只支持数字,不支持字符串,除非字符串的值是数字
    Note[] 首尾要各空一格

    • -eq :检测两个数是否相等,eg:[ $a -eq $b ] or [ $a == $b ] or [[ $a == $b ]]
    • -ne :检测两个数是否不相等,eg:[ $a -eq $b ] or [ $a != $b ] or [[ $a != $b ]]
    • -gt :检测左边的数是否大于右边的;[ $a -gt $b ] or [[ $a > $b ]]
    • -lt :检测左边的数是否小于右边的;[ $a -lt $b ] or [[ $a < $b ]]
    • -ge :检测左边的数是否大于等于右边的;[ $a -ge $b ]
    • -le :检测左边的数是否小于等于右边的;[ $a -le $b ]
  • ii. 逻辑运算符

    [[]] 运算符是[]运算符的扩充。能够支持< >符号运算不需要转义符,计算速度最快!

    多条件在一起时,必须使用[[ cond1 && cond2 ]]或者[ cond1 ] && [ cond 2 ]或者[ cond1 -a cond2 ]这几种形式之一!

    • && :逻辑与,若 cmd1 正确执行($?=0),则开始执行 cmd2;若 cmd1 执行出现错误 ($?≠0),则 cmd2 不执行
    • || :逻辑或,若 cmd1 正确执行($?=0),则 cmd2 不执行;若 cmd1 执行出现错误($?≠0),则开始执行 cmd2
    • eg:[[ 2 < 3 && 4 < 5 ]] && echo 'ok',使用 echo 显示前面的执行结果,若正确执行,则显示 ok ,否则啥都不显示
  • iii. 布尔运算符

    • -a:按位与
      • 第一种方式:[ $a -lt 20 -a $b -gt 100 ]
      • 第二种方式:[ $a -lt 20 ] && [ $b -gt 100 ]
      • 第三种方式:[[ $a -lt 20 && $b -gt 100 ]]
    • -o:按位或
    • !:非运算
  • iiii. 字符串运算符

    • = 或 == :检测两个字符串是否相等,eg:[ $a = $b ]
    • != :检测两个字符串是否不相等,eg:[ $a != $b ]
  • iiiii. 文件测试运算符

    • -d file 检测文件是否是目录,如果是,则返回 true。eg:[ -d file ] or test -d file
    • -f file 检测文件是否是普通文件(既不是目录,也不是设备文件),如果是,则返回 true。eg:[ -f file ] or test -f file
    • -e file 检测文件(包括目录)是否存在,如果是,则返回 true。eg:[ -e file ] or test -e file
if [ ! -e bins/dra_bin ]; then  # 若不存在则执行下面的操作
  echo "make dirs: bins/dra_bin!"
  mkdir -p bins/dra_bin
else
  echo "bins/dra_bin exsits!"
fi

if [ ! -d "$hi_path" ]; then   若不存在则执行下面的操作
	hi_path=../../hilib
	
fi

echo "hi_path is: $hi_path" 


# shell 脚本的执行:bash start.sh

四、参考资料

1. linux shell 逻辑运算符、逻辑表达式详解

此资源包含三部分: 1、jxse-shell-2.5.zip 2、jxse-shell-doc-2.5.tar.tar 3、jxse-shell-src-2.5.tar.tar ===附上linux下使用jxtashell的说明=== (windows用户参加jxse-shell-src-2.5/win32/Jxta_Readme.html说明) Linux具体步骤如下: leekwen@leekwen:~$ unzip jxse-shell-2.5.zip leekwen@leekwen:~$ cd jxse-shell-2.5/ leekwen@leekwen:~/jxse-shell-2.5$ ls lib shell leekwen@leekwen:~/jxse-shell-2.5$ cd shell/ leekwen@leekwen:~/jxse-shell-2.5/shell$ ls jxta.exe Jxta_Readme.html run.bat runjdk.bat run.sh leekwen@leekwen:~/jxse-shell-2.5/shell$ chmod a+x run.sh leekwen@leekwen:~/jxse-shell-2.5/shell$ ./run.sh ........ ============================================= =======<[ Welcome to the JXTA Shell ]>======= ============================================= ........ JXTA> man The following commands are available: Shell JXTA Shell command interpreter cat Concatenate and display a Shell object chpgrp Change the current peer group clear Clear the shell's screen dumpcm Dump the content of the local cache (CM) env Display environment variables exit Exit the Shell exportfile Export enviroment variable to an external file flush flush a jxta advertisement get Get data from a pipe message grep Search for matching patterns groups Discover peer groups help To access help pages use the 'man' command. history No description available for this ShellApp importfile Import an external file info display info about an jxta advertisement instjar Installs jar-files containing additional Shell commands join Instantiate and join peer group leave Resign from and optionally stop a peer group logging Display and optionally adjust logging levels login Authenticate with the group's membership service. man An on-line help command that displays information about a specific Shell command mem Display memory information mkadv Make an advertisement from a document mkmsg Make a pipe message mkpipe Create a pipe more Page through a Shell object or from standard input. newmoduleclass Create a new Module Class advertisment newmodulespec Create a new Module Class advertisment newpgrp Create a new peer group advertisement newpipe Create a new pipe advertisment peerconfig Force Peer Reconfiguration peerinfo Get information about peers peers Discover peers pse.certs Display the certificates contained in the current group's PSE Membership pse.createkey Creates a key in the PSE key store pse.dumpcred Dumps a credential. pse.dupkey Creates a key in the PSE key store pse.erase Erases a key or certificate from the PSE key store pse.importcert Imports a trusted certificate chain. pse.keys Display the keys contained in the current group's PSE Membership pse.newcsr Generates certificate signing request document. pse.signcsr Signs a certificate signing request pse.status Display status infomation for the group's PSE Membership publish Publish a JXTA advertisement put Put data into a message rdvcontrol Controls rendezvous service behaviour rdvserver No description available for this ShellApp rdvstatus Display information about the rendezvous service recv Receive a message from a pipe relaystatus Display the list of relays and clients connected to this peer. remotepublish remote publish a jxta advertisement route Display information about a peer's route info rsh Connects to a remote JXTA Shell rshd Remote JXTA Shell Deamon search Discover jxta advertisements send Send a message into a pipe set Set an environment variable sftp Send a file to another peer share Share an advertisement sleep Sleep for a specified amount of milliseconds storehome Display the location of store home talk Talk to another peer transports Display information about the message transports available in the current group uninstjar Uninstalls jar-files previously installed with 'instjar' unset Removes an environment variable version Display the version number of this Shell instance. wc Count the number of lines, words, and chars in an object who Display credential information whoami Display information about this peer or the current peergroup xfer Send a file to another peer
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值