本期内容:
1 Linux Shell的控制结构实战 2 Linux Shell的函数 3 Linux Shell最佳实践 4 Linux Shell在大数据中的应用
if [ -z "${SPARK_HOME}" ]; thenif条件语句,结束时候是fi ;while循环结束时done一、条件判断 1、使用if,基本语法:if ... then ... fi,注意if条件判断后面没有“;”,需要把then换行,如果if和then处于同样一行的话,则需要在then前面加上“;”,终止if条件使用fi。
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_test的脚本:
#!/bin/bash然后执行如下(if的【】之间有空格):
echo "please type your content:"
read content
if [ "$content" -lt 10 ] #<=10
then echo "The content you typed is smaller then 10"
fi
~
root@Master:~# vim if_test.sh2、复杂的if ... then ... else ... fi在上述代码中添加else语句:
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:~#
if [ "$content" -lt 10 ] # -lt dengjia <=10在spark-shell中的循环:
then echo "The content you typed is smaller then 10"
else echo "The content you taped is :"
fi
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 中列表可以表示:for item in {0..100}):
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
root@Master:~# vim Hellolinux.sh添加变量sum,循环打印出1-100和:
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"
root@Master:~# vim Hellolinux.sh循环中有case:
root@Master:~# ./Hellolinux.sh
1+2+3+...+100=5050
root@Master:~#
#!/bin/bash
for
(( i=0;
i<10; i++));
do
echo $i
done
打印结果:root@Master:~# vim case_test.sh说明:case表达式用于多分支选择语句,会根据表达式的值来选择要执行的预缴,若遇到匹配的值,就执行该值后语句,例如:
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:~#
#!/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:~#