Linux Shell编程进阶案例实战(三)

本文深入探讨Linux Shell编程,包括控制结构如if、while循环的用法,以及Shell函数的应用。通过实例解析了条件判断和循环操作,并展示了在大数据场景中的运用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Linux Shell编程进阶案例实战(三)
本期内容:
1 Linux Shell的控制结构实战
2 Linux Shell的函数
3 Linux Shell最佳实践
4 Linux Shell在大数据中的应用
if [ -z "${SPARK_HOME}" ]; then
  export SPARK_HOME="$(cd "`dirname "$0"`"/..; pwd)"
fi
TACHYON_STR=""
while (( "$#" )); do
case $1 in
    --with-tachyon)
      TACHYON_STR="--with-tachyon"
      ;;
  esac
shift
done
# Load the Spark configuration
. "${SPARK_HOME}/sbin/spark-config.sh"
# Start Master
"${SPARK_HOME}/sbin"/start-master.sh $TACHYON_STR
if条件语句,结束时候是fi ;while循环结束时done
一、条件判断
    1、使用if,基本语法:if ... then ... fi,注意if条件判断后面没有“;”,需要把then换行,如果if和then处于同样一行的话,则需要在then前面加上“;”,终止if条件使用fi。
创建if_test的脚本:
#!/bin/bash
echo "please type your content:"
read content
if [ "$content" -lt 10 ]  #<=10
then echo "The content you typed is smaller then 10"
fi
~    
然后执行如下(if的【】之间有空格):
root@Master:~# vim if_test.sh
root@Master:~# chmod u+x if_test.sh 
root@Master:~# ./if_test.sh 
please type your content:
100
./if_test.sh: line 5: [100: command not found
root@Master:~# vim if_test.sh
root@Master:~# ./if_test.sh 
please type your content:
100
root@Master:~# 
 2、复杂的if ... then ... else ... fi
在上述代码中添加else语句:
if [ "$content" -lt 10 ]  # -lt dengjia <=10
then echo "The content you typed is smaller then 10"
else echo "The content you taped is :"
fi
在spark-shell中的循环:
stty -icanon min 1 -echo > /dev/null 2>&1
export SPARK_SUBMIT_OPTS="$SPARK_SUBMIT_OPTS -Djline.terminal=unix"
"${SPARK_HOME}"/bin/spark-submit --class org.apache.spark.repl.Main --name "Spark shell" "$@"
stty icanon echo > /dev/null 2>&1
else
export SPARK_SUBMIT_OPTS
"${SPARK_HOME}"/bin/spark-submit --class org.apache.spark.repl.Main --name "Spark shell" "$@"
fi

二、循环
    1、循环的类型:for循环语句、while语句、until循环语句;
    2、循环的控制语句break,
实例解析:
#!/bin/bash
for item in 0 1 2 3 4 5 6 7 8 9
# for item in {0..100} equels up
do
        echo "The Current item is : $item"
done
执行结果(for 中列表可以表示:for item in {0..100}):
root@Master:~# vim Hellolinux.sh
root@Master:~# chmod u+x Hellolinux.sh 
root@Master:~# ./Hellolinux.sh 
The Current item is : 0
The Current item is : 1
The Current item is : 2
The Current item is : 3
The Current item is : 4
The Current item is : 5
The Current item is : 6
The Current item is : 7
The Current item is : 8
The Current item is : 9
root@Master:~# 
sum=0
for item in {1..100}
do
#       echo "The Current item is : $item"
        let "sum=item"
done
echo "1+2+3+...+100 = $sum"
添加变量sum,循环打印出1-100和:
root@Master:~# vim Hellolinux.sh
root@Master:~# ./Hellolinux.sh 
1+2+3+...+100=5050
root@Master:~# 
循环中有case:
#!/bin/bash
for (( i=0; i<10; i++)); do
echo $i
done
打印结果:
root@Master:~# vim case_test.sh
root@Master:~# chmod u+x case_test.sh 
root@Master:~# ./case_test.sh 
0
1
2
3
4
5
6
7
8
9
root@Master:~# 
说明:case表达式用于多分支选择语句,会
根据表达式的值来选择要执行的预缴,若遇到匹配的值,就执行该值后语句,
例如:
#!/bin/bash
echo "Please enter your numbers(1-0):"
read number
case "$number" in
1)
        echo "number: 1";;
2)
        echo "number: 2";;
3)
        echo "other number:$number:"
esac
执行结果如下:
root@Master:~# vim case_test2.sh
root@Master:~# chmod u+x case_test2.sh 
root@Master:~# ./case_test2.sh 
Please enter your numbers(1-0):
2
number: 2
root@Master:~# ./case_test2.sh 
Please enter your numbers(1-0):
5
root@Master:~# 


















Shell脚本高级编程教程,希望对你有所帮助。 Example 10-23. Using continue N in an actual task: 1 # Albert Reiner gives an example of how to use "continue N": 2 # --------------------------------------------------------- 3 4 # Suppose I have a large number of jobs that need to be run, with 5 #+ any data that is to be treated in files of a given name pattern in a 6 #+ directory. There are several machines that access this directory, and 7 #+ I want to distribute the work over these different boxen. Then I 8 #+ usually nohup something like the following on every box: 9 10 while true 11 do 12 for n in .iso.* 13 do 14 [ "$n" = ".iso.opts" ] && continue 15 beta=${n#.iso.} 16 [ -r .Iso.$beta ] && continue 17 [ -r .lock.$beta ] && sleep 10 && continue 18 lockfile -r0 .lock.$beta || continue 19 echo -n "$beta: " `date` 20 run-isotherm $beta 21 date 22 ls -alF .Iso.$beta 23 [ -r .Iso.$beta ] && rm -f .lock.$beta 24 continue 2 25 done 26 break 27 done 28 29 # The details, in particular the sleep N, are particular to my 30 #+ application, but the general pattern is: 31 32 while true 33 do 34 for job in {pattern} 35 do 36 {job already done or running} && continue 37 {mark job as running, do job, mark job as done} 38 continue 2 39 done 40 break # Or something like `sleep 600' to avoid termination. 41 done 42 43 # This way the script will stop only when there are no more jobs to do 44 #+ (including jobs that were added during runtime). Through the use 45 #+ of appropriate lockfiles it can be run on several machines 46 #+ concurrently without duplication of calculations [which run a couple 47 #+ of hours in my case, so I really want to avoid this]. Also, as search 48 #+ always starts again from the beginning, one can encode priorities in 49 #+ the file names. Of course, one could also do this without `continue 2', 50 #+ but then one would have to actually check whether or not some job 51 #+ was done (so that we should immediately look for the next job) or not 52 #+ (in which case we terminate or sleep for a long time before checking 53 #+ for a new job).
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值