Linux shell編程入門

Linux shell編程入門

 

 

Linux shell編程入門

 

 

Linux shell編程入門

 

 

Linux shell編程入門

 

 

Linux shell編程入門

 

 

Linux shell編程入門

 

 

 

 

Linux shell編程入門

 

 

Linux shell編程入門

 

 

Linux shell編程入門

 

 

Linux shell編程入門

 

 

Linux shell編程入門

 

 

Linux shell編程入門

 

#!/bin/bashhtml

echo "前進程號:"$$java

echo "start"mysql

sleep 10linux

kill  $$git

sleep 900000000redis

echo "end"sql

 

 

Linux shell編程入門

 

 

Linux shell編程入門

 

 

Linux shell編程入門

 

[root@weekend110 shell]# cat for.sh shell

#!/bin/bashapache

for ((i=0;i<10;i++))編程

do

echo $i

done

[root@weekend110 shell]# for.sh 

0

1

2

3

4

5

6

7

8

9

[root@weekend110 shell]#

 

 

Linux shell編程入門

 

[root@weekend110 shell]# more for1.sh 

#!/bin/bash

for i in 0 1 2 3 4 5 6 7 8 9

do

echo $i

done

[root@weekend110 shell]# for1.sh 

0

1

2

3

4

5

6

7

8

9

[root@weekend110 shell]#

 

 

Linux shell編程入門

 

[root@weekend110 shell]# more for2.sh 

#!/bin/bash

for i in {0..9}

do

echo $i

done

[root@weekend110 shell]# for2.sh 

0

1

2

3

4

5

6

7

8

9

[root@weekend110 shell]#

 

 

Linux shell編程入門

 

 

Linux shell編程入門

 

 

Linux shell編程入門

 

 

Linux shell編程入門

 

[root@weekend110 shell]# more while.sh 

#!/bin/bash

while [ $1 -gt 2 ]

do

echo "true"

sleep 1

done

[root@weekend110 shell]#

 

 

Linux shell編程入門

 

[root@weekend110 shell]# more until.sh 

#!/bin/bash

until [ $1 -gt 2 ]

do

echo "true"

sleep 1

done

[root@weekend110 shell]#

 

 

Linux shell編程入門

 

 

Linux shell編程入門

 

[root@weekend110 shell]# more if.sh 

#!/bin/bash

if [ $1 -eq 1 ] 

then

echo 'one'

else

echo 'none'

fi

[root@weekend110 shell]# more if1.sh 

#!/bin/bash

if [ $1 -eq 1 ] 

then

echo 'one'

elif [ $1 -eq 2 ]

then

echo 'two'

elif [ $1 -eq 3 ]

then

echo 'three'

else

echo 'none'

fi

[root@weekend110 shell]#

 

 

Linux shell編程入門

 

 

Linux shell編程入門

 

[root@weekend110 shell]# more case.sh 

#!/bin/bash

case $1 in

1)

echo 'one'

;;

2)

echo 'two'

;;

3)

echo 'three'

;;

*)

echo 'none'

;;

esac

[root@weekend110 shell]# case.sh

none

[root@weekend110 shell]#

 

 

Linux shell編程入門

 

 

Linux shell編程入門

 

這裏,我就在,/下新建shell目錄,用來做爲shell編程的入門。

[root@weekend110 /]# ls

bin   data  etc   lib    lost+found  misc  net  proc  sbin     srv  tmp  var

boot  dev   home  lib64  media       mnt   opt  root  selinux  sys  usr

[root@weekend110 /]# mkdir shell

[root@weekend110 /]# ls

bin   data  etc   lib    lost+found  misc  net  proc  sbin     shell  sys  usr

boot  dev   home  lib64  media       mnt   opt  root  selinux  srv    tmp  var

[root@weekend110 /]# cd shell/

[root@weekend110 shell]# ll

total 0

[root@weekend110 shell]#

 

 

Linux shell編程入門

 

[root@weekend110 shell]# ls

[root@weekend110 shell]# vim break1.sh

 

 

Linux shell編程入門

 

[root@weekend110 shell]# ll

total 4

-rw-r--r--. 1 root root 79 Oct 22 09:15 break1.sh

[root@weekend110 shell]# chmod +x break1.sh

[root@weekend110 shell]# ll

total 4

-rwxr-xr-x. 1 root root 79 Oct 22 09:15 break1.sh

[root@weekend110 shell]# cat break1.sh    //若是變量i達到2,就跳出循環

#!/bin/bash

for ((i=0;i<10;i++))

do

if [ $i -eq 2 ]

then

break

fi

echo $i

done

[root@weekend110 shell]# break1.sh

-bash: break1.sh: command not found

[root@weekend110 shell]# ./break1.sh                  由於,此刻,還沒加入到環境變量

0

1

[root@weekend110 shell]#

 

 

Linux shell編程入門

 

[root@weekend110 shell]# pwd

/shell

[root@weekend110 shell]# vim /etc/profile

export PATH=$PATH:/shell/

 

 

Linux shell編程入門

 

 

Linux shell編程入門

 

[root@weekend110 shell]# source /etc/profile

[root@weekend110 shell]# break1.sh

0

1

[root@weekend110 shell]#

這樣,就達到了。

 

 

Linux shell編程入門

 

 

如今,寫,while循環在外,for循環在內。

 

Linux shell編程入門

 

[root@weekend110 shell]# ls

break1.sh  break2.sh

[root@weekend110 shell]# cat break2.sh

#!/bin/bash

while [ 1 -eq 1 ]

do

 

for ((i=0;i<10;i++))

do

if [ $i -eq 2 ]

then

break

fi

echo $i

done

echo 'yes'

sleep 1

done

[root@weekend110 shell]# break2.sh

0

1

yes

0

1

yes

0

1

yes

0

1

yes

^C

[root@weekend110 shell]#

 

 

Linux shell編程入門

 

 

Linux shell編程入門

 

[root@weekend110 shell]# cat break3.sh

#!/bin/bash

while [ 1 -eq 1 ]

do

 

for ((i=0;i<10;i++))

do

if [ $i -eq 2 ]

then

break 2              //在這裏,默認是1,寫個2,則說的是跳出2層循環。

fi

echo $i

done

echo 'yes'

sleep 1

done

[root@weekend110 shell]# break3.sh

0

1

[root@weekend110 shell]#

 

跳出單循環

for((i=0;i<10;i++))

do

echo $i

if [ $i -eq 2 ]

then

break

fi

done

 

跳出內循環

while [ 1 -eq 1 ]

do

echo 'yes'

sleep 1

for((i=0;i<10;i++))

do

echo $i

if [ $i -eq 2 ]

then

break

fi

done

done

 

跳出外循環

while [ 1 -eq 1 ]

do

echo 'yes'

sleep 1

for((i=0;i<10;i++))

do

echo $i

if [ $i -eq 2 ]

then

break 2

fi

done

done

 

 

Linux shell編程入門

 

 

Linux shell編程入門

 

[root@weekend110 shell]# cat continue.sh

#/bin/bash

for ((i=0;i<10;i++))

do

if [ $i -eq 2 ]

then

continue

fi

echo $i

done

[root@weekend110 shell]# continue.sh

0

1

3

4

5

6

7

8

9

[root@weekend110 shell]#

 

 

Linux shell編程入門

 

[root@weekend110 shell]# cat fun.sh

#!/bin/bash

function test(){

 echo "this is a funcction"

}

[root@weekend110 shell]# fun.sh 

 

 

Linux shell編程入門

 

[root@weekend110 shell]# cat fun.sh

#!/bin/bash

function test(){

 echo "this is a funcction"

}

test

[root@weekend110 shell]# fun.sh

this is a funcction

[root@weekend110 shell]#

 

可是,通常,生產裏,也不會這麼去作。

 

Linux shell編程入門

 

 

Linux shell編程入門

 

[root@weekend110 shell]# cat fun.sh

#!/bin/bash

function test(){

 echo "this is a funcction"

}

[root@weekend110 shell]# cat funtest.sh

#!/bin/bash

source fun.sh

 

test

[root@weekend110 shell]# funtest.sh

this is a funcction

[root@weekend110 shell]#

 

         這樣的好處,是fun.sh腳本,能夠重用。

 

 

Linux shell編程入門

 

[root@weekend110 shell]# cat fun.sh

#!/bin/bash

function test(){

 echo "this is a funcction"$1

}

[root@weekend110 shell]# cat funtest.sh

#!/bin/bash

source fun.sh

 

test haha

[root@weekend110 shell]# funtest.sh

this is a funcctionhaha

[root@weekend110 shell]#

 

 

 

Linux shell編程入門

 

 

Linux shell編程入門

 

[root@weekend110 shell]# cat fun.sh

#!/bin/bash

function test(){

 echo "this is a funcction"$1

}

[root@weekend110 shell]# cat funtest.sh

#!/bin/bash

source fun.sh

 

test $1

[root@weekend110 shell]# funtest.sh

this is a funcction

[root@weekend110 shell]# funtest.sh hehe

this is a funcctionhehe

[root@weekend110 shell]# funtest.sh haha hehe

this is a funcctionhaha

[root@weekend110 shell]# funtest.sh hahahehe

this is a funcctionhahahehe

[root@weekend110 shell]#

 

 

 

Linux shell編程入門

 

 

Linux shell編程入門

 

[root@weekend110 shell]# cat fun.sh

#!/bin/bash

function test(){

 echo "this is a funcction"$1

 return 20

}

[root@weekend110 shell]# cat funtest.sh

#!/bin/bash

source fun.sh

 

test $1

[root@weekend110 shell]# funtest.sh haha

this is a funcctionhaha

[root@weekend110 shell]# echo $?

20

[root@weekend110 shell]#

 

 

 

Linux shell編程入門

 

 

Linux shell編程入門

 

[root@weekend110 shell]# type cd

cd is a shell builtin

[root@weekend110 shell]# type date

date is hashed (/bin/date)

[root@weekend110 shell]#

 

 

Linux shell編程入門

 

[root@weekend110 shell]# help cd

cd: cd [-L|-P] [dir]

    Change the shell working directory.

   

    Change the current directory to DIR.  The default DIR is the value of the

    HOME shell variable.

   

    The variable CDPATH defines the search path for the directory containing

    DIR.  Alternative directory names in CDPATH are separated by a colon (:).

    A null directory name is the same as the current directory.  If DIR begins

    with a slash (/), then CDPATH is not used.

   

    If the directory is not found, and the shell option `cdable_vars' is set,

    the word is assumed to be  a variable name.  If that variable has a value,

    its value is used for DIR.

   

    Options:

        -L      force symbolic links to be followed

        -P      use the physical directory structure without following symbolic

        links

   

    The default is to follow symbolic links, as if `-L' were specified.

   

    Exit Status:

    Returns 0 if the directory is changed; non-zero otherwise.

[root@weekend110 shell]#

 

 

Linux shell編程入門

 

[root@weekend110 shell]# man date       按長空格鍵,翻頁

DATE(1)                          User Commands                         DATE(1)

 

NAME

       date - print or set the system date and time

 

SYNOPSIS

       date [OPTION]... [+FORMAT]

       date [-u|--utc|--universal] [MMDDhhmm[[CC]YY][.ss]]

 

DESCRIPTION

       Display the current time in the given FORMAT, or set the system date.

 

       -d, --date=STRING

              display time described by STRING, not ‘now’

 

       -f, --file=DATEFILE

              like --date once for each line of DATEFILE

 

       -r, --reference=FILE

              display the last modification time of FILE

 

       -R, --rfc-2822

              output date and time in RFC 2822 format.  Example: Mon, 07 Aug 2006 12:34:56 -0600

 

       --rfc-3339=TIMESPEC

              output  date  and  time in RFC 3339 format.  TIMESPEC=‘date’, ‘seconds’, or ‘ns’ for date and time to the

              indicated precision.  Date and time components are separated by a single space: 2006-08-07 12:34:56-06:00

 

       -s, --set=STRING

              set time described by STRING

 

       -u, --utc, --universal

              print or set Coordinated Universal Time

 

       --help display this help and exit

 

:

 

 

Linux shell編程入門

 

 

Linux shell編程入門

 

[root@weekend110 shell]# type date

date is hashed (/bin/date)

[root@weekend110 shell]# man date

DATE(1) User Commands DATE(1)

NAME

date - print or set the system date and time

SYNOPSIS

date [OPTION]... [+FORMAT]

date [-u|--utc|--universal] [MMDDhhmm[[CC]YY][.ss]]

DESCRIPTION

Display the current time in the given FORMAT, or set the system date.

-d, --date=STRING

display time described by STRING, not ‘now’

-f, --file=DATEFILE

like --date once for each line of DATEFILE

-r, --reference=FILE

display the last modification time of FILE

-R, --rfc-2822

output date and time in RFC 2822 format. Example: Mon, 07 Aug 2006 12:34:56 -0600

--rfc-3339=TIMESPEC

output date and time in RFC 3339 format. TIMESPEC=‘date’, ‘seconds’, or ‘ns’ for date and time to the

indicated precision. Date and time components are separated by a single space: 2006-08-07 12:34:56-06:00

-s, --set=STRING

set time described by STRING

-u, --utc, --universal

print or set Coordinated Universal Time

--help display this help and exit

--version

output version information and exit

FORMAT controls the output. Interpreted sequences are:

%% a literal %

%a locale’s abbreviated weekday name (e.g., Sun)

%A locale’s full weekday name (e.g., Sunday)

%b locale’s abbreviated month name (e.g., Jan)

%B locale’s full month name (e.g., January)

%c locale’s date and time (e.g., Thu Mar 3 23:05:25 2005)

%C century; like %Y, except omit last two digits (e.g., 20)

%d day of month (e.g, 01)

%D date; same as %m/%d/%y

%e day of month, space padded; same as %_d

%F full date; same as %Y-%m-%d

%g last two digits of year of ISO week number (see %G)

%G year of ISO week number (see %V); normally useful only with %V

%h same as %b

%H hour (00..23)

%I hour (01..12)

%j day of year (001..366)

%k hour ( 0..23)

%l hour ( 1..12)

%m month (01..12)

%M minute (00..59)

%n a newline

%N nanoseconds (000000000..999999999)

%p locale’s equivalent of either AM or PM; blank if not known

%P like %p, but lower case

%r locale’s 12-hour clock time (e.g., 11:11:04 PM)

%R 24-hour hour and minute; same as %H:%M

%s seconds since 1970-01-01 00:00:00 UTC

%S second (00..60)

%t a tab

%T time; same as %H:%M:%S

%u day of week (1..7); 1 is Monday

%U week number of year, with Sunday as first day of week (00..53)

%V ISO week number, with Monday as first day of week (01..53)

%w day of week (0..6); 0 is Sunday

%W week number of year, with Monday as first day of week (00..53)

%x locale’s date representation (e.g., 12/31/99)

%X locale’s time representation (e.g., 23:13:48)

%y last two digits of year (00..99)

%Y year

%z +hhmm numeric timezone (e.g., -0400)

%:z +hh:mm numeric timezone (e.g., -04:00)

%::z +hh:mm:ss numeric time zone (e.g., -04:00:00)

%:::z numeric time zone with : to necessary precision (e.g., -04, +05:30)

%Z alphabetic time zone abbreviation (e.g., EDT)

By default, date pads numeric fields with zeroes. The following optional flags may follow ‘%’:

- (hyphen) do not pad the field

_ (underscore) pad with spaces

0 (zero) pad with zeros

^ use upper case if possible

# use opposite case if possible

After any flags comes an optional field width, as a decimal number; then an optional modifier, which is either E

to use the locale’s alternate representations if available, or O to use the locale’s alternate numeric symbols

if available.

DATE STRING

The --date=STRING is a mostly free format human readable date string such as "Sun, 29 Feb 2004 16:21:42 -0800"

or "2004-02-29 16:21:42" or even "next Thursday". A date string may contain items indicating calendar date,

time of day, time zone, day of week, relative time, relative date, and numbers. An empty string indicates the

beginning of the day. The date string format is more complex than is easily documented here but is fully

described in the info documentation.

ENVIRONMENT

TZ Specifies the timezone, unless overridden by command line parameters. If neither is specified, the set-

ting from /etc/localtime is used.

AUTHOR

Written by David MacKenzie.

REPORTING BUGS

Report date bugs to bug-coreutils@gnu.org

GNU coreutils home page: <http://www.gnu.org/software/coreutils/>

General help using GNU software: <http://www.gnu.org/gethelp/>

Report date translation bugs to <http://translationproject.org/team/>

COPYRIGHT

Copyright © 2010 Free Software Foundation, Inc. License GPLv3+: GNU GPL version 3 or later

<http://gnu.org/licenses/gpl.html>.

This is free software: you are free to change and redistribute it. There is NO WARRANTY, to the extent permit-

ted by law.

SEE ALSO

The full documentation for date is maintained as a Texinfo manual. If the info and date programs are properly

installed at your site, the command

info coreutils 'date invocation'

should give you access to the complete manual.

DATE STRING

The --date=STRING is a mostly free format human readable date string such as "Sun, 29 Feb 2004 16:21:42 -0800"

or "2004-02-29 16:21:42" or even "next Thursday". A date string may contain items indicating calendar date,

time of day, time zone, day of week, relative time, relative date, and numbers. An empty string indicates the

beginning of the day. The date string format is more complex than is easily documented here but is fully

described in the info documentation.

ENVIRONMENT

TZ Specifies the timezone, unless overridden by command line parameters. If neither is specified, the set-

ting from /etc/localtime is used.

AUTHOR

Written by David MacKenzie.

REPORTING BUGS

Report date bugs to bug-coreutils@gnu.org

GNU coreutils home page: <http://www.gnu.org/software/coreutils/>

General help using GNU software: <http://www.gnu.org/gethelp/>

Report date translation bugs to <http://translationproject.org/team/>

COPYRIGHT

Copyright © 2010 Free Software Foundation, Inc. License GPLv3+: GNU GPL version 3 or later

<http://gnu.org/licenses/gpl.html>.

This is free software: you are free to change and redistribute it. There is NO WARRANTY, to the extent permit-

ted by law.

SEE ALSO

The full documentation for date is maintained as a Texinfo manual. If the info and date programs are properly

installed at your site, the command

info coreutils 'date invocation'

should give you access to the complete manual.

GNU coreutils 8.4 November 2013 DATE(1)

 

man date對應的中文手冊

date命令的幫助信息

[root@localhost source]# date --help

用法:date [選項]... [+格式]

或:date [-u|--utc|--universal] [MMDDhhmm[[CC]YY][.ss]]

以給定的格式顯示當前時間,或是設置系統日期。

-d,--date=字符串 顯示指定字符串所描述的時間,而非當前時間

-f,--file=日期文件 相似--date,從日期文件中按行讀入時間描述

-r, --reference=文件 顯示文件指定文件的最後修改時間

-R, --rfc-2822 以RFC 2822格式輸出日期和時間

例如:2006年8月7日,星期一 12:34:56 -0600

--rfc-3339=TIMESPEC 以RFC 3339 格式輸出日期和時間。

TIMESPEC=`date',`seconds',或 `ns' 

表示日期和時間的顯示精度。

日期和時間單元由單個的空格分開:

2006-08-07 12:34:56-06:00

-s, --set=字符串 設置指定字符串來分開時間

-u, --utc, --universal 輸出或者設置協調的通用時間

--help 顯示此幫助信息並退出

--version 顯示版本信息並退出

給定的格式FORMAT 控制着輸出,解釋序列以下:

%% 一個文字的 %

%a 當前locale 的星期名縮寫(例如: 日,表明星期日)

%A 當前locale 的星期名全稱 (如:星期日)

%b 當前locale 的月名縮寫 (如:一,表明一月)

%B 當前locale 的月名全稱 (如:一月)

%c 當前locale 的日期和時間 (如:2005年3月3日 星期四 23:05:25)

%C 世紀;好比 %Y,一般爲省略當前年份的後兩位數字(例如:20)

%d 按月計的日期(例如:01)

%D 按月計的日期;等於%m/%d/%y

%e 按月計的日期,添加空格,等於%_d

%F 完整日期格式,等價於 %Y-%m-%d

%g ISO-8601 格式年份的最後兩位 (參見%G)

%G ISO-8601 格式年份 (參見%V),通常只和 %V 結合使用

%h 等於%b

%H 小時(00-23)

%I 小時(00-12)

%c 按年計的日期(001-366)

%k 時(0-23)

%l 時(1-12)

%m 月份(01-12)

%M 分(00-59)

%n 換行

%N 納秒(000000000-999999999)

%p 當前locale 下的"上午"或者"下午",未知時輸出爲空

%P 與%p 相似,可是輸出小寫字母

%r 當前locale 下的 12 小時時鐘時間 (如:11:11:04 下午)

%R 24 小時時間的時和分,等價於 %H:%M

%s 自UTC 時間 1970-01-01 00:00:00 以來所通過的秒數

%S 秒(00-60)

%t 輸出製表符 Tab

%T 時間,等於%H:%M:%S

%u 星期,1 表明星期一

%U 一年中的第幾周,以週日爲每星期第一天(00-53)

%V ISO-8601 格式規範下的一年中第幾周,以週一爲每星期第一天(01-53)

%w 一星期中的第幾日(0-6),0 表明週一

%W 一年中的第幾周,以週一爲每星期第一天(00-53)

%x 當前locale 下的日期描述 (如:12/31/99)

%X 當前locale 下的時間描述 (如:23:13:48)

%y 年份最後兩位數位 (00-99)

%Y 年份

%z +hhmm 數字時區(例如,-0400)

%:z +hh:mm 數字時區(例如,-04:00)

%::z +hh:mm:ss 數字時區(例如,-04:00:00)

%:::z 數字時區帶有必要的精度 (例如,-04,+05:30)

%Z 按字母表排序的時區縮寫 (例如,EDT)

默認狀況下,日期的數字區域以0 填充。

如下可選標記能夠跟在"%"後:

- (連字符)不填充該域

_ (下劃線)以空格填充

0 (數字0)以0 填充

^ 若是可能,使用大寫字母

# 若是可能,使用相反的大小寫

在任何標記以後還容許一個可選的域寬度指定,它是一個十進制數字。

做爲一個可選的修飾聲明,它能夠是E,在可能的狀況下使用本地環境關聯的

表示方式;或者是O,在可能的狀況下使用本地環境關聯的數字符號。

 

 

Linux shell編程入門

 

 

Linux shell編程入門

 

[root@weekend110 shell]# read weekend110

aaaa

[root@weekend110 shell]# echo $weekend110

aaaa

[root@weekend110 shell]# read -p "enter your name:" name

enter your name:zhouls

[root@weekend110 shell]# read -s -p "enter your password:" password

enter your password:[root@weekend110 shell]# echo $password

sss

[root@weekend110 shell]# read -p "enter your name:" name

enter your name:ss

[root@weekend110 shell]# read -t 3 -p "enter your name:" name      3秒以後,自動

enter your name:[root@weekend110 shell]#

 

 

Linux shell編程入門

 

 

Linux shell編程入門

 

[root@weekend110 shell]# a=1+1

[root@weekend110 shell]# echo $a

1+1

[root@weekend110 shell]# declare -i a

[root@weekend110 shell]# a=1+1

[root@weekend110 shell]# echo $a

2

[root@weekend110 shell]# declare -r a

[root@weekend110 shell]# echo $a

2

[root@weekend110 shell]# a=3

-bash: a: readonly variable

[root@weekend110 shell]#

 

   declare的數組,部分,後面更新。

 

除了使用echo $?獲取函數返回值,在shell腳本里怎麼獲取?

答:還能夠,a=$?

 

關於有幾層循環就break?

答:

[root@weekend110 shell]# more break2.sh 

#!/bin/bash

while [ 1 -eq 1 ]

do

for ((i=0;i<10;i++))

do

if [ $i -eq 2 ]

then

break      //等價於 break 1,即退出當前for循環

fi

echo $i

done

echo 'yes'

sleep 1

done

[root@weekend110 shell]#

 

[root@weekend110 shell]# more break3.sh 

#!/bin/bash

while [ 1 -eq 1 ]

do

for ((i=0;i<10;i++))

do

if [ $i -eq 2 ]

then

break 2      //跳出2循環,即先跳出for循環,再跳出while循環

fi

echo $i

done

echo 'yes'

sleep 1

done

[root@weekend110 shell]#

 

while(){ 

for(){ 

break

}

}

 

 

Linux shell編程入門

 

[root@weekend110 shell]# pstree

init─┬─NetworkManager

├─abrtd

├─acpid

├─atd

├─auditd───{auditd}

├─automount───4*[{automount}]

├─bonobo-activati───{bonobo-activat}

├─certmonger

├─console-kit-dae───63*[{console-kit-da}]

├─crond

├─cupsd

├─2*[dbus-daemon───{dbus-daemon}]

├─dbus-launch

├─devkit-power-da

├─gconfd-2

├─gdm-binary─┬─gdm-simple-slav─┬─Xorg

│ │ ├─gdm-session-wor

│ │ ├─gnome-session─┬─at-spi-registry

│ │ │ ├─gdm-simple-gree

│ │ │ ├─gnome-power-man

│ │ │ ├─metacity

│ │ │ ├─plymouth-log-vi

│ │ │ ├─polkit-gnome-au

│ │ │ └─{gnome-session}

│ │ └─{gdm-simple-sla}

│ └─{gdm-binary}

├─gnome-settings-───{gnome-settings}

├─gvfsd

├─hald─┬─hald-runner─┬─hald-addon-acpi

│ │ └─hald-addon-inpu

│ └─{hald}

├─master─┬─pickup

│ └─qmgr

├─5*[mingetty]

├─modem-manager

├─polkitd

├─pulseaudio───2*[{pulseaudio}]

├─rpc.statd

├─rpcbind

├─rsyslogd───3*[{rsyslogd}]

├─rtkit-daemon───2*[{rtkit-daemon}]

├─sshd───sshd───bash───bash───pstree

├─udevd───2*[udevd]

└─wpa_supplicant

[root@weekend110 shell]#

 

 

Linux shell編程入門

 

[root@weekend110 shell]# cat a.sh

echo 'aaa'                  //由於系統,自帶了#!/bin/bash/ ,可是,生產裏,都要寫,這是規範

[root@weekend110 shell]# a.sh

aaa

[root@weekend110 shell]# mv a.sh a.ss

[root@weekend110 shell]# a.ss      //由於,對於linux裏,後綴名無關。可是,生產裏,都要寫,.sh結尾的後綴,這是規範

aaa

[root@weekend110 shell]#

 

 

 

Linux shell編程入門

 

 

Linux shell編程入門

 

[root@weekend110 shell]# zhouls=www.zhouls.com.cn

[root@weekend110 shell]# echo ${zhouls}

www.zhouls.com.cn      //腳標從0開始

[root@weekend110 shell]# echo ${#zhouls}

17

[root@weekend110 shell]# echo ${zhouls:1:1}    第一個1,是腳標,第二個1,是截取長度

w

[root@weekend110 shell]# echo ${zhouls:1:2}

ww

[root@weekend110 shell]# echo ${zhouls:1:3}

ww.

[root@weekend110 shell]# echo ${zhouls:1}

ww.zhouls.com.cn

[root@weekend110 shell]#

 

 

Linux shell編程入門

 

 

 

Linux shell編程入門

 

[root@weekend110 shell]# echo ${zhouls}

www.zhouls.com.cn

[root@weekend110 shell]# echo ${zhouls: -2}       從尾部來截取,-2是截取2個

cn

[root@weekend110 shell]# echo ${zhouls#*c}       自左而右,查找第一次出現c,刪除首部開始到c處的全部內容

om.cn

[root@weekend110 shell]# echo ${zhouls##*c}     自左而右,查找最後一次出現c,刪除首部開始到c處的全部內容

[root@weekend110 shell]# echo ${zhouls%c*}      自右而左,查找第一次出現c,刪除c處到尾部的全部內容

www.zhouls.com.

[root@weekend110 shell]# echo ${zhouls%%c*}   自右而左,查找最後一次出現c,刪除c處到尾部的全部內容

www.zhouls.

[root@weekend110 shell]#

 

 

Linux shell編程入門

 

 

Linux shell編程入門

 

[root@weekend110 shell]# chouls=www.chouls.com.cn

[root@weekend110 shell]# echo ${chouls}

www.chouls.com.cn

[root@weekend110 shell]# echo ${chouls/c/a}     替換第一次出現,用c替換成a

www.ahouls.com.cn

[root@weekend110 shell]# echo ${chouls//c/a}    替換全部的出現,用c替換成a

www.ahouls.aom.an

[root@weekend110 shell]# echo ${chouls/#c/a}   若是行首沒有c,則不替換

www.chouls.com.cn

[root@weekend110 shell]# echo ${chouls/#w/a}  行首有w,用w替換成a

aww.chouls.com.cn

[root@weekend110 shell]# echo ${chouls/#wc/aa}   行首沒有wc

www.chouls.com.cn

[root@weekend110 shell]# echo ${chouls/%wc/aa}  行尾沒有wc

www.chouls.com.cn

[root@weekend110 shell]# echo ${chouls/%cn/aa}  行尾右cn,用cn替換成aa

www.chouls.com.aa

[root@weekend110 shell]#

 

 

 

Linux shell編程入門

 

 

Linux shell編程入門

 

[root@weekend110 shell]# chouls=www.chouls.com.cn

[root@weekend110 shell]# echo ${chouls/c}  刪除第一次的出現,匹配的c

www.houls.com.cn

[root@weekend110 shell]# echo ${chouls//c}  刪除全部的出現,匹配的c

www.houls.om.n

[root@weekend110 shell]# echo ${chouls/#c}  刪除行首匹配的c

www.chouls.com.cn

[root@weekend110 shell]# echo ${chouls/#www}   刪除行首匹配的www

.chouls.com.cn

[root@weekend110 shell]# echo ${chouls/%www}    刪除行尾匹配的www

www.chouls.com.cn

[root@weekend110 shell]# echo ${chouls/%cn}      刪除行尾匹配的cn

www.chouls.com.

[root@weekend110 shell]# echo ${chouls/[x-y]}    

www.chouls.com.cn

[root@weekend110 shell]# echo ${chouls/[a-x]}    

ww.chouls.com.cn

[root@weekend110 shell]# echo ${chouls//[a-x]}

...

[root@weekend110 shell]#

 

 

Linux shell編程入門

 

 

Linux shell編程入門

 

[root@weekend110 shell]# echo ${chouls}

www.chouls.com.cn

[root@weekend110 shell]# echo ${chouls^^}       小寫,轉大寫

WWW.CHOULS.COM.CN

[root@weekend110 shell]# CHOULS=WWW.CHOULS.COM.CN

[root@weekend110 shell]# echo ${chouls,,}         大寫,轉小寫

www.chouls.com.cn

[root@weekend110 shell]#

 

 

Linux shell編程入門

 

 

Linux shell編程入門

 

[root@weekend110 shell]# zhouls=www.zhouls.com.cn

[root@weekend110 shell]# echo ${zhouls}

www.zhouls.com.cn

[root@weekend110 shell]# echo ${zhouls:-aaa}

www.zhouls.com.cn

[root@weekend110 shell]# zhouls=''              

[root@weekend110 shell]# echo ${zhouls:-aaa}       zhouls的值爲空或不存在,則返回aaa,此時,zhouls的值依然是空或不存在

aaa

[root@weekend110 shell]# echo ${zhouls}

[root@weekend110 shell]# echo ${zhouls:=aaa}      zhouls的值爲空或不存在,則返回aaa,此時,zhouls的值變成aaa 

aaa

[root@weekend110 shell]# echo ${zhouls}

aaa 

[root@weekend110 shell]# echo ${zhouls:?:aaa}     zhouls的值爲空或不存在,把aaa當成錯誤信息返回

aaa

[root@weekend110 shell]# echo ${zhouls1:?:aaa}   zhouls1的值爲空或不存在,把aaa當成錯誤信息返回

bash: zhouls1: :aaa

[root@weekend110 shell]# echo ${zhouls1:?:command not found}    houls1的值爲空或不存在,把command not found當成錯誤信息返回

bash: zhouls1: :command not found

[root@weekend110 shell]# echo ${zhouls1:+aaa}         

[root@weekend110 shell]# echo ${zhouls1:-aaa}

aaa

[root@weekend110 shell]#

 

 

Linux shell編程入門

 

 

 

Linux shell編程入門

 

稀疏格式,即,能夠爲空。

僅,支持一維數組。

 

 

Linux shell編程入門

 

[root@weekend110 shell]# a[0]=a0

[root@weekend110 shell]# echo ${a[0]}   一次對一個元素賦值

a0  

[root@weekend110 shell]# echo ${a[1]}

[root@weekend110 shell]# a[2]=a2

[root@weekend110 shell]# echo ${a[2]}

a2

[root@weekend110 shell]# a=(a b c)         一次對多個元素賦值

[root@weekend110 shell]# echo ${a[0]}

a

[root@weekend110 shell]# echo ${a[1]}

b

[root@weekend110 shell]# echo ${a[2]}

c

[root@weekend110 shell]# a[0]=x

[root@weekend110 shell]# echo ${a[0]}

x

[root@weekend110 shell]# a[4]=z

[root@weekend110 shell]# echo ${a[4]}

z

[root@weekend110 shell]# ls /var/log/*.log

/var/log/anaconda.ifcfg.log /var/log/anaconda.yum.log /var/log/pm-powersave.log /var/log/Xorg.9.log

/var/log/anaconda.log /var/log/boot.log /var/log/spice-vdagent.log /var/log/yum.log

/var/log/anaconda.program.log /var/log/dracut.log /var/log/wpa_supplicant.log

/var/log/anaconda.storage.log /var/log/mysqld.log /var/log/Xorg.0.log

[root@weekend110 shell]# logs=($(ls /var/log/*.log))      命令替換

[root@weekend110 shell]# echo ${logs[0]}

/var/log/anaconda.ifcfg.log

[root@weekend110 shell]# echo ${logs[6]}

/var/log/dracut.log

[root@weekend110 shell]# read zhouls

a c b d x

[root@weekend110 shell]# echo ${zhouls[0]}

a c b d x

[root@weekend110 shell]# echo ${zhouls[1]}

[root@weekend110 shell]# read -a zhouls

a b c d x

[root@weekend110 shell]# echo ${zhouls[0]}

a

[root@weekend110 shell]# echo ${zhouls[1]}

b

[root@weekend110 shell]# echo ${zhouls}

a

[root@weekend110 shell]# echo ${zhouls[*]}       查看數組的全部元素

a b c d x

[root@weekend110 shell]# echo ${zhouls[@]}  查看數組的全部元素

a b c d x

[root@weekend110 shell]# echo ${#zhouls[*]}     獲取數組的長度

5

[root@weekend110 shell]# echo ${#zhouls[@]}    獲取數組的長度

[root@weekend110 shell]# echo ${#zhouls[0]}       獲取數組元素的長度

1

[root@weekend110 shell]# echo ${#zhouls[1]}

1

[root@weekend110 shell]# echo ${#zhouls[20]}

0

[root@weekend110 shell]# echo ${#logs[0]}

27

[root@weekend110 shell]# echo ${#logs[*]}

14

[root@weekend110 shell]#

 

 

Linux shell編程入門

 

 

Linux shell編程入門

 

[root@weekend110 shell]# echo ${#zhouls[@]:0:2}        0是偏移腳標,2是取出長度

5

[root@weekend110 shell]# echo ${zhouls[@]:0:2}

a b

[root@weekend110 shell]# echo ${zhouls[@]:0:4}

a b c d

[root@weekend110 shell]# echo ${zhouls[@]:1:2}

b c

[root@weekend110 shell]# echo ${zhouls[@]:1}

b c d x

[root@weekend110 shell]# echo ${zhouls[@]}

a b c d x

 

 

Linux shell編程入門

 

[root@weekend110 shell]# more arr1.sh 

#!/bin/bash

for i in {0..4}

do 

num[$i]=$i

done

for j in ${num[@]}

do

echo $j

done

echo '---------------------'

for j in "${num[@]}"    建議用這

do

echo $j

done

echo '---------------------'

for j in ${num[*]}

do

echo $j

done

echo '---------------------'

for j in "${num[*]}"    認爲是一個元素去了

do

echo $j

done

[root@weekend110 shell]# arr1.sh

0

1

2

3

4

---------------------

0

1

2

3

4

---------------------

0

1

2

3

4

---------------------

0 1 2 3 4

[root@weekend110 shell]#

 

 

Linux shell編程入門

 

[root@weekend110 shell]# more arr2.sh

#!/bin/bash

for j in $@

do

echo $j

done

echo '---------------------'

for j in "$@"

do

echo $j

done

echo '---------------------'

for j in $*

do

echo $j

done

echo '---------------------'

for j in "$*"

do

echo $j

done

[root@weekend110 shell]# arr2.sh

---------------------

---------------------

---------------------

[root@weekend110 shell]# arr2.sh a b c

a

b

c

---------------------

a

b

c

---------------------

a

b

c

---------------------

a b c

[root@weekend110 shell]#

 

 

Linux shell編程入門

 

 

Linux shell編程入門

 

[root@weekend110 shell]# more sleep.sh 

#!/bin/bash

echo 'start'

sleep 10000000000

echo 'stop'

[root@weekend110 shell]# sleep.sh &

[1] 3518

[root@weekend110 shell]# start

[root@weekend110 shell]# ps -ef|grep bash

root 3438 1846 0 15:40 pts/1 00:00:00 -bash

root 3518 3438 0 15:46 pts/1 00:00:00 /bin/bash /shell/sleep.sh

root 3521 3438 0 15:46 pts/1 00:00:00 grep bash

[root@weekend110 shell]#

 

 

Linux shell編程入門

 

 

Linux shell編程入門

 

[root@weekend110 shell]# ps -ef|grep bash

root 3518 1 0 15:46 ? 00:00:00 /bin/bash /shell/sleep.sh

root 3594 3590 0 15:50 pts/2 00:00:00 -bash

root 3621 3594 0 15:52 pts/2 00:00:00 grep bash

[root@weekend110 shell]# nohup sleep.sh &

[1] 3622

[root@weekend110 shell]# nohup: ignoring input and appending output to `nohup.out'

[root@weekend110 shell]#

 

 

Linux shell編程入門

 

[root@weekend110 shell]# ps -ef|grep bash

root 3622 1 0 15:52 ? 00:00:00 /bin/bash /shell//sleep.sh

root 3630 3625 0 15:53 pts/2 00:00:00 -bash

root 3655 3630 0 15:53 pts/2 00:00:00 grep bash

[root@weekend110 shell]# pwd

/shell

[root@weekend110 shell]# ll

total 48

-rwxr-xr-x. 1 root root 280 Oct 22 15:26 arr1.sh

-rwxr-xr-x. 1 root root 216 Oct 22 15:32 arr2.sh

-rwxr-xr-x. 1 root root 79 Oct 22 09:15 break1.sh

-rwxr-xr-x. 1 root root 125 Oct 22 09:40 break2.sh

-rwxr-xr-x. 1 root root 127 Oct 22 09:43 break3.sh

-rwxr-xr-x. 1 root root 81 Oct 22 09:51 continue.sh

-rwxr-xr-x. 1 root root 72 Oct 22 10:05 fun.sh

-rwxr-xr-x. 1 root root 35 Oct 22 10:03 funtest.sh

-rw-------. 1 root root 6 Oct 22 15:52 nohup.out

-rwxr-xr-x. 1 root root 57 Oct 22 15:35 sleep.sh

-rw-r--r--. 1 root root 5561 Oct 22 15:43 zookeeper.out

[root@weekend110 shell]# more nohup.out 

start

[root@weekend110 shell]#

 

在這裏,拋磚引玉。

 

 

Linux shell編程入門

 

[root@weekend110 shell]# jps

3704 Jps

[root@weekend110 shell]# /home/hadoop/app/zookeeper-3.4.6/bin/zkServer.sh start

JMX enabled by default

Using config: /home/hadoop/app/zookeeper-3.4.6/bin/../conf/zoo.cfg

Starting zookeeper ... STARTED

[root@weekend110 shell]# jps

3735 Jps

3722 QuorumPeerMain

[root@weekend110 shell]#

   能夠,參考個人博客

1 storm基本概念 + storm編程規範及demo編寫

 

在這裏,爲何像這的第三方,爲何不需加nohup呢?那是由於,它們在開發時,源碼、內核和腳本都弄好了。

本身寫腳本,須要加nohup。

 

   問:使用nohup啓動的java進程,可讓java日誌不打入到nohup.out中,而打到本身制定的日誌文件中麼?

答:不能夠,必須是在nohup.out。在shell裏面,寫,是能夠打人到本身制定的日誌文件中。

 

 

 

Linux shell編程入門

 

標準輸入、標準輸出、標準錯誤輸出

 

 

Linux shell編程入門

 

標準輸入

[root@weekend110 shell]# more a.txt

ss

a s

vv a

[root@weekend110 shell]# wc < a.txt      把a.txt當作標準輸入,傳給wc

3 5 12        3行,5個單詞,12個字節數

 

標準輸出    

[root@weekend110 shell]# ls

arr1.sh arr2.sh a.txt break1.sh break2.sh break3.sh continue.sh fun.sh funtest.sh nohup.out sleep.sh zookeeper.out

[root@weekend110 shell]# ls > a.txt       把ls當作標準輸出,傳給a.txt

[root@weekend110 shell]# more a.txt

arr1.sh

arr2.sh

a.txt

break1.sh

break2.sh

break3.sh

continue.sh

fun.sh

funtest.sh

nohup.out

sleep.sh

zookeeper.out

[root@weekend110 shell]# ls > a.txt         覆蓋

[root@weekend110 shell]# more a.txt

arr1.sh

arr2.sh

a.txt

break1.sh

break2.sh

break3.sh

continue.sh

fun.sh

funtest.sh

nohup.out

sleep.sh

zookeeper.out

[root@weekend110 shell]# ls 1> a.txt         等價於  ls > a.txt

[root@weekend110 shell]# more a.txt

arr1.sh

arr2.sh

a.txt

break1.sh

break2.sh

break3.sh

continue.sh

fun.sh

funtest.sh

nohup.out

sleep.sh

zookeeper.out

[root@weekend110 shell]# ls >> a.txt      追加

[root@weekend110 shell]# more a.txt

arr1.sh

arr2.sh

a.txt

break1.sh

break2.sh

break3.sh

continue.sh

fun.sh

funtest.sh

nohup.out

sleep.sh

zookeeper.out

arr1.sh

arr2.sh

a.txt

break1.sh

break2.sh

break3.sh

continue.sh

fun.sh

funtest.sh

nohup.out

sleep.sh

zookeeper.out

[root@weekend110 shell]# lkkkk 1> a.txt      lkkkk是錯誤命令,   標準輸出

-bash: lkkkk: command not found

[root@weekend110 shell]# lkkkk 2> a.txt      標準錯誤輸出

[root@weekend110 shell]# more a.txt

-bash: lkkkk: command not found

[root@weekend110 shell]# lkkkk 2>> a.txt    追加

[root@weekend110 shell]# more a.txt

-bash: lkkkk: command not found

-bash: lkkkk: command not found

[root@weekend110 shell]# ls 2>&1 > a.txt     

[root@weekend110 shell]# more a.txt

arr1.sh

arr2.sh

a.txt

break1.sh

break2.sh

break3.sh

continue.sh

fun.sh

funtest.sh

nohup.out

sleep.sh

zookeeper.out

[root@weekend110 shell]# lkkkk 2>&1 >a.txt

-bash: lkkkk: command not found

[root@weekend110 shell]# lkkkk >a.txt 2>&1

[root@weekend110 shell]# more a.txt

-bash: lkkkk: command not found

[root@weekend110 shell]# lkkkk >>a.txt 2>&1

[root@weekend110 shell]# more a.txt

-bash: lkkkk: command not found

-bash: lkkkk: command not found

[root@weekend110 shell]# ls >>a.txt 2>&1

[root@weekend110 shell]# more a.txt

-bash: lkkkk: command not found

-bash: lkkkk: command not found

arr1.sh

arr2.sh

a.txt

break1.sh

break2.sh

break3.sh

continue.sh

fun.sh

funtest.sh

nohup.out

sleep.sh

zookeeper.out

[root@weekend110 shell]# 

[root@weekend110 shell]# ls >/dev/null       把輸出信息定向到無底洞,即通常是把一些無用的信息,放到無底洞裏。

[root@weekend110 shell]# ls >/dev/null 2>&1

[root@weekend110 shell]# lkkkk /dev/null 2>&1

-bash: lkkkk: command not found

[root@weekend110 shell]#

 

 

Linux shell編程入門

 

[root@weekend110 shell]# more sleep.sh 

#!/bin/bash

echo 'start' >>a.txt        這樣的好處,猶如在大數據裏的日誌文件同樣,便於開發人員,隨時查看日誌。就是採用這個原理

sleep 10000000000

echo 'stop'

[root@weekend110 shell]#

 

 

Linux shell編程入門

 

 

Linux shell編程入門

 

[root@weekend110 shell]# man crontab

CRONTAB(1) Cronie Users’ Manual CRONTAB(1)

NAME

crontab - maintain crontab files for individual users

SYNOPSIS

crontab [-u user] file

crontab [-u user] [-l | -r | -e] [-i] [-s]

DESCRIPTION

Crontab is the program used to install, remove or list the tables used to drive the cron(8) daemon. Each user

can have their own crontab, and though these are files in /var/spool/ , they are not intended to be edited

directly. For SELinux in mls mode can be even more crontabs - for each range. For more see selinux(8).

The cron jobs could be allow or disallow for different users. For classical crontab there exists cron.allow and

cron.deny files. If cron.allow file exists, then you must be listed therein in order to be allowed to use this

command. If the cron.allow file does not exist but the cron.deny file does exist, then you must not be listed

in the cron.deny file in order to use this command. If neither of these files exists, only the super user will

be allowed to use this command. The second option is using PAM authentication, where you set up users, which

could or couldn’t use crontab and also system cron jobs from /etc/cron.d/.

The temporary directory could be set in enviroment variables. If it’s not set by user than /tmp is used.

OPTIONS

-u Append the name of the user whose crontab is to be tweaked. If this option is not given, crontab exam-

ines "your" crontab, i.e., the crontab of the person executing the command. Note that su(8) can confuse

crontab and that if you are running inside of su(8) you should always use the -u option for safety’s

sake. The first form of this command is used to install a new crontab from some named file or standard

input if the pseudo-filename "-" is given.

-l The current crontab will be displayed on standard output.

-r The current crontab will be removed.

-e This option is used to edit the current crontab using the editor specified by the VISUAL or EDITOR envi-

ronment variables. After you exit from the editor, the modified crontab will be installed automatically.

ines "your" crontab, i.e., the crontab of the person executing the command. Note that su(8) can confuse

crontab and that if you are running inside of su(8) you should always use the -u option for safety’s

sake. The first form of this command is used to install a new crontab from some named file or standard

input if the pseudo-filename "-" is given.

-l The current crontab will be displayed on standard output.

-r The current crontab will be removed.

-e This option is used to edit the current crontab using the editor specified by the VISUAL or EDITOR envi-

ronment variables. After you exit from the editor, the modified crontab will be installed automatically.

-i This option modifies the -r option to prompt the user for a ’y/Y’ response before actually removing the

crontab.

-s It will append the current SELinux security context string as an MLS_LEVEL setting to the crontab file

before editing / replacement occurs - see the documentation of MLS_LEVEL in crontab(5).

SEE ALSO

crontab(5),cron(8)

FILES

/etc/cron.allow

/etc/cron.deny

STANDARDS

The crontab command conforms to IEEE Std1003.2-1992 (‘‘POSIX’’). This new command syntax differs from previous

versions of Vixie Cron, as well as from the classic SVR3 syntax.

DIAGNOSTICS

A fairly informative usage message appears if you run it with a bad command line.

AUTHOR

Paul Vixie <vixie@isc.org>

Marcela Mašláňová 20 July 2009 CRONTAB(1)

 

crontab對應的中文手冊:

基本格式 : 

*  *  *  *  *  command 

分 時 日 月 周 命令 

第1列表示分鐘1~59 每分鐘用*或者 */1表示 

第2列表示小時1~23(0表示0點) 

第3列表示日期1~31 

第4列表示月份1~12 

第5列標識號星期0~6(0表示星期天) 

第6列要運行的命令 

crontab文件的一些例子: 

30 21 * * * /usr/local/etc/rc.d/lighttpd restart 

上面的例子表示每晚的21:30重啓apache。 

45 4 1,10,22 * * /usr/local/etc/rc.d/lighttpd restart 

上面的例子表示每個月一、十、22日的4 : 45重啓apache。 

10 1 * * 6,0 /usr/local/etc/rc.d/lighttpd restart 

上面的例子表示每週6、週日的1 : 10重啓apache。 

0,30 18-23 * * * /usr/local/etc/rc.d/lighttpd restart 

上面的例子表示在天天18 : 00至23 : 00之間每隔30分鐘重啓apache。 

0 23 * * 6 /usr/local/etc/rc.d/lighttpd restart 

上面的例子表示每星期六的11 : 00 pm重啓apache。 

* */1 * * * /usr/local/etc/rc.d/lighttpd restart 

每一小時重啓apache 

* 23-7/1 * * * /usr/local/etc/rc.d/lighttpd restart 

晚上11點到早上7點之間,每隔一小時重啓apache 

0 11 4 * mon-wed /usr/local/etc/rc.d/lighttpd restart 

每個月的4號與每週一到週三的11點重啓apache 

0 4 1 jan * /usr/local/etc/rc.d/lighttpd restart 

一月一號的4點重啓apache 

名稱 : crontab 

使用權限 : 全部使用者 

使用方式 : 

crontab file [-u user]-用指定的文件替代目前的crontab。 

crontab-[-u user]-用標準輸入替代目前的crontab. 

crontab-1[user]-列出用戶目前的crontab. 

crontab-e[user]-編輯用戶目前的crontab. 

crontab-d[user]-刪除用戶目前的crontab. 

crontab-c dir- 指定crontab的目錄。 

crontab文件的格式:M H D m d cmd. 

M: 分鐘(0-59)。 

H:小時(0-23)。 

D:天(1-31)。 

m: 月(1-12)。 

d: 一星期內的天(0~6,0爲星期天)。 

cmd要運行的程序,程序被送入sh執行,這個shell只有USER,HOME,SHELL這三個環境變量 

說明 : 

crontab 是用來讓使用者在固定時間或固定間隔執行程序之用,換句話說,也就是相似使用者的時程表。-u user 是指設定指定 

user 的時程表,這個前提是你必需要有其權限(好比說是 root)纔可以指定他人的時程表。若是不使用 -u user 的話,就是表示設 

定本身的時程表。 

參數 : 

crontab -e : 執行文字編輯器來設定時程表,內定的文字編輯器是 VI,若是你想用別的文字編輯器,則請先設定 VISUAL 環境變數 

來指定使用那個文字編輯器(好比說 setenv VISUAL joe) 

crontab -r : 刪除目前的時程表 

crontab -l : 列出目前的時程表 

crontab file [-u user]-用指定的文件替代目前的crontab。 

時程表的格式以下 : 

f1 f2 f3 f4 f5 program 

其中 f1 是表示分鐘,f2 表示小時,f3 表示一個月份中的第幾日,f4 表示月份,f5 表示一個星期中的第幾天。program 表示要執 

行的程序。 

當 f1 爲 * 時表示每分鐘都要執行 program,f2 爲 * 時表示每小時都要執行程序,其餘類推 

當 f1 爲 a-b 時表示從第 a 分鐘到第 b 分鐘這段時間內要執行,f2 爲 a-b 時表示從第 a 到第 b 小時都要執行,其餘類推 

當 f1 爲 */n 時表示每 n 分鐘個時間間隔執行一次,f2 爲 */n 表示每 n 小時個時間間隔執行一次,其餘類推

當 f1 爲 a, b, c,... 時表示第 a, b, c,... 分鐘要執行,f2 爲 a, b, c,... 時表示第 a, b, c...個小時要執行,其餘類推 

使用者也能夠將全部的設定先存放在檔案 file 中,用 crontab file 的方式來設定時程表。 

例子 : 

#天天早上7點執行一次 /bin/ls : 

0 7 * * * /bin/ls 

在 12 月內, 天天的早上 6 點到 12 點中,每隔3個小時執行一次 /usr/bin/backup : 

0 6-12/3 * 12 * /usr/bin/backup 

週一到週五天天下午 5:00 寄一封信給 alex@domain.name : 

0 17 * * 1-5 mail -s "hi" alex@domain.name < /tmp/maildata 

每個月天天的午夜 0 點 20 分, 2 點 20 分, 4 點 20 分....執行 echo "haha" 

20 0-23/2 * * * echo "haha" 

注意 : 

當程序在你所指定的時間執行後,系統會寄一封信給你,顯示該程序執行的內容,如果你不但願收到這樣的信,請在每一行空一格之 

後加上 > /dev/null 2>&1 便可 

例子2 : 

#天天早上6點10分 

10 6 * * * date 

#每兩個小時 

0 */2 * * * date 

#晚上11點到早上8點之間每兩個小時,早上8點 

0 23-7/2,8 * * * date 

#每月的4號和每一個禮拜的禮拜一到禮拜三的早上11點 

0 11 4 * mon-wed date 

#1月份日早上4點 

0 4 1 jan * date 

範例 

$crontab -l 列出用戶目前的crontab.

 

參考: http://www.cnblogs.com/cocowool/archive/2009/04/22/1441291.html    

 

 

Linux shell編程入門

 

[root@weekend110 ~]# service crond status

crond (pid 1624) is running...

[root@weekend110 ~]# crontab -e

no crontab for root - using an empty one

#* * * * * /usr/sbin/ntpdate time.nist.gov

* * * * * echo 'date'

 

 

Linux shell編程入門

 

[root@weekend110 ~]# crontab -l        查看使用

#* * * * * /usr/sbin/ntpdate time.nist.gov

* * * * * echo 'date'

[root@weekend110 ~]#

 

 

Linux shell編程入門

 

[root@weekend110 ~]# service rsyslog status

rsyslogd (pid 1244) is running...

[root@weekend110 ~]# tail -f /var/log/cron

Oct 22 20:21:44 weekend110 crontab[1891]: (root) END EDIT (root)

Oct 22 20:22:02 weekend110 CROND[1915]: (root) CMD (echo 'date')

Oct 22 20:22:25 weekend110 crontab[1923]: (root) LIST (root)

Oct 22 20:22:37 weekend110 crontab[1925]: (root) BEGIN EDIT (root)

Oct 22 20:22:41 weekend110 crontab[1925]: (root) REPLACE (root)

Oct 22 20:22:41 weekend110 crontab[1925]: (root) END EDIT (root)

Oct 22 20:22:43 weekend110 crontab[1929]: (root) LIST (root)

Oct 22 20:23:01 weekend110 crond[1624]: (root) RELOAD (/var/spool/cron/root)

Oct 22 20:23:02 weekend110 CROND[1933]: (root) CMD (echo 'date')

Oct 22 20:24:01 weekend110 CROND[1949]: (root) CMD (echo 'date')

^C

You have new mail in /var/spool/mail/root

[root@weekend110 ~]# tail -f /var/spool/mail/root

X-Cron-Env: <SHELL=/bin/sh>

X-Cron-Env: <HOME=/root>

X-Cron-Env: <PATH=/usr/bin:/bin>

X-Cron-Env: <LOGNAME=root>

X-Cron-Env: <USER=root>

Message-Id: <20161022122502.14D1C41D08@weekend110.localdomain>

Date: Sat, 22 Oct 2016 20:25:02 +0800 (CST)

date

^C

You have mail in /var/spool/mail/root

[root@weekend110 ~]# service crond status

crond (pid 1624) is running...

[root@weekend110 ~]#

  

Linux shell編程入門

 

Linux shell編程入門

 

[root@weekend110 shell]# ps

PID TTY TIME CMD

1861 pts/0 00:00:00 bash

2021 pts/0 00:00:00 ps

You have new mail in /var/spool/mail/root

[root@weekend110 shell]# jps

2023 Jps

[root@weekend110 shell]# ps -ef|grep java

root 2033 1861 0 20:29 pts/0 00:00:00 grep java

[root@weekend110 shell]# cd /tmp/hsperfdata_root/

You have new mail in /var/spool/mail/root

[root@weekend110 hsperfdata_root]# ll

total 0

[root@weekend110 hsperfdata_root]# /home/hadoop/app/zookeeper-3.4.6/bin/zkServer.sh start

JMX enabled by default

Using config: /home/hadoop/app/zookeeper-3.4.6/bin/../conf/zoo.cfg

Starting zookeeper ... STARTED

You have new mail in /var/spool/mail/root

[root@weekend110 hsperfdata_root]# jps

2075 Jps

2063 QuorumPeerMain

[root@weekend110 hsperfdata_root]# ps -ef|grep java

root 2063 1 51 20:31 pts/0 00:00:04 /home/hadoop/app/jdk1.7.0_65/bin/java -Dzookeeper.log.dir=. -Dzookeeper.root.logger=INFO,CONSOLE -cp /home/hadoop/app/zookeeper-3.4.6/bin/../build/classes:/home/hadoop/app/zookeeper-3.4.6/bin/../build/lib/*.jar:/home/hadoop/app/zookeeper-3.4.6/bin/../lib/slf4j-log4j12-1.6.1.jar:/home/hadoop/app/zookeeper-3.4.6/bin/../lib/slf4j-api-1.6.1.jar:/home/hadoop/app/zookeeper-3.4.6/bin/../lib/netty-3.7.0.Final.jar:/home/hadoop/app/zookeeper-3.4.6/bin/../lib/log4j-1.2.16.jar:/home/hadoop/app/zookeeper-3.4.6/bin/../lib/jline-0.9.94.jar:/home/hadoop/app/zookeeper-3.4.6/bin/../zookeeper-3.4.6.jar:/home/hadoop/app/zookeeper-3.4.6/bin/../src/java/lib/*.jar:/home/hadoop/app/zookeeper-3.4.6/bin/../conf: -Dcom.sun.management.jmxremote -Dcom.sun.management.jmxremote.local.only=false org.apache.zookeeper.server.quorum.QuorumPeerMain /home/hadoop/app/zookeeper-3.4.6/bin/../conf/zoo.cfg

root 2086 1861 0 20:32 pts/0 00:00:00 grep java

[root@weekend110 hsperfdata_root]# ll

total 32

-rw-------. 1 root root 32768 Oct 22 20:31 2063

[root@weekend110 hsperfdata_root]# rm 2063

rm: remove regular file `2063'? y

[root@weekend110 hsperfdata_root]# ll

total 0

[root@weekend110 hsperfdata_root]# jps

2100 Jps

 

 

Linux shell編程入門

 

 

Linux shell編程入門

 

[root@weekend110 shell]# cat echo.sh 

#!/bin/bash

function cutime(){

echo "current time is 'date +%Y-%m-%d'"

}

clear

echo

echo -e "\t\t\tSys admin menu"

echo -e "\t1.get current time"

echo -e "\t2.ls command"

echo -en "\t\tenter option:"

read option

case $option in

1)

cutime

;;

2)

ls

;;

*)

clear

echo "sorry wrong option"

;;

esac

[root@weekend110 shell]# echo.sh

Sys admin menu

1.get current time

2.ls command

enter option:

 

 

 

Linux shell編程入門

 

[root@weekend110 shell]# echo.sh

Sys admin menu

1.get current time

2.ls command

enter option:1

current time is 'date +%Y-%m-%d'

[root@weekend110 shell]# echo.sh

Sys admin menu

1.get current time

2.ls command

enter option:2

arr1.sh   a.txt   break2.sh   continue.sh   fun.sh    nohup.out    zookeeper.out

arr2.sh    break1.sh   break3.sh    echo.sh    funtest.sh   sleep.sh

[root@weekend110 shell]#

相關文章
相關標籤/搜索