Linux Shell Program Tutorial(1)

今天在bilibili上发现了一个宝藏博主:https://space.bilibili.com/37817800?spm_id_from=333.788.b_765f7570696e666f.1

有很多可以学习的视频,后悔没早点发现,不过现在学起来也不晚。目前希望跟着把Linux Shell以及难啃的OpenFOAM看起来,梳理一下也是好的。OpenFOAM这个视频竟然是创始人上课,感动。。。

虽然跟着视频做笔记学习有点慢,但是感觉还是边听边操作更加有用,否则我这七秒的记忆、、、

下面是跟着视频梳理的内容:

1. Using variables and comments

*There are system and user-defined variables;

*Variables' name cannot start with a number;

#!/bin/bash
# this is a comment
echo "Hello World" # this is also a comment


#these are system variables
echo $BASH
echo $BASH_VERSION
echo $HOME
echo $PWD

#this is a user-defined variable

name=Aegean

echo The name is $name

2.Read user input

#!/bin/bash

echo "Enter name : "
read name
echo "Entered name : $name"


echo "Enter names : "
read name1 name2 name3 #more than one string to read
echo "Names : $name1 , $name2 , $name3"


read -p 'username : ' user_var #same line to print
read -sp 'password : ' pass_var #-s silent
echo #like line break
echo "username : $user_var"
echo "password : $pass_var"

echo "Enter names : "
read -a names
echo "Names : ${names[0]} , ${names[1]}"

echo "Enter name : "
read
echo "Name : $REPLY"

3. Pass arguments to a bash-script

#!/bin/bash

echo $0 $1 $2 $3 ' > echo $1 $2 $3'

args=("$@")
echo  ${args[0]}  ${args[1]}  ${args[2]} 

echo $@ #print all arguments

echo $# #print the number of all arguments

4. if statement(if then, if then else, if elif else)

#!/bin/bash

word=a

if [ $word == "b" ]
then
    echo "condition a is true"
elif [ $word != "b" ]
then
    echo "condition b is true "

#else
 #   echo "condition is false"
fi

5. File test operators

#!/bin/bash

echo -e "Enter the name of the file : \c"
read file_name

if [ -e $file_name ]
then
    echo "$file_name found"
else
    echo "$file_name not found"
fi

#!/bin/bash

echo -e "Enter the name of the file : \c"
read file_name

if [ -f $file_name ]
then
    echo "$file_name found"
else
    echo "$file_name not found"
fi

if [ -s $file_name ]
then
    echo "$file_name not empty"
else
    echo "$file_name empty"
fi

#other flags: -d -f -e

6. How to append output to the end of text file

#!/bin/bash

echo -e "Enter the name of the file : \c"
read file_name

if [ -f $file_name ]
then
    if [ -w $file_name ]
    then
        echo "Type some text data. To quit press ctrl+d."
        cat >> $file_name
    else
        echo "The file do not have write permissions"
    fi
else
    echo "$file_name not exists"
fi

7. Perform arithmetic operation

#!/bin/bash
num1=20
num2=5

echo $(( num1 + num2 ))
echo $(( num1 - num2 ))
echo $(( num1 * num2 ))
echo $(( num1 / num2 ))
echo $(( num1 % num2 ))

echo $(expr $num1 + $num2 )
echo $(expr $num1 - $num2 )
echo $(expr $num1 \* $num2 )
 #here is a “\” to ESC(escape code)
echo $(expr $num1 / $num2 )
echo $(expr $num1 % $num2 )

8. Floating point math operations in bash_bc command

#!/bin/bash
num1=20.5
num2=5

echo "20.5 + 5" | bc
echo "20.5 - 5" | bc
echo "20.5 * 5" | bc
echo "scale=2;20.5 / 5" | bc
  #scale controls the precision
echo "20.5 % 5" | bc

echo "$num1+$num2" | bc
echo "$num1-$num2" | bc

num=27

echo "scale=2;sqrt($num)" | bc -l #need to include libarary “sqrt” “^”
echo "scale=2;3^3" | bc -l

9. The case statement

#!/bin/bash

vehicle=$1

case $vehicle in
    "car")
        echo "Rent of $vehicle is 100 dollar" ;;
    "van")
        echo "Rent of $vehicle is 80 dollar" ;;
    "bicycle")
        echo "Rent of $vehicle is 5 dollar" ;;
    "truck")
        echo "Rent of $vehicle is 150 dollar" ;;
    * )
        echo "Unknown vehicle" ;;

esac

/*今日标签*/

四分之三的雨,

四分之一的我和你。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值