`
DavyJones2010
  • 浏览: 154254 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

Linux Shell Introduction I

阅读更多

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

 

 

 

 

 

 

 

 

 

 

 

分享到:
评论

相关推荐

    Introduction to unix and linux shell - 1

    在《Introduction to Unix and Linux Shell - 1》这份文档中,作者Star Ren(2008年1月19日)为我们提供了关于Unix/Linux Shell的基础介绍。这部分内容主要涵盖了Shell的基础概念、为何学习Shell的重要性以及一些...

    Linux Shell Scripting Cookbook

    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 ...

    An Introduction to the Linux Command Shell For Beginners

    ### Linux命令Shell入门知识点 #### 一、引言与背景 在《Linux命令Shell入门》这一教程中,作者Victor Gedris旨在为初学者提供一个快速而简单的Linux命令Shell使用介绍。该文档假设读者对Linux命令行界面几乎没有...

    Introduction to unix and linux shell - 2

    【Unix/Linux Shell简介 - 2】 Unix/Linux Shell是操作系统中的一种命令解释器,它为用户提供了一个与系统交互的界面,允许用户输入命令、执行程序、管理文件和进行系统操作。Shell不仅是一个命令行接口,还是一个...

    linux_introduction.rar_linux_linux c

    1. **命令行界面**:Linux主要通过命令行界面(CLI)进行操作,如bash shell。掌握常用的Linux命令,如ls(列出目录内容)、cd(切换目录)、mkdir(创建目录)、rm(删除文件或目录)、cp(复制文件或目录)、mv...

    Introduction to Linux

    Bash(Bourne Again SHell)是Linux最常用的命令行解释器。为了有效使用Linux,用户还需掌握如何获取帮助,例如通过man命令查看手册页以及利用其他在线资源。 其他重要知识点还包括: - 如何在图形模式和文本模式下...

    Linux for Beginners: An Introduction to the Linux Operating System

    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 ...

    introduction to linux

    Bash(Bourne Again SHell)是Linux中最常用的shell。了解如何使用Bash的一些高级功能,如命令历史、别名定义和管道操作,可以极大地提高工作效率。 ##### 2.3 获取帮助 **2.3.1 man页面** 在Linux中,几乎所有的...

    The Linux Command Line A Complete Introduction

    《The Linux Command Line: A Complete Introduction》这本书通过易懂、短小精悍的章节帮助读者逐步掌握这些技能,克服对命令行的初始恐惧(通常被称作"shell shock"),并发现命令行与计算机沟通的自然和富有表现力...

    《Linux与shell编程》教学大纲(本科).docx

    3. 网络资源 MOOC (103/Introduction-to-Linux/) 操作系统原理() 该教学大纲旨在为学生提供一个系统的学习路线,帮助学生掌握 Linux 操作系统的基本原理和 shell 编程技术,并为学生提供了许多实验项目供实践。

    101 shell script for linux and unix

    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 - ...

    unix_linux_introduction.pdf

    - **Shell**: Shell是用户与Unix/Linux系统之间的接口,它能够解释用户的命令并将其传递给操作系统执行。 - **命令行工具**: 包括文件操作(如cp、mv)、进程管理(如ps、kill)、网络工具(如ping、curl)等丰富的...

    The Linux Command Line: A Complete Introduction, 2nd Edition(Linux命令行大全,第二版)

    在技术方面,内容覆盖了Linux shell的基本概念,如何使用终端模拟器,进行初步的键盘输入、命令历史回顾、光标移动,以及如何尝试一些简单的命令。此外,书中还对Linux文件系统进行了介绍,包括文件系统树的概念、...

    Best Linux Books

    - 《Linux命令行与Shell脚本编程大全》:介绍了Linux命令行的基本使用和编写Shell脚本的方法。 - 《鸟哥的Linux私房菜》:适合Linux初学者,全面覆盖了Linux基本操作、系统管理等知识。 2. **Linux系统管理**: ...

    The Linux Command Line A Complete Introduction.pdf

    《The Linux Command Line: A Complete Introduction》是William E. Shotts, Jr.所著的一本全面介绍Linux命令行的书籍,旨在带领读者从零基础学习Linux命令行,直至能够编写Bash脚本。本书不仅提供了Linux命令行的...

    高清彩版 The Linux Command Line A Complete Introduction

    ### 《高清彩版 The Linux Command Line: A Complete Introduction》知识点总结 #### 一、书籍基本信息 - **书名**:《高清彩版 The Linux Command Line: A Complete Introduction》 - **作者**:William E. ...

    linux-command-line-introduction-2nd

    本书致力于将读者从对命令行的初步了解引导至能够编写完整程序的高级水平,特别是使用当前版本的bash shell,它是Linux系统中最受欢迎的shell之一。本书覆盖了从系统管理、网络配置、软件包安装到进程管理的各项技能...

Global site tag (gtag.js) - Google Analytics