开始学习Liux shell洛

 

 

工作需要了解linux shell 编程,以前接触比较少!就知道些简单的命令,希望现在还来得及。最近工作比较闲,所以有很多时间可以空下来学习。废话不多说,开始看文档,我在网上找的这篇。http://www.freeos.com/guides/lsst/。本人现在觉得看英文文档比中文文档要有意思!嘿嘿:)

 

"do it yourself or learn by doing "

 

如果大家有好的东西推荐,欢迎留言。

 

shell wiki: http://en.wikipedia.org/wiki/Unix_shell

 

shell的种类

Linux中的shell有多种类型,其中最常用的几种是 Bourne shell(sh)、C shell(csh)和Korn shell(ksh)。三种shell各有优缺点。Bourne shell是UNIX最初使用的shell,并且在每种UNIX上都可以使用。Bourne shell在shell编程方面相当优秀,但在处理与用户的交互方面做得不如其他几种shell。Linux操作系统缺省的shell是Bourne Again shell,它是Bourne shell的扩展,简称Bash,与Bourne shell完全向后兼容,并且在Bourne shell的基础上增加、增强了很多特性。Bash放在/bin/bash中,它有许多特色,可以提供如命令补全、命令编辑和命令历史表等功能,它还包含 了很多C shell和Korn shell中的优点,有灵活和强大的编程接口,同时又有很友好的用户界面。

C shell是一种比Bourne shell更适于编程的shell,它的语法与C语言很相似。 Linux为喜欢使用C shell的人提供了Tcsh。Tcsh是C shell的一个扩展版本。Tcsh包括命令行编辑、可编程单词补全、拼写校正、历史命令替换、作业控制和类似C语言的语法,它不仅和Bash shell是提示符兼容,而且还提供比Bash shell更多的提示符参数。

Korn shell集合了C shell和Bourne shell的优点并且和Bourne shell完全兼容。Linux系统提供了pdksh(ksh的扩展),它支持任务控制,可以在命令行上挂起、后台执行、唤醒或终止程序。

 

something about linux:

  • Like Unix, Linux is also written is C.
  • Like Unix, Linux is also the Multi-user/Multitasking/32 or 64 bit Network OS.
  • Like Unix, Linux is rich in Development/Programming environment.
  • Like Unix, Linux runs on different hardware platform; for e.g.
    • Intel x86 processor (Celeron/PII/PIII/PIV/Old-Pentiums/80386/80486)
    • Macintosh PC's 
    • Cyrix processor 
    • AMD processor 
    • Sun Microsystems Sparc processor
    • Alpha Processor (Compaq

    刚好需要vi相关的东西,既然看到,先留着,以备不时只需。

Common vi editor command list

For this Purpose Use this vi Command Syntax
To insert new textesc + i ( You have to press 'escape' key then 'i')
To save fileesc + : + w (Press 'escape' key  then 'colon' and finally 'w')
To save file with file name (save as)esc + : + w  "filename"
To quit the vi editoresc + : + q
To quit without savingesc + : + q!
To save and quit vi editoresc + : + wq
To search for specified word in forward directionesc + /word (Press 'escape' key, type /word-to-find, for e.g. to find word 'shri ', type as
/shri )
To continue with search n
To search for specified word in backward directionesc + ?word (Press 'escape' key, type word-to-find)
To copy the line where cursor is locatedesc + yy
To paste the text just deleted or copied at the cursoresc + p
To delete entire line where cursor is locatedesc + dd
To delete word from cursor positionesc + dw
To Find all occurrence of given word and Replace then globally without confirmation esc + :$s/word-to-find/word-to-replace/g

For. e.g. :$s/mumbai/pune/g
Here word "mumbai" is replace with "pune"

 

To Find all occurrence of given word and Replace then globally with confirmationesc + :$s/word-to-find/word-to-replace/cg
To run shell command like ls, cp or date etc within viesc + :!shell-command

For e.g. :!pwd

 

补充个东西,什么情况下不适合shell

1,资源密集型的任务,尤其在需要考虑效率时(比如,排序,hash 等等)
2.需要处理大任务的数学操作,尤其是浮点运算,精确运算,或者复杂的算术运算
(这种情况一般使用C++或FORTRAN 来处理)
3有跨平台移植需求(一般使用C 或Java)
4复杂的应用,在必须使用结构化编程的时候(需要变量的类型检查,函数原型,等等)
5对于影响系统全局性的关键任务应用。
6对于安全有很高要求的任务,比如你需要一个健壮的系统来防止入侵,破解,恶意破坏等等.
7项目由连串的依赖的各个部分组成。
8需要大规模的文件操作
9需要多维数组的支持
10需要数据结构的支持,比如链表或数等数据结构
11需要产生或操作图形化界面GUI
12需要直接操作系统硬件
13需要I/O 或socket 接口
14需要使用库或者遗留下来的老代码的接口
15私人的,闭源的应用(shell 脚本把代码就放在文本文件中,全世界都能看到)

 

1 利用shell 进行表达式计算:

netxu26:~ # expr 6+3
6+3
netxu26:~ # expr 6 + 3
9

看到区别了吗? 操作数前面必须加上空格。

Syntax: expr   op1   operator   op2
Where, op1 and op2 are any Integer Number (Number without decimal point) and operator can be
+ Addition
- Subtraction
/ Division

 

 

expr 10 /* 3 - Multiplication use /* and not * since its wild card.

 

 

先搞清楚一些基本问题:

 

你可能需要自己安装个linux玩玩,不过公司有云,我随便create 个instance 就ok。

Where to get linux?

 

Linux distributions.
Website/Logo
Debian GNU/Linux: http://www.debian.org/
Slackware Linux: http://www.slackware.com/

 

理解linux的kernel。不是很深刻,但是基本了解足矣。

Linux 的kernel:

It manages resource of Linux Os. Resources means facilities available in Linux. For e.g. Facility to store data, print data on printer, memory, file management etc

 

Linux Kernel between you/shell/apps and your computers hardware

It's Memory resident portion of Linux. It performance following task :-

  • I/O management
  • Process management
  • Device management
  • File management
  • Memory management

到底什么是linux shell?

In Os there is special program called Shell. Shell accepts your instruction or commands in English (mostly) and if its a valid command, it is pass to kernel.

 

Shell is a user program or it's environment provided for user interaction.

Shell is an command language interpreter that executes commands read from the standard input device (keyboard) or from a file.

 

Shell is not part of system kernel , but uses the system kernel to execute programs, create files etc.

 

查看本机可用的shell以及当前使用的shell

netxu26:~ # cat /etc/shells
/bin/ash
/bin/bash
/bin/bash1
/bin/csh
/bin/false
/bin/ksh
/bin/sh
/bin/tcsh
/bin/true
/bin/zsh
/usr/bin/csh
/usr/bin/ksh
/usr/bin/passwd
/usr/bin/bash
/usr/bin/tcsh
/usr/bin/zsh
netxu26:~ # echo $SHELL
/bin/bash

 

 

什么是shell 脚本:简单的说写入文件的一些列command line

 

Shell script defined as:
"Shell Script is series of command written in plain text file . Shell script is just like batch file is MS-DOS but have more power than the MS-DOS batch file. "

 

 

 


 

创建一个简单的shell 脚本的步骤:

第一步:用vi或者其他编辑器编写你的shell脚本

第二步骤:加权限:

syntax:
chmod permission your-script-name

Examples:
$ chmod +x your-script-name
$ chmod 755 your-script-name

第三步:执行:

syntax:
bash your-script-name
sh your-script-name
./your-script-name

Examples:
$ bash bar
$ sh bar
$ ./bar

 

NOTE In the last syntax ./ means current directory, But only . (dot) means execute given command file in current shell without starting the new copy of shell , The syntax for . (dot) command is as follows
Syntax:
. command-name

System Variable
Meaning
BASH=/bin/bashOur shell name
BASH_VERSION=1.14.7(1)Our shell version name
COLUMNS=80No. of columns for our screen
HOME=/home/vivekOur home directory
LINES=25No. of columns for our screen
LOGNAME=studentsstudents Our logging name
OSTYPE=LinuxOur Os type
PATH=/usr/bin:/sbin:/bin:/usr/sbinOur path settings
PS1=[/u@/h /W]/$Our prompt settings
PWD=/home/students/CommonOur current working directory
SHELL=/bin/bashOur shell name
USERNAME=vivekUser name who is currently login to this PC

 

Quote:

There are three types of quotes

Quotes
Name
Meaning
" Double Quotes"Double Quotes" - Anything enclose in double quotes removed meaning of that characters (except / and $).
' Single quotes'Single quotes' - Enclosed in single quotes remains unchanged.
` Back quote
`Back quote` - To execute command

 

 

linux的退出状态:0表示成功,非0表示不成功,错误状态保持$?

 

By default in Linux if particular command/shell script is executed, it return two type of values which is used to see whether command or shell script executed is successful or not.

 

(1) If return value is zero (0), command is successful.
(2) If return value is nonzero , command is not successful or some sort of error executing command/shell script.

 

This value is know as Exit Status .

But how to find out exit status of command or shell script?
Simple, to determine this exit Status you can use $? special variable of shell.

 

例子:

netxu26:~ # rm ufdf
rm: cannot remove `ufdf': No such file or directory
netxu26:~ # echo $?
1

 

更多的linux 内部变量:

Shell Built in Variables

Shell Built in Variables Meaning
$# Number of command line arguments. Useful to test no. of command line args in shell script.
$* All arguments to shell
$@ Same as above
$- Option supplied to shell
$$ PID of shell
$! PID of last started background process (started with &)
  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

惹不起的程咬金

来都来了,不赏点银子么

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值