First shell script
Program
#!/bin/sh #第一行统一这样写echo ‘abc‘ #输出abc
Run (because the directory is incorrect)
[email protected]:~$ test.shtest.sh: command not found
Programme I
[email protected]:~$ sh test.shabc
Scenario two (no permissions)
[email protected]:~$ ~/test.sh -bash: /home/liuil/test.sh: Permission denied
Scenario two problem resolution (after granting permissions and then executing)
[email protected]:~$ chmod u+x test.sh
Variable
Example
[email protected]:~$ vim test.sh#!/bin/shi=0 #变量名由字母数字下划线组成,不能数字开头echo ‘$i‘ #单引号不解析echo "$i" #双引号解析echo ${i}1234echo $PATH #环境变量echo $(date)echo `date`echo $1 #位置变量echo $2
Results
[email protected]:~$ sh test.sh hello world$i001234/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/gamesFri Jan 2 09:35:27 CST 2015Fri Jan 2 09:35:27 CST 2015
- Positional variables
Use of the DATE function
Show Current Time
[email protected]:~$ date +%Y-%m-%d_%H:%M:%S2015-01-02_19:00:42
Displays the current time and the difference in the number of seconds since 1970-01-01 00:00:00
[email protected]:~$ date +%s1420196538
Specify time output
[email protected]:~$ date --date=‘2009-01-01 11:11:11‘Thu Jan 1 11:11:11 CST 2009[email protected]:~$ date --date=‘3 days ago‘Tue Dec 30 19:04:22 CST 2014
Time format
A%%a of the current locale (for example: Day, representing Sunday)%a the full name of the current locale (e.g., Sunday)%b the month name of the current locale (e.g., one, for January)%b the current locale Full month name (e.g. January)%c current locale date and time (e.g. Thursday, March 3, 2005 23:05:25)%c century; for example,%Y, typically omitting the last two digits of the current year (for example:)%d date by month (ex: £)%d Date by month, equal to%m/%d/%y%e date by month, add space, equal to%_d%f full date format, equivalent to the last two bits of the%y-%m-%d%g ISO-8601 format year (see%G)%G ISO-8601 format year (see%V) , typically only used in conjunction with%V%h equals%b%h hours (00-23)%I Hours (00-12)%c by year date (001-366)%k (0-23)%l (1-12)%m month (01-12)%m min (00-59)%n newline%n nanoseconds (000000000-999999999) %p "Morning" or "afternoon" under the current locale, the output is empty when unknown%p is similar to%p, but the output lowercase letter%R The 12 hour clock time under the current locale (for example: 11:11:04 pm)%R 24 hours time and minutes, equivalent to% h:%m%s the number of seconds since UTC time 1970-01-01 00:00:00%s seconds (00-60)%t output tab tab%t time, equals%h:%m:%s%u week, 1 represents Monday%u year Week, taking Sunday as the first day of the Week (00-53)%V ISO-8601 format specification, the first day of the Year (01-53)%w the day of the Week (0-6), and 0 for Monday%w The first day of the year (00-53) per week, in Monday, in Monday. X Date Description Under current locale (for example: 12/31/99)%x time description under current locale (e.g. 23:13:48)%y year last two digits (00-99)%y year%z +hHmm digital time zone (for example, -0400)%:z +hh:mm Digital time zone (for example, -04:00)%::z +hh:mm:ss Digital time zone (for example, -04:00:00)%:::z Digital time zone with the necessary precision (for example, -04,+05:30)%Z the time zone abbreviation by alphabet (for example, EDT)
Standard input and output
0表示标准输入,1表示标准输出,2表示错误 [email protected]:~$ ls 1>a.txt [email protected]:~$ ls a.txt devProgram Downloads Music Public test.sh Desktop Documents examples.desktop Pictures Templates Videos [email protected]:~$ more a.txt a.txt Desktop devProgram Documents Downloads examples.desktop Music Pictures Public Templates test.sh Videos
Scheduled Tasks
Judgment and circulation
If
[email protected]:~$ vim test.sh#!/bin/shif [ ‘a‘=‘a‘ ]then echo ‘=‘fi[email protected]:~$ sh test.sh =
For (may fail under Ubuntu, requires sudo dpkg-reconfigure dash to select No)
#!/bin/shfor ((i=0;i<3;i++))do echo $idone[email protected]:~$ ~/test.sh 012
Function
- The last line statement is the default return value
When referencing a custom function file, use the source func.sh
#!/bin/shfunction dayAgo(){ date --date="$1 days ago" +%Y-%m-%d}dayAgo 3[email protected]:~$ sh test.sh 2014-12-30
View functions
[email protected]:~$ man dateDATE(1) User Commands DATE(1)NAMEdate - print or set the system date and time
Getting Started with Linux shell