1, Shell script entry

1 . Script format

Script #! / Bin / bash at the beginning (designated analytical unit )

2 . The first Shell Script : helloworld

( 1 ) Demand: Creating a Shell script, output helloworld

( 2 ) Case practical operation:

[atguigu@hadoop101 datas]$ touch helloworld.sh

[Atguigu @ hadoop101 datas] $ vi helloworld.sh

 

In helloworld.sh enter the following in

#!/bin/bash

echo "helloworld"

( 3 ) common implementation of the script

Di Yi species: using bash or sh + script of a relative or absolute path (not given script + x permission)

sh + phase script on path

[atguigu@hadoop101 datas]$ sh helloworld.sh

Helloworld

sh + script absolute path

[atguigu@hadoop101 datas]$ sh /home/atguigu/datas/helloworld.sh

helloworld

bash + scripting relative path

[atguigu@hadoop101 datas]$ bash helloworld.sh

Helloworld

bash + scripting the absolute path

[atguigu@hadoop101 datas]$ bash /home/atguigu/datas/helloworld.sh

Helloworld

First dicarboxylic kinds: using the input script absolute or relative path to execute the script ( must have executable rights + X )

( A ) First, we want to give helloworld.sh script + x permissions

[atguigu@hadoop101 datas]$ chmod 777 helloworld.sh

( B ) execution of the script

Relative path

[atguigu@hadoop101 datas]$ ./helloworld.sh

Helloworld

Absolute path

[atguigu@hadoop101 datas]$ /home/atguigu/datas/helloworld.sh

Helloworld

Note: The first execution method, nature is bash parser to help you execute the script, so the script itself does not need execute permissions. Di two kinds execution method, essentially a script needs its own execution , the order requires execute permissions.

3. Di Er Ge Shell Scripting : Multi-command processing

( 1 ) Demand :

In / home / atguigu / directory created under a banzhang.txt, banzhang.txt file in an increase in the "I love cls".

( 2 ) Case practical operation:

[atguigu@hadoop101 datas]$ touch batch.sh

[Atguigu @ hadoop101 datas] $ vi batch.sh

 

In batch.sh enter the following in

#!/bin/bash

 

cd /home/atguigu

touch cls.txt

echo "I love cls" >>cls.txt

Guess you like

Origin www.cnblogs.com/liuzhonghui/p/12001319.html