当前位置:   article > 正文

SHELL入门学习

SHELL入门学习

SHELL 入门学习

shell 变量

vim shellViable.sh

#! /bin/bash
# shell demo
string="abcd"
echo ${#string}
echo $string

echo "Sheel "
echo "$0"
echo "$1"
echo "$2"
echo "$3"

val=`expr 2 + 2`
echo "number sum:$val"
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
[root@alliyun cainiaoSheelScript]# chmod +x  shellViable.sh
[root@alliyun cainiaoSheelScript]# ./shellViable.sh 
4
abcd
Sheel 
./shellViable.sh



number sum:4

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

shell echo

vim shellEcho.sh

#! /bin/bash
# echo 
echo "It is a test"
echo It is a test

read name
echo $name It is a test

echo It is a test >myfile
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
[root@alliyun cainiaoSheelScript]# chmod +x shellEcho.sh
[root@alliyun cainiaoSheelScript]# ./shellEcho.sh 
It is a test
It is a test
look
look It is a test

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

shell printf

vim shellPrintf.sh

#! /bin/bash
# printf
echo Hello,sheel
printf Hello,sheel "\n"

printf "%-10s %-8s %-4s \n"
printf "%-10s %-8s %-4.2f \n"
printf "%-10s %-8s %-4.2f \n"
printf "%-10s %-8s %-4.2f \n"
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
[root@alliyun cainiaoSheelScript]# chmod +x shellPrintf.sh 
[root@alliyun cainiaoSheelScript]# ./shellPrintf.sh 
Hello,sheel
Hello,sheel                         
                    0.00 
                    0.00 
                    0.00 

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

shell test

vim shellTest.sh

#! /bin/bash
# sheel test
num1=100
num2=101
if test $num1 -eq $num2
then
        echo true
else
        echo false
fi

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
[root@alliyun cainiaoSheelScript]# chmod +x shellTest.sh
[root@alliyun cainiaoSheelScript]# ./shellTest.sh 
false
  • 1
  • 2
  • 3

shell if then

vim shellIfThen.sh

#! /bin/bash
# if then else then

num1=100
num2=200

if [ $num1 -eq 100 ]
then
        echo num1 is 100
else
        echo num1 is not 100
fi

if  [ $num2 -eq 100 ]
then
        echo num2 is 100
elif [ $num2 -eq 200 ]
then
        echo num2 is 200
else
        echo num2 is not 200
fi
~        
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
[root@alliyun cainiaoSheelScript]# chmod +x shellIfThen.sh 
[root@alliyun cainiaoSheelScript]# ./shellIfThen.sh 
num1 is 100
num2 is 200

  • 1
  • 2
  • 3
  • 4
  • 5

shell While

vim shellWhile.sh

#! /bin/bash
# sheel while

int=1
while (($int<=5))
do
        echo $int
        let int++
done

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
[root@alliyun cainiaoSheelScript]# chmod +x shellWhile.sh 
[root@alliyun cainiaoSheelScript]# ./shellWhile.sh 
1
2
3
4
5

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

shell function

vim shellFunction.sh

#! /bin/bash
# function
domoFun(){
        echo this is my first sheel fun!
}

echo fun start..
domoFun
echo fun end...

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
[root@alliyun cainiaoSheelScript]# chmod +x shellFunction.sh 
[root@alliyun cainiaoSheelScript]# ./shellFunction.sh 
fun start..
this is my first sheel fun!
fun end...

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
本文内容由网友自发贡献,转载请注明出处:https://www.wpsshop.cn/w/盐析白兔/article/detail/676174
推荐阅读
相关标签
  

闽ICP备14008679号