Shell Programming Quick Start

Source: Internet
Author: User

Shell programming: For Hadoop programmers, it is often necessary to familiarize yourself with shell programming, because the shell can easily run program code.

1. Shell file format: xxx.sh

#!/bin/sh---The first line of the shell file must be written like this
#井号后面的内容是注释
Echo ' ABCD '----echo is output, echo


mkdir shellfile
Span style= "font-size:14pt" >CD shellfile
ls
vi test.sh
#!/bin/sh
echo ' ABCD '
ls
ll
tset.sh---Now perform discovery without permission to modify permissions to execute
chmod u+x test.sh
ll    ---Listing file details
test.sh---can do it now. , the ABCD

is output

VI test.sh
#!/bin/sh
I=0---Variables do not need to be declared, and initialization does not require a specified type. Variable names can only consist of letters, numbers, underscores, and cannot begin with a number.
echo $i---Display variable values using: Echo $ variable Name

2. Variable classification: Temporary variables and environment variables (/etc/profile:export XXX)

3, single quotation marks, double quotation marks, the use of floating sign:
VI test.sh
#!/bin/sh
I=0
echo ${i}1234---Output 01234, if you want the variable to be used independently, open with curly braces

VI test.sh
#!/bin/sh
I=0
echo ' $i '---output $i, single quote not parsed variable
echo "$i"---output 0, double quote resolution variable

VI test.sh
#!/bin/sh
I=0
Echo ' $i '
echo "$i"
echo $ (date)---output time
echo ' Date '---float is the execution content, similar to the $ (XXX) above. Here ' data ' ==$ (date)

4, positional variables: When the script is executed, the passed in parameters are used in sequence, The value of the variable is referenced in the order.
vi test.sh
#!/ bin/sh
i=0
echo "First param:$1"
echo "second param:$2"
execute: test.shabc123
output: first param:abc
        second param:123

5, using Date: (see date command uses document ")
1, You can execute the date directly, outputting the current time of the system.
Br> 3, date +%s---format%s represents the number of seconds since 1970-01-01 00:00:00
4, date--date= ' 2017-4-10 '---Specify time output--date= ' 2009-01-01 11:11:11 '
5, specified time output--date= ' 3 days ago '

6, standard input, output, error:
1, standard input, output, error are command line, use file descriptor 0, 1, 2 reference
2, use redirection to convert information to another location
---------
ls > A.txt--- Converting test.sh to a.txt outputs the
ls 1>b.txt---a.txt and test.sh to b.txt output

7, using crontab (similar to a timer in Java):
1, scheduled tasks under Linux
2, edit using CRONTAB-E         
* * * * * date>>/root/a.txt
There are six columns, namely: time-of-day weekly command
(default is output by the first minute of each hour)
to follow the 5th minute output per hour, it needs to be modified to: 5 * * * * date>>/root/a.txt

3, View using crontab-l

8, if judgment, for loop: (note the spaces between the symbols!) There is a space at the end of each line!!!
if [...]---if and [between, [and conditions, conditions, and] there is a space between.
then
...
fi

for ((i=0;i<10;i++))---for and (there is a space between


done
----------
vi if.sh
#!/bin/sh
if [' a ' = ' a ']
then
echo ' equal '
fi
chmod u+x if.sh
if.sh---output equal

VI for.sh
#!/bin/sh
For ((i=0;i<10;i++))
Do
Echo $i
Done
chmod u+x for.sh
for.sh

9. Custom functions:
Function name () {
....
}

The last line statement is the default return value

When referencing a custom function file, use the source func.sh
------------------
VI func.sh
#!/bin/sh
function dat () {
Date +%y-%m-%d
}
Dat
chmod u+x func.sh
func.sh

VI func.sh
#!/bin/sh
function dat () {
Date--date= "Days Ago" +%y-%m-%d
}
DAT 2
func.sh---Output date 2 days before the current time
-----------------

Refer to func.sh in test.sh:
VI test.sh
#!/bin/sh
SOURCE func.sh---Best to specify a path
Dat-2
test.sh---Output 2 days after the date

Shell Programming Quick Start

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.