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

Linux Shell Introduction II

阅读更多

1. How to test variable

1) Example:

#String test
test str1=str2    #Test if str1.equals(str2)
test str1!=str2   #Test if !str1.equals(str2)
test str1             #Test if str1 != null
test -n str1         #Test if str1 != null
test -z str1         #Test if str1 == null

#Integer test
test int1 -eq int2  #Test if int1 == int2
test int1 -ne int2  #Test if int1 != int2
test int1 -ge int2  #Test if int1 >= int2
test int1 -gt int2   #Test if int1 > int2
test int1 -le int2   #Test if int1 <= int2
test int1 -lt int2    #Test if int1 < int2

#File test
test -d file    #Test if file type is directory
test -f file     #Test if file type is file
test -x file    #Test if file is executable
test -r file     #Test if file is readable
test -w file   #Test if file is writable
test -a file    #Test if file exists
test -s file    #Test if file size != 0

#We can use
#echo $? to decide the test result
#If equals 0, that means test result is true,
#If not, that means test result if false.
$ ls -lt | grep .sh
-rwxr-xr-x ... expr.sh

$ test -d expr.sh
$ echo $?
1

$ test -x expr.sh
$ echo $?
0

$ test -w expr.sh
$ echo $?
0

$ chmod 555 expr.sh
$ test -w expr.sh
$ echo $?
1

2) "test" is commonly used together with "if"

#!/bin/bash
#A demo for usage of test

if [ -d $1 ]
then
    echo "Is directory"
fi

# "test -d $1" is the same as "[ -d $1 ]"

#Sample Input:
#sh test.sh Templates
#Sample Output:
#Is directory

 

2. if ... then ... else ... fi

#!/bin/bash
#A demo for usage of if statement

if [ -d $1 ]
then
    echo "Is directory"
else
    echo "Not directory"
fi

#Sample Input
#bash test.sh Templates
#Sample Output
#Is directory

#Sample Input
#bash test.sh test.sh
#Sample Output
#Not directory
#!/bin/bash
#A demo for usage of complex if statement

if [ -d $1 ]
    then
    echo "Is directory"
elif [ -f $1 ]
    then
    echo "Is file"
elif [ -c $1 ]
    then
    echo "Is device file"
else
    echo "Is unknown file"

#Sample Input
#sh test2.sh test2.sh
#Sample Output
#Is file

#Sample Input
#sh test2.sh donotexist.sh
#Sample Output
#Is unknown file
#!/bin/bash
#A demo for usage of more complex if statement

if [ -d $1 ]
    then
    echo "Is directory"
elif [ -f $1 ]
    then
    echo "Is file"
    if [ -r $1 ]
        then
        echo "File is readable"
    else
        echo "File is not readable"
    if
    if [ -w $1 ]
        then
        echo "File is writable"
    else
       echo "File is not writable"
    fi
    if [ -x $1 ]
        then
        echo "File is executable"
    else
        echo "File is not executable"
    fi
elif [ -c $1 ]
    then
    echo "Is a device file"
else
    echo "Is an unknown file"
fi

#Sample Input
#sh test2.sh test2.sh
#Sample Output
#Is file
#File is readable
#File is writable
#File is executable
#!/bin/bash

if [ $# -ne 2 ]
    then
    echo "Not enough params"
    exit 0
if [ $1 -eq $2 ]
    then
    echo "$1 equals $2"
elif [ $1 -gt $2 ]
    then
    echo "$1 greater than $2"
elif [ $1 - lt $2 ]
    then
    echo "$1 less than $2"
else
    echo "You will never get here"
fi

#Sample Input
#sh compare.sh 1 2
#Sample Output
#1 less than 2
#Sample Input
#sh compare.sh 2 1
#Sample Output
#2 greater than 1
#Sample Input
#sh compare.sh 2 2
#Sample Output
#2 euqlas 2

 

3. AND OR

1) -a: AND

2) -o: OR

 

4. Exit

1) exit 0: means normal exit process

 

5. for ... in ... do ... done

#!/bin/bash

for DAY in Sunday Monday Tuesday Wednesday Thursday Friday Saturday
do
    echo "The day is $DAY"
done

#Sample Output
#The day is Sunday
#The day is Monday
#The day is Tuesday
#The day is Wednesday
#The day is Thursday
#The day is Friday
#The day is Saturday

 

6. Example

#!/bin/bash
#"if ... else" usage
#Using this program to show your system's services

echo "Now, the web services of this Linux System will be detected..."
echo 

#Detect www service
web=`/usr/bin/pgrep httpd`
if [ "$web" != "" ]
then
    echo "The Web service is running."
else
    echo "The Web service is NOT running."
    /etc/rc.d/init.d/httpd start
fi

 

7. awk

#detect user whose UID=0 (means SUPER USER)
$grep root /etc/passwd
#format:
#username:password:UID:GID:userinfo:homefolder:shell
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin

$awk -F: '$3==0 {print $1}' /etc/passwd

#-F --> assign the seperator in each line, the default seperator is space, here is ":"
#'$3==0 {print $1}' means that if the third segment equals 0, then we will print out the first segment
#/etc/passwd means the file that will be processed


$ps -el
PID    PPID  PGID   WINPID TTY UID  STIME      COMMAND
5212 1       5212    5212     con 500 20:36:31 /bin/sh
4008 5212 4008    4448     con 500 21:03:03 /bin/ps

$ps -el | awk '$1==5212 {print $8}'
/bin/bash
$ps -el | awk 'length($2)==1 {print $8}'
/bin/sh
$ps -el | awk 'length($2)==4 {print $8}'
COMMAND/bin/ps/bin/sh

#detect user whose password is empty
$awk -F: 'length($2)=0 {print $1}' /etc/shadow

 

8. Example

#!/bin/bash

/bin/echo "Please input username"
read username
/bin/grep $username /etc/passwd > /dev/null 2> /dev/null
if [ $? -eq 0 ]
then
    /bin/echo "Username is: $username"
else
    /bin/echo "User $username doesn't exist"
    exit 1
fi
/bin/echo

#list /etc/passwd info
#grep ^$username:x --> means that find lines which start with $username:x
#grep $username
#root:x:0:0:root:/root:/bin/bash
#operator:x:11:0:operator:/root:/sbin/nologin
#grep ^$username:x
#root:x:0:0:root:/root:/bin/bash --> Here is the valur of $userinfo
userinfo=`/bin/grep ^$username:x /etc/passwd`
userid=`/bin/echo $userinfo | /bin/awk -F: '{print $3}'`
groupid=`/bin/echo $userinfo | /bin/awk -F: '{print $4}'`
homedir=`/bin/echo $userinfo | /bin/awk -F: '{print $6}'`
shell=`/bin/echo $userinfo | /bin/awk -F: '{print $7}'`

/bin/echo "userid = $userid"
/bin/echo "groupid = $groupid"
/bin/echo "homedir = $homedir"
/bin/echo "shlle = $shell"

 

9. killuser

#!/bin/bash
#The script to kill logined user

username="$1"

#find all processes that belongs to this user
/bin/ps -aux | /bin/grep $username | /bin/awk '{print $2}' > /tmp/temp.id
killid=`cat /tmp/temp.pid`

#kill all the processes that started by this user
for PID in $killid
do
    /bin/kill -9 $PID 2> /dev/null
done
#once we have killed all the processes that started by a specific user, that means the user is no longer logged in current system.
分享到:
评论

相关推荐

    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

    ii Table of Contents Slicing file names based on extension 84 Renaming and moving files in bulk 86 Spell checking and dictionary manipulation 89 Automating interactive input 90 Chapter 3: File In, ...

    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命令查看手册页以及利用其他在线资源。 其他重要知识点还包括: - 如何在图形模式和文本模式下...

    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)等丰富的...

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

    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