Linux - Shell编程基础
来源:互联网 发布:神兽传说java下载 编辑:程序博客网 时间:2024/06/06 03:52
用户和操作系统之间的接口
Shell分类
Shell的双重角色
命令解释程序Shell的工作步骤打印提示符得到命令行解析命令查找文件准备参数执行命令独立的程序设计语言解释器KISS (Keep It Small and Stupid)可复用工具tools重定向和管道
也称Shell script(Shell脚本)
是一组命令
#!/bin/shls -altouch aacp aa bb
Shell编程的基础知识
Linux环境
Linux命令
Shell程序结构
Shell脚本的执行方法
#!/bin/sh# first.sh# This file looks through all the files in the current# directory for the string POSIX, and then displays those# files to the standard outputfor file in *do if grep –q POSIX $file then more $file fidoneexit 0
方法1$ sh script_file 方法2chmod +x script_file (可选chown, chgrp)./script_file方法3source script_file,或.script_file
Shell启动文件
sh/etc/profile login shell, system wide~/.profile login shellENVcsh/etc/csh.cshrc always, system wide/etc/csh.login login shell, system wide~/.cshrc always~/.login login shell~/.logout logout shell/etc/csh.logout logout shell, system widetcsh~/.tcshrc login shellbash/etc/profile ~/.bash_profile ~/.bash_login ~/.bash_profile/etc/bash.bashrc ~/.bashrcBASH_ENV
Shell输入/输出重定向举例
>:输出重定向$ ls –l > lsoutput.txt >>:追加$ ps >> lsoutput.txt出错输出重定向 (2>)$ kill –HUP 1234 > killout.txt 2> error.txt<:输入重定向$ more < killout.txt
Shell程序设计的语法
Shell环境变量
Shell变量赋值
$ salutation=Hello$ echo $salutationHello$ salutation="Yes Dear"$ echo $salutationYes Dear$ salutation=7+5$ echo $salutation7+5
Shell变量访问
% echo “$PAGER”% echo “${PAGER}”使用{}避免歧异% temp_name=“haha”% temp=“hehe”% echo $temphehe% echo $temp_namehaha% echo ${temp}_namehehe_name% echo ${temp_name}haha
参数变量和内部变量举例1
假设脚本名为myscript
如果执行./myscript foo bar baz,结果如何?
#!/bin/shsalutation="Hello"echo $salutationecho "The program $0 is now runnning"echo "The 1st & the 2nd parameters were $1 & $2"echo $*exit 0
$ ./myscript foo bar bazHelloThe program ./myscript is now runnningThe 1st & the 2nd parameters were foo & barfoo bar baz
参数变量和内部变量举例2
假设脚本名为var3.sh
执行sh ./var3.sh hello world earth,输入如何?
#!/bin/sh echo "I was called with $# parameters" echo "My name is $0" echo "My first parameter is $1"echo "My second parameter is $2"echo "All parameters are $@"
$ sh ./var3.sh hello world earthI was called with 3 parameters My name is ./var3.sh My first parameter is hello My second parameter is world All parameters are hello world earth
参数变量和内部变量举例3
1 #!/bin/sh 2 echo "What is your name?" 3 read USER_NAME 4 echo "Hello $USER_NAME" 5 echo "File ${USER_NAME}_file will be created" 6 touch "${USER_NAME}_file"
代码说明
第5行使用
Shell变量引用
当包含一个或多个空格时使用”…”
当
在
Shell变量引用举例
#!/bin/shmyvar="Hi there"echo $myvar echo "$myvar" echo '$myvar' echo \$myvar echo Enter some text read myvar echo '$myvar' is $myvar exit 0
Output#!/bin/shmyvar="Hi there"echo $myvar Hi thereecho "$myvar" Hi thereecho '$myvar'$myvarecho \$myvar $myvarecho Enter some text Enter some textread myvar Helloecho '$myvar' is $myvar $myvar is Helloexit 0
0 0
- Linux Shell 编程基础
- Linux Shell编程基础
- Linux Shell编程基础
- Linux Shell编程基础
- Linux Shell编程基础
- Linux Shell编程基础
- Linux Shell编程基础
- Linux Shell编程基础
- Linux / Shell编程基础
- Linux Shell编程基础
- Linux Shell编程基础
- Linux Shell编程基础
- Linux Shell编程基础
- Linux Shell编程基础
- Linux Shell编程基础
- Linux Shell编程基础
- Linux Shell编程基础
- Linux基础Shell编程
- C++ 下载文件 HTTP
- VB.Net语法笔记
- 2015年第九周项目三:分数类中的运算符重载(续)(2)
- 15第九周项目五——方程也是类
- JSP标准标签库JSTL总结
- Linux - Shell编程基础
- 相关度度量之皮尔逊相关系数
- javascript caller 显示谁调用了我这个方法
- InstallShield Limited Edition for Visual Studio 2013 图文教程(教你如何打包.NET程序)
- 多线程
- [leetcode]Minimum Size Subarray Sum
- git使用!法则!
- JPA系列1-JPA环境搭建
- Windows系统创建ODBC数据源