【shell】基本用法学习1

shell的基本用法学习

  • $a  传递变量a
  • ``
  • #

目录

shell运行指定目录下的脚本运行,找目录

shell传递参数

shell打印日期

shell定义变量

shell数组

shell等式

打印日期

shell字符串写入新文件

shell #


shell运行指定目录下的脚本运行,找目录

demo.sh如下

#!/bin/bash

#read -p "please enter a number: " num

#if [ $num -gt 10 ]; then
#    echo "num > 10"
#else
#    echo "num <= 10"
#fi

#echo deploy.sh exists

#if [ -n $var ]
#then
#    echo "var is $var"
#fi 

BASEDIR=`dirname $0`/..  # 获取当前脚本的相对路径 ./..
echo $BASEDIR   # ./..
BASEDIR=$(readlink -f `(cd "$BASEDIR"; pwd)`) # 打印当前脚本所在的上级目录
echo $BASEDIR

echo "****************"
echo $BASEDIR     # /media/cfs/xxx/Home/
echo `pwd`    # /home/xxx/Home/shell_study


echo ***#####@@@@@@@
# cd `dirname $0`;pwd  先cd到当前路径然后pwd,打印成绝对路径
echo `(cd "$BASEDIR"; pwd)`   # /media/cfs/xxx/Home

#echo killing $BASEDIR

demo.sh 的路径 /home/xxxx/Home/shell_study/demo.sh 

$ sh demo
./..
/media/cfs/xxx/Home
****************
/media/cfs/xxx/Home
/home/xxx/Home/demo
***#####@@@@@@@
/media/cfs/xxx/Home

再来体会一个,

a.sh 内容如下,

#!/bin/bash
#BASEDIR=`dirname $0`/..
#BASEDIR=$(readlink -f `(cd "$BASEDIR"; pwd)`)

BASEDIR=/opt/app/

echo BASEDIR is "$BASEDIR"
FEATURE=mq_server

pid_num=`ps -ef|grep "$BASEDIR$FEATURE"|grep -v grep|wc -l`
echo dir is "$BASEDIR$FEATURE"
echo pid_num is "$pid_num"

if [ $pid_num -lt 1 ]
        then
                echo "no program killed."
        else
            for pid in `ps -ef|grep "$BASEDIR $FEATURE"|grep -v grep|awk '{print $2}'`
                do
                        kill -15 $pid
                        echo "$pid stopped."
                done
                sleep 1
                echo "program stopped."
fi

运行结果,

$ sh a.sh
BASEDIR is /opt/app/
dir is /opt/app/mq_server
pid_num is 0
no program killed.

shell传递参数

shell_传递参数 脚本内容如下,

echo "-- \$* 演示 ---"
for i in "$*"; do
    echo $i
done

echo "-- \$@ 演示 ---"
for i in "$@"; do
    echo $i
done

运行结果,

$ sh shell_传递参数 8 9 10 11
-- $* 演示 ---
8 9 10 11
-- $@ 演示 ---
8
9
10
11

*是横着输出,@是换行输出。

shell打印日期

demo.sh

#!/bash/bin
xxxx
date

输出结果,

$ sh demo.sh
demo.sh: line 2: xxxx: command not found
2022年 12月 07日 星期三 22:16:38 CST

shell定义变量

demo.sh

name="apple"
echo $name
echo ${name}

运行结果

$ sh shell_review
apple
apple

runoob.sh

your_name='runoob'
str="Hello, I know you are \"$your_name\"! \n"
echo -e $str

your_name="runoob"
# 使用双引号拼接
greeting="hello, "$your_name" !"
greeting_1="hello, ${your_name} !"
echo $greeting  $greeting_1
# 使用单引号拼接
greeting_2='hello, '$your_name' !'
greeting_3='hello, ${your_name} !'
echo $greeting_2  $greeting_3

运行结果

$ sh runobb.sh
Hello, I know you are "runoob"!

hello, runoob ! hello, runoob !
hello, runoob ! hello, ${your_name} !

shell数组

my_array=(A B "C" D)

echo "第一个元素为: ${my_array[0]}"
echo "第二个元素为: ${my_array[1]}"
echo "第三个元素为: ${my_array[2]}"
echo "第四个元素为: ${my_array[3]}"

echo "数组的元素为: ${my_array[*]}"
echo "数组的元素为: ${my_array[@]}"

echo "获取数组的长度:"
echo "数组元素个数为: ${#my_array[*]}"
echo "数组元素个数为: ${#my_array[@]}"

运行结果

$ sh shell_review
第一个元素为: A
第二个元素为: B
第三个元素为: C
第四个元素为: D
数组的元素为: A B C D
数组的元素为: A B C D
获取数组的长度:
数组元素个数为: 4
数组元素个数为: 4

shell等式

a=10
b=20

val=`expr $a + $b`
echo "a + b : $val"

val=`expr $a - $b`
echo "a - b : $val"

val=`expr $a \* $b`
echo "a * b : $val"

val=`expr $b / $a`
echo "b / a : $val"

val=`expr $b % $a`
echo "b % a : $val"

if [ $a == $b ]
then
   echo "a 等于 b"
fi
if [ $a != $b ]
then
   echo "a 不等于 b"
fi

运行结果

a + b : 30
a - b : -10
a * b : 200
b / a : 2
b % a : 0
a 不等于 b
a="abc"
b="efg"

if [ $a = $b ]
then
   echo "$a = $b : a 等于 b"
else
   echo "$a = $b: a 不等于 b"
fi
if [ $a != $b ]
then
   echo "$a != $b : a 不等于 b"
else
   echo "$a != $b: a 等于 b"
fi
if [ -z $a ]
then
   echo "-z $a : 字符串长度为 0"
else
   echo "-z $a : 字符串长度不为 0"
fi
if [ -n "$a" ]
then
   echo "-n $a : 字符串长度不为 0"
else
   echo "-n $a : 字符串长度为 0"
fi
if [ $a ]
then
   echo "$a : 字符串不为空"
else
   echo "$a : 字符串为空"
fi

 运行结果

abc = efg: a 不等于 b
abc != efg : a 不等于 b
-z abc : 字符串长度不为 0
-n abc : 字符串长度不为 0
abc : 字符串不为空

打印日期

echo `date`

# 2022年 12月 07日 星期三 22:16:06 CST

shell字符串写入新文件

#!/bin/sh
#read name 
#echo "$name It is a test"

echo "OK! "\\"n"
echo "It is a test"

echo "It is a test" > myfile # 将It is a test写入 myfile文件中

echo '$name\"'

运行结果

OK! \n
It is a test
$name\"

shell #

string="abcd"
echo ${#string} #输出 4

string="runoob is a great site"
echo `expr index "$string" io`
$ sh shell_review
4
4

你可能感兴趣的:(Linux,bash)