Spark's way of cultivation (basic)--linux Big Data Development Basics: 12th: Introduction to Shell Programming (iv)

Source: Internet
Author: User
Tags shebang

The main contents of this section
    1. Shell Script debugging
    2. Shell functions
    3. Shell Control Structure Preliminary
1. Shell Script debugging

When a script goes wrong, you need to debug the script and learn that script debugging is an essential skill for every Linux system user. Shell script debugging does not require any additional tools, just need to add the-X option in front of the script file to create the debug.sh file with the following content:

#!/bin/bash#Filename: debug.shecho"scripting"echo"debuging"ls +

Script debugging using the Bash-x command

root@sparkslave02:~/ShellLearning/Chapter12# bash -x debug.sh + echo scriptingscripting+ echo debugingdebuging+ ls +ls: cannot access +:Noor directory

The-X option prints out each line of command and execution results from the shell script so that the developer can quickly navigate to the wrong script location. You can use Set-x if the code size is large or if the script developer knows the approximate location of the code error; Set +x; command for local debugging, such as

#!/bin/bash#Filename: debug2.shforin {1..6}doset -x//set -x表示跟在该命令后的脚本输出调试信息echo$i//set +x表示从此处禁用调试set +xdoneecho"Script executed"

The above code means that only the output echo $i is printed, and the specific debug information output is as follows:

[Email Protected]2:~/shelllearning/chapter12#./debug2.sh+Echo 11+Set+x+Echo 22+Set+x+Echo 33+Set+x+Echo 44+Set+x+Echo 55+Set+x+Echo 66+Set+x

In addition to script debugging with the Bash-x command, you can also add the-XV command to the first line of the script, allowing the script to debug by default, for example:

//加-xv选项,使脚本执行时会打印输出调试信息#!/bin/bash -xv#Filename: debug.shforin {1..6}doset -xecho$iset +xdoneecho"Script executed"~                       
2. Shell functions

Similarly, like C, C + + and other programming languages, the shell can define functions, the function is defined in the following format

function fname(){   shell脚本语句;}

Vim command to create a functiondemo.sh script file

root@sparkslave02:~/ShellLearning/Chapter12# vim functionDemo.sh#定义一个函数fnamefunction fname(){   #输出第一个参数   $1   #输出函数所在文件名   $0   #输出所有参数   [email protected]}#将函数中传入两个参数"arg1""args"

Execution Result:

root@sparkslave02:~/ShellLearning/Chapter12# ./functionDemo.sh arg1./functionDemo.sharg1 args
3. Shell Control Structure Preliminary

Like other programming languages, the shell has its own control structure, including for loops, while loops, until loops, if statements, and so on. This section first describes the use of a For loop, with a lot of usage for the For loop, and gives the four most commonly used for loop usages

(1) for I in $ (seq 10)
Root@sparkslave02: ~/shelllearning/chapter12# vim forloop.sh forIinch $(SeqTen) DoEcho$iDoneroot@sparkslave02: ~/shelllearning/chapter12# chmod a+x forloop.shRoot@sparkslave02: ~/shelllearning/chapter12#./forloop.sh123456789Ten
(2) for ((i=1;i<=10;i++))
for((i=2;i<=10;i++))doecho$idone
(3) for I in ls
root@sparkslave02:~/ShellLearning/Chapter12# vim forloop3.shforin `ls`do$idoneroot@sparkslave02:~/ShellLearning/Chapter12# chmod a+x forloop3.shroot@sparkslave02:~/ShellLearning/Chapter12# ./forloop3.sh debug2.shdebug.shforloop2.shforloop3.shforloop.shfunctionDemo.sh
(4) for I in ${arr[*]}
root@sparkslave02:~/ShellLearning/Chapter12# vim forloop4.sharr=(0123)forin${arr[*]}do${arr[i]}doneroot@sparkslave02:~/ShellLearning/Chapter12# ./forloop4.sh 0123

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Spark's path to cultivation (basic)--linux Big Data Development Basics: 12th: Getting Started with Shell programming (iv)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.