note of SHELL (二)

本文介绍了一系列实用的Shell脚本案例,包括文件内容修改、在线状态检查、颜色选择交互、数组操作、文件备份等,通过具体示例展示了如何使用Shell进行高效任务处理。

#------------------------------------------------------------
#name:call_file.sh
#note:call shell file
#!/bin/sh
if  [[ -f "test.sh" ]] ;then
        sh countdown.sh 5
        else
    echo -e "file is no exists/n"
        fi

#########################################
################ tmp ####################
#pear
#banana
#apple
#scriptname:chang_name
#purpose:chang file content which you do not like
#!/bin/sh
while read data
do
        echo -e "$data/n"
        echo -e "do you like this fruit(y/n)/n"
        read answer</dev/tty
         case $answer in
        Y|y)
            echo -e "ok ,skip this/n"
        #    exit 0
            ;;
        *)
            echo -e "input your favorite fruit/n"
            read i</dev/tty
            sed -e "s/$data/$i/g" tmp>tmp2
            mv tmp2 tmp
        esac
done<tmp

#----------------------------------------------
#name:check_online.sh
#note:test if eth0 connected
#!/bin/sh

if [ !  $UID -eq 0 ] ;then
        echo -e "Change to root account please!!!/n"
        exit 1
        fi
while [ 1 ] ; do
        ifconfig eth0 |grep up>/dev/null && echo -e "It is connected /n" && exit
        echo "You are log out/n"
        sleep 3
done

##################################################
#script:color.sh
#purpose:using regular expressions
#!/bin/sh
more $0
echo -e "choose a color from red blue green/n";
read color
case $color in
[bB]l*)
echo -e "the sky is $color/n";;
[gG]reen)
echo -e "the tree is $color/n";;
[rR]ed)
echo -e "$color is warm color/n";;
*)
echo -e "input error/n";;
esac

 

#-----------------------------------------------------
#
#
#!/bin/sh
declare -a name
name=(`cat "test.txt"`)
echo -e "the array /@name is ${name[*]}/n"
echo -e "this array has " ${#name[*]} "elements/n"

 

###############################################
#scriptname:backup.sh
#purpose:backup file to the specified directory
#!/bin/sh
for file in 1 2 3 4 ;do
        if [[ ! -f $file ]];then
                echo -e "$file is not exists!!!/n"
                fi
                cp $file old2
                echo -e "$file is backup success!!!/n"
    done

 

#######################################
#scriptname:cal.sh
#purpose:print a few numbers
#!/bin/sh
i=1
while [[ $i -le 10 ]] ;do
        echo -e "$i/n"
        ((i=i+1))
done

 

################################################
#scriptname:calculator.sh
#purpose:calculate two numbers ,this script
#        supports float point
#!/bin/sh

cat<<logo
***********************************************

  welcome to calculator programme

***********************************************
logo

echo -e "input the first number/n"
read num1

echo -e "input the operator/n"
select symbol in + - /* / ;do
case $symbol in
+)
ope="+"
echo -e "you choose +/n"
break;;
-)
ope="-"
echo -e "you choose -/n"
break;;
'*')
ope="*"
echo -e "you choose */n"
break;;
'/')
ope="/"
echo -e "you choose //n"
break;;
esac
done

echo -e "input the second number/n"
read num2

#((result=num1"$ope"num2))
result=$(echo $num1"$ope"$num2|bc )

echo -e  "the result is $result/n"

 

#-----------------------------------------------------
#script:find.sh
#purpose:find the specified file in specified path,list
#        the result
#!/bin/sh
path=$1
file=$2
argc=$#
if [[ ! argc -eq 2 ]] ;then
        echo "usage:$0 path_to_search file_to_search /n"
        exit 1
fi

find $path -name "$file" -print >test

if [[ -s test ]] ; then
   number=`cat test|wc -l`
   more test
    echo -e "find $number files/n";
else
        echo -e "this file is not exist/n" ;
fi

 

#----------------------------------------------------
#name:function.sh
#note:creating function in shell
#!/bin/sh
start()
{
        echo "--------------------------------"
        echo "game"
        echo "is "
        echo "start"
}

for ((a=0;a<5;a++ ));do
        start
        done
        exit 0

 

############################################
#
#
#!/bin/sh
while getopts qwe option
do
        if [ "$option" = q ] ;then
                echo -e "you type -q/n"
        elif [ "$option" = w ] ;then
                echo -e "you type -w/n"
        elif [ "$option" = e ] ;then
                echo -e "you type -e/n"
        elif [ "$option" = /? ] ;then
                echo -e "you do not type option/n" 1>&2
        fi
done

#-----------------------------------------------------
#script:getopts.sh
#purpose:using getopts function
#!/bin/sh
while getopts abc:f opt    #colon dose not take effect here
do
    case "$opt" in
        a) echo -e "option a input/n";;
        b)
        echo -e "option b input/n"
        ;;
        c)
        echo -e "option c input/n"
        ;;
        f)
        echo -e "option f input/n";;
        *)
        echo -e "error: invalid option/n"
        ;;

    esac
done

05-27
### Shell Scripting or Shell Commands Shell scripting is a fundamental aspect of Unix-like operating systems, including Linux and macOS. It involves writing scripts that automate repetitive tasks, manage system resources, and interact with other programs. The shell itself serves as both an interactive command interpreter and a scripting language[^1]. #### Special Meanings of Parentheses in Shell Scripts Parentheses `(` and `)` have specific meanings in shell scripting, particularly in bash. They are used for creating subshells, grouping commands, and defining arrays. For example: - **Subshells**: `(command1; command2)` creates a subshell where the commands inside the parentheses are executed in isolation from the main shell environment. - **Command Grouping**: `{ command1; command2; }` groups commands together but does not create a subshell. Note the semicolon after each command and the braces. - **Array Definitions**: `array=(value1 value2 value3)` defines an array named `array` with three elements. When using parentheses literally in commands like `tcpdump`, they must be escaped with a backslash (`\`) so the shell interprets them as literal characters rather than part of its syntax[^1]. ```bash tcpdump \( host 192.168.1.1 \) ``` #### External Programs in Shell Scripts Many useful commands within shell scripts are external Unix utilities rather than built-in shell commands. Some common external utilities include: - `tr`: Translating or squeezing characters. - `grep`: Searching files for lines that match patterns. - `expr`: Evaluating expressions. - `cut`: Removing sections from each line of files. Built-in commands such as `echo`, `test`, and `which` are often directly integrated into the shell, providing faster execution without invoking an external process[^2]. #### Identifying Directories and Executables The `ls` command can help identify directories and executable programs. By using the `-F` flag, `ls` appends a `/` to directory names and a `*` to executable program names. This feature aids in quickly distinguishing between different types of files when navigating or scripting in the shell[^3]. ```bash ls -F /path/to/directory ```
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值