1. A simple shell script:
#!/bin/bash #This is a shell sample echo "Our first sample" echo #This insert an empty line in output echo "We are currently in the following dir" /bin/pwd echo echo "This directory contains the following files that includes .sh keyword" /bin/ls | grep .sh
2. Structure of shell
1) #! (Shebang) point out the shell we are using (/bin/bash or /bin/ksh)
2) # represents comments --> It is recommanded to explain the function of this script at the begining of the script.
3) linux commands and flow control
3. Steps to create a shell script:
1) Create a script file
2) Add x permission to this file
3) Execute this shell script using "./script.sh" or "sh script.sh"
4. Another sample shell script
#!/bin/sh #Auto mail system info /bin/date +%F >> /tmp/sysinfo echo "disk info:" >> /tmp/sysinfo /bin/df -h >> /tmp/sysinfo echo >> /tmp/sysinfo echo "online users:" >> /tmp/sysinfo /usr/bin/who | /bin/grep -v root >> /tmp/sysinfo echo >> /tmp/sysinfo echo "memory info:" >> /tmp/sysinfo /usr/bin/free -m >> /tmp/sysinfo echo >> /tmp/sysinfo #write to root /usr/bin/write root < /tmp/sysinfo && /bin/rm /tmp/sysinfo #crontab -e #0 9 * * 1-5 script
5. How to create variable in shell
1) Two kinds of variable in shell: temp var and persist var
Temp vars are vars that defined inside a shell program and it is only effective inside this program.
Temp vars include user defined vars and position vars
Persist vars are environment variables, they would be effective both inside and outside shell program.
#!/bin/bash #A sample script for displaying environment variables echo "Our system path" echo $PATH echo "Our system lang" echo $LANG #Empty result represents english echo "Our system shell" echo $SHELL echo "Our system command line prompt" echo $PS1
2) Use uppercase alphabet to name a variable by convention.
Variable name can only starts with alphabets and cannot start with digits or other character.
When we assign a value to a variable, there should be no space at the both side of "="
#!/bin/bash #This is a demo for usage of variables NUM=1 #There should be no spaces at both side of "=" echo $NUM #When we use a variable, we should add "$" prefix TIME=`date` #When we need to assign the result of a command to a var, we should use "`" echo TIME A=2 echo "The value of A is $A" B=3 echo "The value of B is $B" B=$A echo "The value of B is $B"
3) The difference between "" and '' when applied for variable usage
#!/bin/bash #A demo shows the difference between "" and '' A=1 echo "A=$A" echo 'A=$A" #Output #A=1 #A=$A
4) Delete variables
#!/bin/bash #A demo for delete variable #Show all the variables that has been defined echo "Defined variables are as below:" set A=1 echo "A=$A" unset A echo "A=$A" #Output #Defined variables are as below: #.... #A=1 #A=
5) Define variables that contains multiple words that seperated by spaces or tabs
#!/bin/bash #A demo for defining variable contains multiple words NAME="DAVY JONES" echo $NAME COMPANY="CITI CORP" echo $COMPANY INFO="NAME: $NAME, COMPANY: $COMPANY" echo $INFO
6. Introduction to position variables and special variables
1) When using shell to compile user command, the first part of the command is regarded as command name, and the other parts are regarded as params.
These params are regarded as position params, they are marked by their position in the command.
ls -l file1 file2 file3 $0 means the name of the command "ls -l" $n means the params of the command, n=1~9
#!/bin/bash #A demo for position variables and special variables #Script name variables.sh echo "Command name is: $0" echo "First position params is: $1" echo "Second position params is: $2" echo "Third position params is: $3" #Input #sh variables.sh aaa bbb ccc #Output #Command name is: variables.sh #First position params is: aaa #Second position params is: bbb #Third position params is: ccc #Input #sh variables.sh aaa #Output #Command name is: variables.sh #First position params is: aaa #Second position params is: #Third position params is: #Input #sh variables.sh aaa bbb ccc ddd #Output #Command name is: variables.sh #First position params is: aaa #Second position params is: bbb #Third position params is: ccc
2) Special variables
1) $* represents all the params in command line
2) $# represents the param number in command line
3) $$ represents the PID of current program
4) $! represents the PID of previous backend command
5) $? represents the return value of last executed command.
0 represents previous command successfully executed
non-0 represents previous command failed execution
#!/bin/bash #A demo for special variables ls -ltr | grep.sh echo "Last command execution status: $?" aaa -aa echo "Last command execution status: $?" #Output: #..... #Last command execution status: 0 variable6.sh: line6: aaa: command not found #Last command execution status: 127
#!/bin/bash #A demo for usage of special variables #Usage: sh variable7.sh file01 file02 echo '$# is:' $# echo '$* is:' $* echo '$? is:' $? echo '$$ is:' $$ echo '$0 is:' $0 #Output: #$# is: 2 #$* is: file01 file02 #$? is: 0 #$$ is: 5580 #$0 is: variable7.sh
7. How to read user input
#!/bin/bash #A demo for reading user input read first second third echo "The first param is $first" echo "The second param is $second" echo "The third param is $third" #Sample Input #sh read.sh aaa bbb ccc #Sample Output #The first param is aaa #The second param is bbb #The last param is ccc #Sample Input #sh read.sh aaa bbb #Sample Output #The first param is aaa #The second param is bbb #The third param is #Sample Input #sh read.sh aaa bbb ccc ddd #Sample Output #The first param is aaa #The second param is bbb #The third param is ccc ddd
1) A technique frequently used for debugging shell: sh -x ***.sh
8. expr
1) expr is used for arithmatic calculation for int variable
2) There should be spaces at the both end of operator
3) Think of a scenario that we want to batch add user, we use a specific prefix, and use 1, 2, 3 ... as suffix.
#!/bin/bash #A demo for usage of expr var1 = 3 var2 = 10 var3 = 13 expr 3 + 5 expr $var1 - 5 expr $var1 / $var2 expr $var3 \* $var1 #Sample Output #8 #-2 #0 #39
[root@localhost ***]# expr 3 + 5 8 [root@localhost ***]# expr 100 / 3 33 [root@localhost ***]# expr 3+5 3+5 [root@localhost ***]# expr 3 * 5 expr: syntax error [root@localhost ***]# expr 3 \* 5 15
#!/bin/bash #A demo for advanced usage of expr var1=1 var2=2 var3=3 var4=4 expr `expr $var1 + $var2` / $var3 var4=`expr $var2 / $var1` echo "Value of var4 is $var4" #Sample Output #1 #Value of var4 is 2
相关推荐
We start with an introduction to the Shell environment and basic commands used. Next, we explore process management in Linux OS, real-world essentials such as debugging and perform Shell arithmetic ...
A good introduction to Shell, including two parts. This is part 1. Please open this file with PPT 2007.
Chapter 1: Shell Something Out 7 Introduction 7 Printing in the terminal 9 Playing with variables and environment variables 12 Doing math calculations with the shell 17 Playing with file descriptors ...
A good introduction to Shell, including two parts. This is part 2. Please open this file with PPT 2007.
非常实用的Linux命令入门,包括基本的系统管理,查找等命令
Wicked Cool Shell Scripts—101 Scripts for Linux, Mac OS X, and Unix Systems Introduction Chapter 1 - The Missing Code Library Chapter 2 - Improving on User Commands Chapter 3 - ...
inux for Beginners doesn't make any assumptions about your background or knowledge of Linux. You need no prior knowledge to benefit from this book. You will be guided step by step using a logical and ...
The Linux Command Line takes you from your very first terminal keystrokes to writing full programs in Bash, the most popular Linux shell. Along the way you'll learn the timeless skills handed down by ...
Introduction of linux shell cheatsheet, it will be helpful for you.
Chapter 1 An Introduction to Linux Chapter 2 Working with the Linux Shell Chapter 3 Using the vi Text Editor Chapter 4 Managing Linux Files and Directories Chapter 5 Installing Linux Chapter 6 ...
经典的图书 linux command line introduction 第二版,2019年3月出版
PART I Installing Ubuntu Linux 21 2 Installation Overview 23 3 Step-by-Step Installation 45 PART II Getting Started with Ubuntu Linux 85 4 Introduction to Ubuntu Linux 87 5 The Linux Utilities 145 6 ...
Part I Introduction 1 Introduction to Linux 3 2 Getting Started 17 Part II The Linux Shell and File Structure 3 The Shell 35 4 The Shell Scripts and Programming 65 5 Shell Confi guration 89 6 Linux ...
但是这种命名方式给Linux命令行工具和Shell带来了困扰,因为大多数命令中,都是默认以空格做为值与值之间的分隔符,而不是做为文件名的一部分。 看下面的脚本,我们可以了解的更清楚: 执行的结果如下: 实际上,...
Part I: Getting off the Ground with Linux. Chapter 1: Starting with Linux. Chapter 2: Linux Projects, Activities, and Careers. Part II: Running a Linux Desktop. Chapter 3: Getting into the Desktop...
Linux命令行专为新命令行用户设计。 除了命令行使用和shell脚本编写的基础知识外,本书还包括有关命令行上使用的许多常见程序的章节以及更高级的主题。
This is Linux for those of us who don’t mind typing. All Linux users and administrators tend to like the flexibility and ...Chapter 14: Introduction to Bash Shell Scripting Appendix A: Installing Linux
#Introduction因此,从超级计算机到PC,许多类型的计算机上都可以使用Linux。 目前,UBUNTU是一种非常流行Linux风格。UNIX操作系统是由美国贝尔实验室的一群爱好者在1970年代初开发的。 从那以后,它被许多不同的...