Linux基础

Posted 还是牛

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Linux基础相关的知识,希望对你有一定的参考价值。

Linux基础(六)

shell脚本中的三大循环和函数知识点

一、流程控制之if结构

1.简单的if实例:

#!/bin/bash
var=\'/etc/init.d\'
#var=\'/dev/sda\'
if [ -d $var ]
    then
        echo "$var is directory"
elif [ -b $var ]
    then
        echo "$var is block"
elif [ -f $var ]
    then
        echo "$var is regular file"
else
        echo \'unknow\'
fi
View Code

执行效果:

2.向脚本传递参数

eg:

#3.sh
echo $0
echo $1
echo $2
echo $3
echo ${11}
echo \'$$\' $$
echo \'$*\' $*
echo \'$@\' $@
echo \'$#\' $#
echo \'$?\' $?
View Code

测试结果:

[root@localhost ~]# ./3.sh 1 2 3 4 5 6 7 8 9 10
./3.sh
1
2
3

$$ 8755
$* 1 2 3 4 5 6 7 8 9 10
$@ 1 2 3 4 5 6 7 8 9 10
$# 10
$? 0
View Code

二.循环结构之while

while (条件)

do
动作
done

需要无限循环时我们会选择while :
写个脚本 让用户输入,输入数字通过,输出错误重新输入

#!/bin/bash
login=0
while [ $login != 1 ]
do
    read -p \'please input your name: \' name
    read -p \'please input your password: \' pwd
    if [ $name == \'egon\' ] && [ $pwd == \'123\' ]
        then
            echo \'login sucessful\'
            login=1
    fi
done
View Code

运行及查看结果:

[root@localhost ~]# ./4.sh
please input your name: egon
please input your password: 123
login sucessful
[root@localhost gandong]# 
View Code

三、循环结构之for

shell格式的for

for i in {1..10}
do
echo $i
done
View Code
[root@localhost ~]# ./5.sh 
1
2
3
4
5
6
7
8
9
10
View Code

应用循环和条件判断写的一个实例:

vim 6.sh

while :
do

    read -p \'please input your name: \' name
    read -p \'please input your password: \' psd
    if [-z $name ] || [-z $psd ]
        then
            continue
    fi
    if [ $name = \'alex\'  -a $psd = \'alex3714\' ]
        then
            echo \'login successful ,welcome da SB\'
            while :
            do
                read -p \'please input your cmd: \' cmd
                if [ $cmd = \'quit\' ]
                    then
                        break
                fi
                $cmd
            done
    else      
        echo \'username or password is error\'
    fi
done
    
echo \'========================================================\'
    
~                                                   
View Code

四、函数知识点

交互shell中的函数

eg:

vim 7.sh
function abc() {
 echo \'aaa\';echo \'bbbb\';
 }
abc
View Code

运行结果:

[root@localhost ~]# vim 7.sh
[root@localhost ~]# ./7.sh
aaa
bbbb
View Code

 

以上是关于Linux基础的主要内容,如果未能解决你的问题,请参考以下文章

20155307刘浩《网络对抗》逆向及Bof基础

20155311高梓云《网络对抗》逆向及Bof基础

20145301赵嘉鑫《网络对抗》逆向及Bof基础

[vscode]--HTML代码片段(基础版,reactvuejquery)

Linux bash基础特性二

[linux][c/c++]代码片段01