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

Linux Shell Introduction III

阅读更多

1) select var in keywords do ops done

#!/bin/bash
#select regards each item in keywords as form, and do ops interactiv ely

echo "What is your favourite OS?"

select var in "LINUX" "UNIX" "WINDOWS" "OTHER"
do
    break
done

echo "You have selected $var"

#Sample Input
#sh select.sh
#What is your favourite OS?
# 1) LINUX
# 2) UNIX
# 3) WINDOWS
# 4) OTHER
# #? 1
# You have selected LINUX

 

2) case var in str1) ops ;; str2) ops ;; str3) ops ;; ... *) ops esac

#!/bin/bash
#Demo for case usage

echo "************"
echo "Please select your operation:"
echo " Press C to Copy"
echo " Press D to Delete"
echo " Press B to Backup"
echo "************"

read op
case $op in
    C)
    echo "You have selected Copy"
    ;;
    D)
    echo "You have selected Delete"
    ;;
    B)
    echo "You have selected Backup"
    ;;
    *)
    echo "Invalid selection"
esac

 

3) while *** do ops done

#!/bin/bash
#Demo for while usage

NUM=1
SUM=0

while [ $NUM -le 10 ]
do
    SUM='expr $NUM + $SUM`
    NUM=`expr $NUM + 1`
done

echo "Sum is $SUM"

#Output Sample
#Sum is 55
#!/bin/bash
#Demo for while break usage

while [ -d /etc ]
do
    ls -ld /etc
    break
done

 

4) batch add user

#Normal add user
#useradd davy
#passwd davy
#Changing password for user davy
#New UNIX password:
...

#How to escape the interactive process when set password for user?
#Example below will solve this problem:
$echo 123455 | passwd --stdin davy

#!/bin/bash
#Demo for batch add user

#Read user
echo "Please input user prefix"
read userprefix
echo "Please input user count"
read count
#Set user
n=1
while [ $n -le $count ]
do
    useradd $userprefix$n
    n=`expr $n + 1`
done

#Read password
echo "Please input password prefix"
read pwdprefix
#Set password
n=1
while [ $n -le $count ]
do
    echo $pwdprefix$n | passwd --stdin $userprefix$n
    n=`expr $n + 1`
done

 

5) batch delete user

#!/bin/bash
#Demo for batch delete user

echo "Please input userprefix"
read userprefix
echo "Please input user count"
read usercount

echo "Start batch deleting user..."
n=1
while [ $n -le $usercount ]
do
    usr/sbin/userdel -r $userprefix$n
    n=`expr $n + 1`
done
echo "Finished batch deleting user..."

 

6) until ... do ops done

#!/bin/bash
#Demo for until usage

N=1
COUNT=51
SUM=0

until [ $N -eq $COUNT ]
do
    SUM=`expr $SUM + $N`
    N=`expr $N + 1`
done

echo "SUM IS $SUM"

 

7) break/continue

#!/bin/bash
#Demo for break/continue usage

while true
do
    echo "Please select operation"
    echo "1 COPY"
    echo "2 DELETE"
    echo "3 BACKUP"
    echo "4 QUIT"
    read op
    case $op in
        1)
        echo "YOUR SELECTION IS COPY"
        ;;
        2)
        echo "YOUR SELECTION IS DELETE"
        ;;
        3)
        echo "YOUR SELECTION IS BACKUP"
        ;;
        4)
        echo "QUITTING>>>"
        break;
        ;;
        *)
        echo "INVALID SELECTION"
        ;;
    esac
done

 

8) shift

1) It is used to move all the params left by one position.

    The params that moved out of space can no longer be accessed.

2) Shift one position will cause param count decrease by one

3) Shift is useful when input param number is more than 9

#!/bin/bash
#Demo for shift usage

echo "Number of args is $#"
echo "arg1=$1, arg2=$2, arg3=$3"
shift
echo "Number of args is $#"
echo "arg1=$1, arg2=$2, arg3=$3"
shift
echo "Number of args is $#"
echo "arg1=$1, arg2=$2, arg3=$3"
shift

#Sample Input
#sh shift.sh 1 2 3
#Sample Output
#Number of args is 3
#arg1=1, arg2=2, arg3=3
#Number of args is 2
#arg1=2, arg2=3, arg3=
#Number of args is 1
#arg1=3, arg2=, arg3=
#!/bin/bash
#Demo for shift usage

echo "Number of args is $#"
echo "arg1=$1, arg2=$2, arg3=$3, arg4=$4, arg5=$5, arg6=$6, arg7=$7, arg8=$8, arg9=$9
shift
echo "Number of args is $#"
echo "arg1=$1, arg2=$2, arg3=$3, arg4=$4, arg5=$5, arg6=$6, arg7=$7, arg8=$8, arg9=$9
shift
echo "Number of args is $#"
echo "arg1=$1, arg2=$2, arg3=$3, arg4=$4, arg5=$5, arg6=$6, arg7=$7, arg8=$8, arg9=$9

#Sample Input
#sh shift.sh 1 2 3 4 5 6 7 8 9 10 11
#Sample Output
#Number of args is 11
#arg1=1, arg2=2, arg3=3, arg4=4, arg5=5, arg6=6, arg7=7, arg8=8, arg9=9
#Number of args is 10
#arg1=2, arg2=3, arg3=4, arg4=5, arg5=6, arg6=7, arg7=8, arg8=9,  arg9=10
#Number of args is 9
#arg1=3, arg2=4, arg3=5, arg4=6, arg5=7, arg6=8, arg7=9, arg8=10,  arg9=11
#!/bin/bash
#Demo for shift usage

if [ $# -le 0 ]
then
    echo "Not enough param"
    exit 1
fi

COUNT=0
SUM=0
while [ $# -gt 0 ]
do
    COUNT=`expr $COUNT + 1`
    SUM=`expr $SUM + $1`
    shift
done

echo "SUM IS $SUM
echo "COUNT IS $COUNT"

#Sample Input
#sh shift2.sh
#Sample Output
#Not enough param
#Sample Input
#sh shift2.sh 1 2 3
#Sample Output
#SUM IS 6
#COUNT IS 3
#Sample Input
#sh shift2.sh 1 2 3 4
#Sample Output
#SUM IS 10
#COUNT IS 4
#Sample Input
#sh shift2.sh 1 2 3 4 5 6 7 8 9 10 11 12 13 14
#Sample Output
#SUM IS 105
#COUNT IS 14

 

9) function

1) The reason why we use function is to make shell script more human readable.

2) We can totally do not use function at all, but that will mess our script up

3) There is no local variable inside a function, all the variable in a function is global visiable, except special variables and position variables

4) The special variables and position variable inside a function is only local visiable, it is passed when we call this function. 

4) We can add param when we call a function

#An apache httpd example

case "$1" in
    start)
        start
        ;;
    stop)
        stop
        ;;
    restart)
        restart
        ;;
....

reload() {
    echo -n $"Reloading $prog:"
    if ...
    else ...
    fi
}

3) define function syntax

function_name(){
    ops..
}

4) use function syntax

function_name param1 param2 param3 ...

5) Sample

#!/bin/bash
#Demo for function usage

help(){
echo "Incorrect param number"
echo "Usage: sh function.sh \$1 \$2 \$3"
}

if [ $# -ne 3 ]
then
    help
else
    echo "Thank you for your correct input"
fi

#Sample Input
#sh function.sh 1
#Sample Output
#Incorrect param number
#Usage: sh function.sh \$1 \$2 \$3
#Sample Input
#sh function.sh 1 2 3
#Sample Output
#Thank you for your correct input
#!/bin/bash
#Demo for function usage

test(){
# $1 is only visiable in current function
# $# is only visiable in current function
echo "Param1=$1"
echo "Param number=$#"
}

test 1 2 3

#Sample Input
#sh function2.sh
#Sample Output
#Param1=1
#Param number=3
#/bin/bash
#Demo for function usage

test(){
echo "Param1=$1"
eccho "Param number=$#"
}

test $1 $2 $3

#Sample Input
#sh function3.sh
#Sample Output
#Param1=
#Param number=0

#Sample Input
#sh function3.sh 2 2 2
#Sample Output
#Param1=2
#Param number=3

#Sample Input
#sh function3.sh 2 2 2 2 2 2
#Sample Output
#Param1=2
#Param number=3

 

10) sh -n ***.sh --> Only check the syntax error

      sh -x ***.sh  --> Will show every step of script execution

 

11) If a user need to execute a script, the permissions required are as follows:

1) User have r permission for the script
2) User have rx permission for the directory that stores the script

# If the user don't have the permission of x for the script, he can still execute the script with syntax: sh script.sh ...
# If the user have the permission of x for the script, then he can execute the script with syntax: /.../script.sh ...

 

分享到:
评论

相关推荐

    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

    iii Table of Contents Printing n lines before or after a pattern in a file 172 Removing a sentence in a file containing a word 174 Implementing head, tail, and tac with awk 175 Text slicing and ...

    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