Spark修煉之道(基礎篇)——Linux大資料開發基礎:第十三節:Shell程式設計入門(五)
本節主要內容
while expression
do
command
command
done
(1)計數器格式
適用於迴圈次數已知或固定時
root@sparkslave02:~/ShellLearning/Chapter13# vim whileLoop.sh
.#!/bin/bash
i=1
while(($i<5))
do
echo $i
let i++
done
root@sparkslave02:~/ShellLearning/Chapter13# chmod a+x whileLoop.sh
root@sparkslave02:~/ShellLearning/Chapter13 # ./whileLoop.sh
1
2
3
4
(2)標誌符結束while迴圈
[email protected]:~/ShellLearning/Chapter13# vim flagWhileLoop.sh
#!/bin/bash
echo "Please input the num (1~~10): "
#接受使用者輸入
read num
while [[ $num != 4 ]]
do
#if語句,後面詳細介紹,這裡判斷是否小於4
if [ $num -lt 4 ]
then
echo "Too small ,Try again.."
read num
#判斷是否大於4
elif [ $num -gt 4 ]
then
echo "Too big ,Try again.. "
read num
else
exit 0
fi
done
echo "Yes ,you are right !!"
[email protected]:~/ShellLearning/Chapter13# chmod a+x flagWhileLoop.sh [email protected]:~/ShellLearning/Chapter13# ./flagWhileLoop.sh
Please input the num (1~~10):
4
Yes ,you are right !!
if 條件
then
Command
else
Command
#if條件判斷的結束,用反拼表示
fi
最常用的判斷為:判斷字串、判斷數字、判斷檔案及邏輯判斷等
(1)判斷字串
1.if [ str1=str2 ];then fi ----當兩個字串相同時返回真
2.if [ str1!=str2 ];then fi ----當兩個字串不相等時返回真
3.if [ -n str1 ];then fi ----當字串的長度大於0時返回真 (判斷變數是否有值)
4.if [ -z str1 ];then fi ----當字串的長度為0時返回真
root@sparkslave02:~/ShellLearning/Chapter13# vim if01.sh
str1="hello"
str2="hell"
#判斷兩字串是否相等
if [ str1=str2 ]
then
echo "equal"
fi
#判斷兩字串是否不等
if [ str1!=str2 ]
then
echo "not equal"
fi
#判斷字串長度是否大於0
if [ -n str1 ]
then
echo "the length of str1 is not zero"
fi
#判斷字串長度是否等於0
if [ -z str1 ]
then
echo "the length of str1 is not zero, it can't be executed"
fi
root@sparkslave02:~/ShellLearning/Chapter13# ./if01.sh
not equal
the length of str1 is not zero
(2)判斷數字
1.int1 -eq int2 --相等
2.int1 -ne int2 --不相等
3.int1 -gt int2 --大於
4.int1 -ge int2 --大於等於
5.int1 -lt int2 --小於
6.int1 -le int2 --小於等於
使用示例:
root@sparkslave02:~/ShellLearning/Chapter13# vim if02.sh
i=2
j=3
if [ $i -lt $j ]
then
echo "i is less than j"
fi
if [ $j -gt $i ]
then
echo "j is great than i"
fi
root@sparkslave02:~/ShellLearning/Chapter13# chmod a+x if02.sh
root@sparkslave02:~/ShellLearning/Chapter13# ./if02.sh
i is less than j
j is great than i
(3)判斷檔案
檔案判斷常用命令如下:
1. -r file --使用者可讀為真
2. -w file --使用者可寫為真
3. -x file --使用者可執行為真
4. -f file --檔案存在且為正規檔案為真
5. -d file --如果是存在目錄為真
6. -c file --檔案存在且為字元裝置檔案
7. -b file --檔案存在且為塊裝置檔案
8. -s file --檔案大小為非0為真,可以判斷檔案是否為空
9. -e file --如果檔案存在為真
使用示例:
root@sparkslave02:~/ShellLearning/Chapter13# vim if03.sh
root@sparkslave02:~/ShellLearning/Chapter13# chmod a+x if03.sh
#判斷檔案是否存在
if [ -f if03.sh ]
then
echo "if03.sh exists!!"
fi
#判斷目錄是否存在
if [ -d ../Chapter13 ]
then
echo "directory Chapter13 exists!!"
fi
root@sparkslave02:~/ShellLearning/Chapter13# ./if03.sh
if03.sh exists!!
directory Chapter13 exists!!
(4)邏輯判斷
邏輯判斷主要有下面三個命令
1. -a --與
2. -o --或
3. ! --非
使用示例:
root@sparkslave02:~/ShellLearning/Chapter13# vim if04.sh
#判斷if04.sh檔案與目錄Chapter13是否同時存在,同時存在則為真
if [ -f if04.sh -a -d ../Chapter13 ]
then
echo "file if04.sh and directory Chapter13 both exists!!!"
fi
root@sparkslave02:~/ShellLearning/Chapter13# chmod a+x if04.sh
root@sparkslave02:~/ShellLearning/Chapter13# ./if04.sh
file if04.sh and directory Chapter13 both exists!!!
(5)if [] then else fi的用法
前面給出的例子都是if [] then fi的形式,這裡再給出if [] then else fi的用法
root@sparkslave02:~/ShellLearning/Chapter13# vim if05.sh
i=7
if [ $i -lt 6 ]
then
echo "i is less than 6"
else
echo "i is great than or equal 6"
fi
root@sparkslave02:~/ShellLearning/Chapter13# chmod a+x if05.sh
root@sparkslave02:~/ShellLearning/Chapter13# ./if05.sh
i is great than or equal 6
(6)if [] then elif then else fi的用法
多種判斷,看示例程式碼就能明白:
root@sparkslave02:~/ShellLearning/Chapter13# cp if05.sh if06.sh
root@sparkslave02:~/ShellLearning/Chapter13# vim if06.sh
i=7
if [ $i -le 6 ]
then
echo "i is less than 6"
elif [ $i -eq 7 ]
then
echo "i equal 7"
else
echo "i is great than 7"
fi
root@sparkslave02:~/ShellLearning/Chapter13# ./if06.sh
i equal 7
## 3. until迴圈控制結構##
語法格式:
until condition
do
command
done
使用示例:
root@sparkslave02:~/ShellLearning/Chapter13# vim until01.sh
i=0
until [ $i -gt 2 ]
do
let i+=1
echo "i=$i"
done
root@sparkslave02:~/ShellLearning/Chapter13# chmod a+x until01.sh
root@sparkslave02:~/ShellLearning/Chapter13# ./until01.sh
i=1
i=2
i=3
相關推薦
Spark修煉之道(基礎篇)——Linux大資料開發基礎:第十三節:Shell程式設計入門(五)
本節主要內容 while expression do command command done (1)計數器格式 適用於迴圈次數已知或固定時 root@sparkslave02:~/ShellLearning/Chapter13# vim w
Spark修煉之道(進階篇)——Spark入門到精通:第一節 Spark 1.5.0叢集搭建
作者:周志湖 網名:搖擺少年夢 微訊號:zhouzhihubeyond 本節主要內容 作業系統環境準備 Hadoop 2.4.1叢集搭建 Spark 1.5.0 叢集部署 注:在利用CentOS 6.5作業系統安裝spark 1.5叢集過程中,
Spark修煉之道(進階篇)——Spark入門到精通:第十四節 Spark Streaming 快取、Checkpoint機制
作者:周志湖 微訊號:zhouzhihubeyond 主要內容 Spark Stream 快取 Checkpoint 案例 1. Spark Stream 快取 通過前面一系列的課程介紹,我們知道DStream是由一系列的RDD構成的,
Spark修煉之道(進階篇)——Spark入門到精通:第十六節 Spark Streaming與Kafka
作者:周志湖 主要內容 Spark Streaming與Kafka版的WordCount示例(一) Spark Streaming與Kafka版的WordCount示例(二) 1. Spark Streaming與Kafka版本的WordCount示例
Spark修煉之道(進階篇)——Spark入門到精通:第十節 Spark SQL案例實戰(一)
作者:周志湖 放假了,終於能抽出時間更新部落格了……. 1. 獲取資料 本文通過將github上的Spark專案git日誌作為資料,對SparkSQL的內容進行詳細介紹 資料獲取命令如下: [[email protected] spa
Spark修煉之道(高階篇)——Spark原始碼閱讀:第十三節 Spark SQL之SQLContext(一)
作者:周志湖 1. SQLContext的建立 SQLContext是Spark SQL進行結構化資料處理的入口,可以通過它進行DataFrame的建立及SQL的執行,其建立方式如下: //sc為SparkContext val sqlContext
Spark修煉之道(進階篇)——Spark入門到精通:第十三節 Spark Streaming—— Spark SQL、DataFrame與Spark Streaming
主要內容 Spark SQL、DataFrame與Spark Streaming 1. Spark SQL、DataFrame與Spark Streaming import org.apache.spark.SparkConf import org
Spark修煉之道(進階篇)——Spark入門到精通:第十五節 Kafka 0.8.2.1 叢集搭建
作者:周志湖 微訊號:zhouzhihubeyond 本節為下一節Kafka與Spark Streaming做鋪墊 主要內容 1.kafka 叢集搭建 1. kafka 叢集搭建 kafka 安裝與配置 tar -zxvf kafka_2
Spark修煉之道(高階篇)——Spark原始碼閱讀:第十二節 Spark SQL 處理流程分析
作者:周志湖 下面的程式碼演示了通過Case Class進行表Schema定義的例子: // sc is an existing SparkContext. val sqlContext = new org.apache.spark.sql.SQLConte
Spark修煉之道(進階篇)——Spark入門到精通:第九節 Spark SQL執行流程解析
1.整體執行流程 使用下列程式碼對SparkSQL流程進行分析,讓大家明白LogicalPlan的幾種狀態,理解SparkSQL整體執行流程 // sc is an existing SparkContext. val sqlContext = new or
Spark修煉之道(進階篇)——Spark入門到精通:第六節 Spark程式設計模型(三)
作者:周志湖 網名:搖擺少年夢 微訊號:zhouzhihubeyond 本節主要內容 RDD transformation(續) RDD actions 1. RDD transformation(續) (1)repartitionAnd
Spark修煉之道(進階篇)——Spark入門到精通:第十節 Spark Streaming(一)
本節主要內容 Spark流式計算簡介 Spark Streaming相關核心類 入門案例 1. Spark流式計算簡介 Hadoop的MapReduce及Spark SQL等只能進行離線計算,無法滿足實時性要求較高的業務需求,例如實時推薦、實時
Spark修煉之道(高階篇)——Spark原始碼閱讀:第八節 Task執行
Task執行 在上一節中,我們提到在Driver端CoarseGrainedSchedulerBackend中的launchTasks方法向Worker節點中的Executor傳送啟動任務命令,該命令的接收者是CoarseGrainedExecutorBack
Spark修煉之道(高階篇)——Spark原始碼閱讀:第一節 Spark應用程式提交流程
作者:搖擺少年夢 微訊號: zhouzhihubeyond spark-submit 指令碼應用程式提交流程 在執行Spar應用程式時,會將spark應用程式打包後使用spark-submit指令碼提交到Spark中執行,執行提交命令如下: root@s
Android查缺補漏(IPC篇)-- 進程間通訊基礎知識熱身
內部 eat ack 學習過程 and ... 綁定 his nec 本文作者:CodingBlock 文章鏈接:http://www.cnblogs.com/codingblock/p/8479282.html 在Android中進程間通信是比較難的一部分,同時又非常重要
機器學習實戰(第二篇)-k-近鄰演算法開發手寫識別系統
上一篇文章中,我們學習了使用k近鄰演算法改進約會網站,實現了通過一些資料的輸入判斷人員屬於哪一個分類。但是上篇文章基於的資料都是我們能夠簡單理解的數字資訊,本篇文章我們在人不太容易看懂的資料上使用分類器。這篇文章中我們將一步步構造使用k-近鄰分類器的手寫識別系統。為了
Agile敏捷開發管理Salesforce專案(第一篇)- 4大核心價值觀+12條原則
【什麼是敏捷開發?】資深程式設計師之路(5)--agile開發敏捷開發(scrum, agile)相對於瀑布流開發(waterfull)更適合現在快節奏的商業模式需求,它將一整個專案拆分為相互獨立的小塊,我們成為sprint(衝刺),每個sprint都包含前期的需求分析,開發
第十三節:pandas之groupby()分組
info ima size afr 技術分享 .com color panda -s 1、Series()對象分組 1.1、單級索引 1.2、多級索引 2、DataFrame()對象分組 第十三節:pandas之groupby()分組
shell程式設計入門(bash)
一、背景知識 (1)什麼是shell? 高大上的解釋,往往讓人摸不住頭腦。一句話概括就是:shell程式設計就是對一堆Linux命令的邏輯化處理。 (2)為什麼需要shell? 譬如我們要在linux下建立一個檔案a.c,可以touch a.c 但是如果我現在是用在linux下建立1
爾雅 科學通史(吳國盛) 個人筆記及課後習題 2018 第十章 20世紀的科學技術變革
第十章 20世紀的科學技術變革 19世紀被譽為人類歷史上第一個科學的世紀 20世紀的兩次世界大戰,改變了世界的格局 10.1 理論科學的變革 20世紀最重要的四大理論模型 宇宙學中的大爆炸宇宙模型