基礎指令操作

  1. date:顯示日期及時間。
    [dywang@dywHome2 ~]$ date --help
    Usage: date [OPTION]... [+FORMAT]
      or:  date [-u|--utc|--universal] [MMDDhhmm[[CC]YY][.ss]]
    Display the current time in the given FORMAT, or set the system date.
    
      -d, --date=STRING         display time described by STRING, not `now'
                             ## 以 STRING 格式顯示時間,不是'現在'時間。			
      -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
          --rfc-3339=TIMESPEC   output date and time in RFC 3339 format.
                                TIMESPEC=`date', `seconds', or `ns' for
                                date and time to the indicated precision.
      -s, --set=STRING          set time described by STRING
                             ## 以 STRING 格式設定時間。		
      -u, --utc, --universal    print or set  
                             ## 顯示目前的格林威治時間。		
          --help     display this help and exit
          --version  output version information and exit
    
    FORMAT controls the output.  The only valid option for the second form
    specifies Coordinated Universal Time.  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., 21)
      %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)
    
    時間方面 : 
     
     %% : 印出 % 
     %n : 下一行 
     %t : 跳格 
     %H : 小時(00..23) 
     %I : 小時(01..12) 
     %k : 小時(0..23) 
     %l : 小時(1..12) 
     %M : 分鐘(00..59) 
     %p : 顯示本地 AM 或 PM 
     %r : 直接顯示時間 (12 小時制,格式為 hh:mm:ss [AP]M) 
     %s : 以 1970 年 1 月 1 日 00:00:00 UTC 到目前為止的秒數 
     %S : 秒(00..61) 
     %T : 直接顯示時間 (24 小時制) 
     %X : 相當於 %H:%M:%S 
     %Z : 顯示時區 
     
     日期方面 : 
     %a : 星期幾 (Sun..Sat) 
     %A : 星期幾 (Sunday..Saturday) 
     %b : 月份 (Jan..Dec) 
     %B : 月份 (January..December) 
     %c : 直接顯示日期與時間 
     %d : 日 (01..31) 
     %D : 直接顯示日期 (mm/dd/yy) 
     %h : 同 %b 
     %j : 一年中的第幾天 (001..366) 
     %m : 月份 (01..12) 
     %U : 一年中的第幾周 (00..53) (以 Sunday 為一周的第一天的情形) 
     %w : 一周中的第幾天 (0..6) 
     %W : 一年中的第幾周 (00..53) (以 Monday 為一周的第一天的情形) 
     %x : 直接顯示日期 (mm/dd/yy) 
     %y : 年份的最後兩位數字 (00.99) 
     %Y : 完整年份 (0000..9999) 
     
    
    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.
    
    Report bugs to <bug-coreutils@gnu.org>.
    
    [dywang@mdk-dyw ~]$ date +%Y/%m/%d
    2006/10/05
    
    [dywang@dywOffice ~]$ date +%H:%M
    09:37
    
    [dywang@dywHome2 ~]$  # 顯示時間後跳行,再顯示目前日期: 
    [dywang@dywHome2 ~]$ date '+%T%n%D'
    18:16:19
    08/01/09
    
    [dywang@dywHome2 ~]$  # 顯示月份與日
    [dywang@dywHome2 ~]$ date '+%B %d'
    August 01
    
    [dywang@dywHome2 ~]$  # 明天
    [dywang@dywHome2 ~]$ date --date="1 day" +%F
    2009-08-02
    
    [dywang@dywHome2 ~]$  # 後天
    [dywang@dywHome2 ~]$  date --date="2 day" +%F
    2009-08-03
    
    [dywang@dywHome2 ~]$  # 昨天
    [dywang@dywHome2 ~]$  date --date="1 day ago" +%F
    2009-07-31
    
    [dywang@dywHome2 ~]$  # 前天
    [dywang@dywHome2 ~]$  date --date="2 day ago" +%F
    2009-07-30
    
    [dywang@dywHome2 ~]$  # 2009-1-1 的前一天
    [dywang@dywHome2 ~]$  date --date="2009-1-1 1 day ago" +%F
    2008-12-31
    
    [dywang@dywHome2 ~]$  # 2008-2-28 的後一天
    [dywang@dywHome2 ~]$  date --date="2008-2-28 1 day" +%F
    2008-02-29
    
    [dywang@dywHome2 ~]$  # 2008-2-28的兩個月前
    [dywang@dywHome2 ~]$  date --date="2008-2-28 2 month ago" +%F
    2007-12-28
    
    [root@dywHome2 ~]#  # 設定時間為 12:34:56,必須以 root 身份
    [root@dywHome2 ~]# date -s '12:34:56'
    Sat Aug  1 12:34:56 CST 2009
    
  2. cal:顯示日月曆。
    [dywang@dywHome2 ~]$ cal --help
    cal: invalid option -- -
    usage: cal [-13smjyV] [[month] year]
    
    [dywang@mdk-dyw ~]$ man cal
    CAL(1)                    BSD General Commands Manual                   CAL(1)
    
    NAME
         cal - displays a calendar
    
    SYNOPSIS
         cal [-smjy13] [[month] year]
    
    DESCRIPTION
         Cal displays a simple calendar.  If arguments are not specified, the cur-
         rent month is displayed.  The options are as follows:
    
         -1      Display single month output.  (This is the default.)
    
         -3      Display prev/current/next month output.
    
         -s      Display Sunday as the first day of the week.  (This is the
                 default.)
    
         -m      Display Monday as the first day of the week.
    
         -j      Display Julian dates (days one-based, numbered from January 1).
    
         -y      Display a calendar for the current year.
    
         A single parameter specifies the year (1 - 9999) to be displayed; note
         the year must be fully specified: “cal 89” will not display a calendar
         for 1989.  Two parameters denote the month (1 - 12) and year.  If no
         parameters are specified, the current month's calendar is displayed.
    
         A year starts on Jan 1.
    
         The Gregorian Reformation is assumed to have occurred in 1752 on the 3rd
         of September.  By this time, most countries had recognized the reforma-
         tion (although a few did not recognize it until the early 1900's.)  Ten
         days following that date were eliminated by the reformation, so the cal-
         endar for that month is a bit unusual.
    
    HISTORY
         A cal command appeared in Version 6 AT&T UNIX.
    
    OTHER VERSIONS
         Several much more elaborate versions of this program exist, with support
         for colors, holidays, birthdays, reminders and appointments, etc. For
         example, try the cal from http://home.sprynet.com/~cbagwell/projects.html
         or GNU gcal.
    
    BSD                              June 6, 1993                              BSD
    
    [dywang@dywHome2 ~]$ cal
        August 2009
    Su Mo Tu We Th Fr Sa
                       1
     2  3  4  5  6  7  8
     9 10 11 12 13 14 15
    16 17 18 19 20 21 22
    23 24 25 26 27 28 29
    30 31
    [dywang@dywHome2 ~]$ cal -1
        August 2009
    Su Mo Tu We Th Fr Sa
                       1
     2  3  4  5  6  7  8
     9 10 11 12 13 14 15
    16 17 18 19 20 21 22
    23 24 25 26 27 28 29
    30 31
    [dywang@dywHome2 ~]$ cal -3
         July 2009            August 2009          September 2009
    Su Mo Tu We Th Fr Sa  Su Mo Tu We Th Fr Sa  Su Mo Tu We Th Fr Sa
              1  2  3  4                     1         1  2  3  4  5
     5  6  7  8  9 10 11   2  3  4  5  6  7  8   6  7  8  9 10 11 12
    12 13 14 15 16 17 18   9 10 11 12 13 14 15  13 14 15 16 17 18 19
    19 20 21 22 23 24 25  16 17 18 19 20 21 22  20 21 22 23 24 25 26
    26 27 28 29 30 31     23 24 25 26 27 28 29  27 28 29 30
                          30 31
    [dywang@dywHome2 ~]$ cal -s
        August 2009
    Su Mo Tu We Th Fr Sa
                       1
     2  3  4  5  6  7  8
     9 10 11 12 13 14 15
    16 17 18 19 20 21 22
    23 24 25 26 27 28 29
    30 31
    [dywang@dywHome2 ~]$ cal -m
        August 2009
    Mo Tu We Th Fr Sa Su
                    1  2
     3  4  5  6  7  8  9
    10 11 12 13 14 15 16
    17 18 19 20 21 22 23
    24 25 26 27 28 29 30
    31
    [dywang@dywHome2 ~]$ cal -j
            August 2009
    Sun Mon Tue Wed Thu Fri Sat
                            213
    214 215 216 217 218 219 220
    221 222 223 224 225 226 227
    228 229 230 231 232 233 234
    235 236 237 238 239 240 241
    242 243
    [dywang@dywHome2 ~]$ cal -y
                                   2009
    
           January               February                 March
    Su Mo Tu We Th Fr Sa   Su Mo Tu We Th Fr Sa   Su Mo Tu We Th Fr Sa
                 1  2  3    1  2  3  4  5  6  7    1  2  3  4  5  6  7
     4  5  6  7  8  9 10    8  9 10 11 12 13 14    8  9 10 11 12 13 14
    11 12 13 14 15 16 17   15 16 17 18 19 20 21   15 16 17 18 19 20 21
    18 19 20 21 22 23 24   22 23 24 25 26 27 28   22 23 24 25 26 27 28
    25 26 27 28 29 30 31                          29 30 31
    
            April                   May                   June
    Su Mo Tu We Th Fr Sa   Su Mo Tu We Th Fr Sa   Su Mo Tu We Th Fr Sa
              1  2  3  4                   1  2       1  2  3  4  5  6
     5  6  7  8  9 10 11    3  4  5  6  7  8  9    7  8  9 10 11 12 13
    12 13 14 15 16 17 18   10 11 12 13 14 15 16   14 15 16 17 18 19 20
    19 20 21 22 23 24 25   17 18 19 20 21 22 23   21 22 23 24 25 26 27
    26 27 28 29 30         24 25 26 27 28 29 30   28 29 30
                           31
            July                  August                September
    Su Mo Tu We Th Fr Sa   Su Mo Tu We Th Fr Sa   Su Mo Tu We Th Fr Sa
              1  2  3  4                      1          1  2  3  4  5
     5  6  7  8  9 10 11    2  3  4  5  6  7  8    6  7  8  9 10 11 12
    12 13 14 15 16 17 18    9 10 11 12 13 14 15   13 14 15 16 17 18 19
    19 20 21 22 23 24 25   16 17 18 19 20 21 22   20 21 22 23 24 25 26
    26 27 28 29 30 31      23 24 25 26 27 28 29   27 28 29 30
                           30 31
           October               November               December
    Su Mo Tu We Th Fr Sa   Su Mo Tu We Th Fr Sa   Su Mo Tu We Th Fr Sa
                 1  2  3    1  2  3  4  5  6  7          1  2  3  4  5
     4  5  6  7  8  9 10    8  9 10 11 12 13 14    6  7  8  9 10 11 12
    11 12 13 14 15 16 17   15 16 17 18 19 20 21   13 14 15 16 17 18 19
    18 19 20 21 22 23 24   22 23 24 25 26 27 28   20 21 22 23 24 25 26
    25 26 27 28 29 30 31   29 30                  27 28 29 30 31
    
    
    [dywang@dywHome2 ~]$ cal -m3 9 2012
        August 2012          September 2012         October 2012
    Mo Tu We Th Fr Sa Su  Mo Tu We Th Fr Sa Su  Mo Tu We Th Fr Sa Su
           1  2  3  4  5                  1  2   1  2  3  4  5  6  7
     6  7  8  9 10 11 12   3  4  5  6  7  8  9   8  9 10 11 12 13 14
    13 14 15 16 17 18 19  10 11 12 13 14 15 16  15 16 17 18 19 20 21
    20 21 22 23 24 25 26  17 18 19 20 21 22 23  22 23 24 25 26 27 28
    27 28 29 30 31        24 25 26 27 28 29 30  29 30 31
    

  3. bc:簡單好用的計算機。
    [dywang@dywHome2 ~]$ bc --help
    usage: bc [options] [file ...]
      -h  --help         print this usage and exit
      -i  --interactive  force interactive mode
      -l  --mathlib      use the predefine math routnes
      -q  --quiet        don't print initial banner
      -s  --standard     non-standard bc constructs are errors
      -w  --warn         warn about non-standard bc constructs
      -v  --version      print version information and exit
    
    [dywang@mdk-dyw ~]$ man bc
    ## 以下只整理使用重點,完整使用手冊請自行參考 man bc。
    
       NUMBERS
           ...省略... The length is the total number of significant  decimal digits
           in a number and the scale is the total number of decimal  digits  after 
           the decimal point.  For example:
                   .000001 has a length of 6 and scale of 6.
                   1935.000 has a length of 7 and a scale of 3.
    
       VARIABLES
           ...省略... All  letters  must  be  lower case.
           ...省略... The default value of scale is 0.
    
       COMMENTS
           Comments in bc start with the characters /* and end with the characters
           */. ...省略... A single line comment starts at a # character
           and continues to the next end of the line. ...省略... 
    
       EXPRESSIONS
           ...省略... 
    
           - expr The result is the negation of the expression.
    
           ++ var The variable is incremented by one and  the  new  value  is  the
                  result of the expression.
    
           -- var The  variable  is  decremented  by  one and the new value is the
                  result of the expression.
    
           var ++  The result of the expression is the value of the  variable  and
                  then the variable is incremented by one.
    
           var -- The  result  of  the expression is the value of the variable and
                  then the variable is decremented by one.
    
           expr + expr
                  The result of the expression is the sum of the two  expressions.
    
           expr - expr
                  The  result  of  the  expression  is  the  difference of the two
                  expressions.
    
           expr * expr
                  The result of the expression is the product of the  two  expres-
                  sions.
    
           expr / expr
                  The  result of the expression is the quotient of the two expres-
                  sions.  The scale of the result is the  value  of  the  variable
                  scale.
    
           expr % expr
                  The  result  of the expression is the "remainder". ...省略... 
    
           expr ^ expr
                  The result of the expression is the value of the first raised to
                  the  second.  The second expression must be an integer. ...省略... 
    
           ( expr )
                  This alters the standard precedence to force the  evaluation  of
                  the expression.
    
           var = expr
                  The variable is assigned the value of the expression.
    
           var <op>= expr
                  This  is  equivalent to "var = var <op> expr" ...省略... 
    
           ...省略... relational operators are:
    
           expr1 < expr2
                  The result is 1 if expr1 is strictly less than expr2.
    
           expr1 %*<== expr2*)
                  The result is 1 if expr1 is less than or equal to expr2.
    
           expr1 > expr2
                  The result is 1 if expr1 is strictly greater than expr2.
    
           expr1 >= expr2
                  The result is 1 if expr1 is greater than or equal to expr2.
    
           expr1 == expr2
                  The result is 1 if expr1 is equal to expr2.
    
           expr1 != expr2
                  The result is 1 if expr1 is not equal to expr2.
    
           ...省略...  The  boolean  operators are:
    
           !expr  The result is 1 if expr is 0.
    
           expr && expr
                  The result is 1 if both expressions are non-zero.
    
           expr || expr
                  The result is 1 if either expression is non-zero.
    
           The expression precedence is as follows: (lowest to highest)
                  || operator, left associative
                  && operator, left associative
                  ! operator, nonassociative
                  Relational operators, left associative
                  Assignment operator, right associative
                  + and - operators, left associative
                  *, / and % operators, left associative
                  ^ operator, right associative
                  unary - operator, nonassociative
                  ++ and -- operators, nonassociative
    
           ...省略... 
    
           length ( expression )
                  The  value  of  the length function is the number of significant
                  digits in the expression.
    
           read ( )
                  The read function (an extension) will read  a  number  from  the
                  standard   input,  ...省略... 
    
           scale ( expression )
                  The value of the scale function is the number  of  digits  after
                  the decimal point in the expression.
    
           sqrt ( expression )
                  The value of the sqrt function is the square root of the expres-
                  sion.  If the expression is negative, a run time error is gener-
                  ated.
    
       STATEMENTS
           ...省略... 
    
          { statement_list }
                  This is the compound statement.  It allows  multiple  statements
                  to be grouped together for execution.
    
          if ( expression ) statement1 [else statement2]
                  ...省略... 
                  If  the  expression  is  non-zero,  statement1  is executed.  If
                  statement2 is present and the value of the expression is 0, then
                  statement2 is executed.
    
           while ( expression ) statement
                  The while statement will execute the statement while the expres-
                  sion is non-zero.  ...省略... 
    
           for ( [expression1] ; [expression2] ; [expression3] ) statement
                  The for statement controls repeated execution of the  statement.
                  ...省略... 
    
    
           break  This statement causes a forced exit of the most recent enclosing
                  while statement or for statement.
    
           continue
                  The continue statement (an extension)  causes  the  most  recent
                  enclosing for statement to start the next iteration.
    
           halt   ...省略... 
    
           return Return the value 0 from a function.  (See the section  on  func-
                  tions.)
    
           return ( expression )
                  Return  the  value  of the expression from a function. ...省略... 
    
       PSEUDO STATEMENTS
           ...省略... 
    
       FUNCTIONS
           ...省略...   A function is defined as follows:
                  define name ( parameters ) { newline
                      auto_list   statement_list }
           A function call is just an expression of the form "name(parameters)".
    
           The  auto_list  is  an  optional list of variables that are for "local"
           use.  The syntax of the auto list (if present) is "auto name,  ...  ;".
           ...省略...  The variables are then initialized to  zero ...省略...   
    
           For example, the following definitions are legal.
                  define d (n) { return (2*n); }
                  define d (n)
                    { return (2*n); }
    
       MATH LIBRARY
           ...省略... 
    
           s (x)  The sine of x, x is in radians.
    
           c (x)  The cosine of x, x is in radians.
    
           a (x)  The arctangent of x, arctangent returns radians.
    
           l (x)  The natural logarithm of x.
    
           e (x)  The exponential function of raising e to the value x.
    
           j (n,x)
                  The bessel function of integer order n of x.
    
       EXAMPLES
           ...省略... 
    
           範例:定義階層 x! 函數
                  define f (x) {
                    if (x %*<== 1) return (1);*)
                    return (f(x-1) * x);
                  }
    
       READLINE AND LIBEDIT OPTIONS
           ...省略... 
    
       DIFFERENCES
           ...省略... 
    
       LIMITS
           ...省略... 
    
    ENVIRONMENT VARIABLES
            ...省略... 
    
    DIAGNOSTICS
           ...省略... 
    
    BUGS
           ...省略... 
    
    AUTHOR
           Philip A. Nelson
           philnelson@acm.org
    
    ACKNOWLEDGEMENTS
           The author would like to thank  Steve  Sommars  (Steve.Sommars@att.com)
           for  his extensive help in testing the implementation.  Many great sug-
           gestions were given.  This is a much better product due to his involve-
           ment.
    
    
    
                                           .                                 bc(1)
    
    [dywang@mdk-dyw ~]$ bc
    bc 1.06
    Copyright 1991-1994, 1997, 1998, 2000 Free Software Foundation, Inc.
    This is free software with ABSOLUTELY NO WARRANTY.
    For details type `warranty'.
    (3+4)^2
    49
    4/3
    1
    scale=3
    4/3
    1.333
    
    sacle=0
    x=read()
    100
    for(i=0;i<x;i++) {
    total+=(i+1)
    }
    total
    5050
    
    var1=10
    var2=var1++
    var1;var2
    11
    10
    var2=++var1
    var1;var2
    12
    12
    
    define f (x) {
    if (x %*<== 1) return (1);*)
    return (f(x-1) * x);
    }
    
    f(4)
    24
    

練習題

  1. 如何以指令叫出 2007 年 12 月的日曆?
    Sol. cal 12 2007
  2. 現在於 Linux 系統中,輸入指令 date +%Y/%m/%d,結果為何?
    Sol. 今天的目期,格式如 /2008/09/01
  3. 現在於 Linux 系統中,輸入指令 date +%H:%M,結果為何?
    Sol. 目前的時間,格式如 10:10
  4. 於 Linux 系統中,輸入指令 cal 2008,結果為何?
    Sol. 出現 2008 年的日曆。
  5. 於 Linux 系統中,輸入指令 cal -1,結果為何?
    Sol. 出現本月的日曆。
  6. 於 Linux 系統中,輸入指令 cal -3,結果為何?
    Sol. 出現本月與其前後共三個月的日曆。
  7. 於 Linux 系統中,輸入指令 bc,結果為何?
    Sol. 啟動一個簡單的計算器。
  8. 於 Linux 指令 bc 環境下,預設 4/3 的結果為何?
    Sol. 1
  9. 於 Linux 指令 bc 環境下,要讓 4/3 的結果顯示至小數點三位,要如何設定?
    Sol. scale=3
  10. 於 Linux 指令 bc 環境下,能否執行 C 語言的 for 迴圈?
    Sol. 可以
  11. 於 Linux 指令 bc 環境下先設定變數 var1=4,再執行 var2=–var1 後,var1 及 var2 分別等於?
    Sol. var1=3 var2=3
  12. 於 Linux 指令 bc 環境下先設定變數 var1=4,再執行 var2=var1– 後,var1 及 var2 分別等於?
    Sol. var1=3 var2=4
  13. 於 Linux 指令 bc 環境下先設定變數 var1=4,再執行 var2=++var1 後,var1 及 var2 分別等於?
    Sol. var1=5 var2=5
  14. 於 Linux 指令 bc 環境下先設定變數 var1=4,再執行 var2=var1++ 後,var1 及 var2 分別等於?
    Sol. var1=5 var2=4