Shell Programming (i)

Source: Internet
Author: User

[TOC]

Shell programming (i) a shell introduction 1 concept

? Command interpreter

2 common shells
bash    linux标准shellsh      早期shell,较简单csh ksh tcsh    unix  shellvi /etc/shells      linux支持的shell
3 shell scripts

Example 1:

#!/bin/bashecho "hello world!"

How the script executes:

1)  赋予执行权限路径执行    /root/shell/echo.sh./echo.sh2)  bash  脚本名       脚本可以不赋予执行权限
4 Bash Common features 1) History command
默认保存1000条历史命令vi  /etc/profile        修改环境变量配置文件,要生效,必须注销HISTSIZE=1000           修改默认历史命令条数history         查询系统历史命令历史命令保存文件~/.bash_historyhistory  -w         把内存中命令历史,保存入文件history  -c         清空所有的历史命令重复历史命令!n          重复第n条命令!str        重复最后一个以str开头的命令上箭头         调用上面的命令
2) aliases
alias       查看系统中生效的别名alias  ls=‘ls  --color=never‘   手工设定别名,临时生效vi  ~/.bashrc       写入别名,永久生效
5 Input and output redirection
标准输入     /dev/stdin       0                键盘标准输出     /dev/stdout      1                显示器标准错误输出 /dev/stderr      2                显示器            #设备文件名      #文件描述符      #默认设备    
1) Output redirection

Redirect the output that should be output to the screen to the file.

>   覆盖>>  追加ls  >  aa       覆盖到aals  >>  aa      追加到aals  gdlslga  2>>aa      错误信息输出到aa       强调:错误输出,不能有空格ls  &>aa            错误和正确都输入到aa掌握ls  >> aa  2>>bb        正确信息输入aa,错误信息输入bbls  >>  aa  2>&1        错误和正确都输入到aa,可以追加            2>&1    把标准错误重定向到标准正确输出
6 multi-command sequential execution
1)命令1  ;  命令2 ; 命令3     命令123顺序执行。之间没有任何关系2)命令1  &&  命令2          命令1正确执行后,命令2才会执行3)命令1  ||  命令2          命令1执行不正确,命令2才会执行ls aa && echo "cunzai" || echo "bu cunzai!" 执行ls  aa,判断如果正确,输出“存在”。如果不存在,输出“不存在”
7 Pipe Break
命令1  |  命令2         命令1的执行结果,作为命令2的执行条件netstat -tlun | grep 80                 查询监听的端口号,并查看80端口是否启动。ls  -l  /etc/  |  more                  分屏显示ls内容ls -l /etc/ | grep yum
Two-Variable 1 classification
本地变量环境变量位置参数变量预定义变量
2 Local Variables
1)声明        变量名=变量值     注意:=号左右不能有空格    aa=1232)调用    echo  $变量名3)查看变量    set     查看所有变量,包括环境变量和本地变量4)删除    unset  变量名
3 Variable setting rules
1)变量以等号连接值,等号不能有空格2)变量名由数字和字母和下划线组成,不能以数字开头3)变量值中有空格,用引号括起来4)双引号内,有特殊字符。如$5)单引号中特殊字符无含义 6)在变量值中,可以使用\转义符7)变量值可以直接调用系统命令。    `命令`   $(命令)8)变量值可以累加       aa=123    aa="$aa"456   echo $aa  --->1234569)环境变量一般设为大写
4 environment variable 1) declaration
export  变量名=变量值export  aa
2) View
set              查看所有变量env     export       只能查看环境变量declare      声明变量类型的,如果不特别声明,所有变量为字符串型-i   声明为int  -x   声明为环境变量
3) Delete
unset  变量名
4) Common environment variables
echo $PATH/usr/kerberos/sbin:/usr/kerberos/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin系统查找命令的路径PATH="$PATH":/root/shell 在系统默认路径后,追加/root/shell目录作为命令查找路径
5) environment variable configuration file
/etc/profile/etc/bashrc      所有用户生效(两个都是)~/.bashrc~/.bash_profile  只对指定用户生效(两个都是)

Shell Programming (i)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.