• Shell脚本精选 规格严格


    This section on shell programming, is a brief introduction to shell programming, and only talks about the bash shell. For more complete information, refer to "The CTDP Linux Programmer's Guide".

    Linux Variables

    When variables are used they are referred to with the $ symbol in front of them. There are several useful variables available in the shell program. Here are a few:

    • $$ = The PID number of the process executing the shell.
    • $? = Exit status variable.
    • $0 = The name of the command you used to call a program.
    • $1 = The first argument on the command line.
    • $2 = The second argument on the command line.
    • $n = The nth argument on the command line.
    • $* = All the arguments on the command line.
    • $# The number of command line arguments.

    The "shift" command can be used to shift command line arguments to the left, ie $1 becomes the value of $2, $3 shifts into $2, etc. The command, "shift 2" will shift 2 places meaning the new value of $1 will be the old value of $3 and so forth.

    Iteration, control and if statements

    • if - Used to execute one or more statements on a condition. An example:
      if [ ! -d /mnt ]		# be sure the directory /mnt exists
      then
      mkdir /mnt
      fi
    • case - Used to execute specific commands based on the value of a variable. An example:
      case $NUM of
      1)
      echo The number is 1
      ;;
      2)
      echo The number is 2
      ;;
      *)
      echo The number is not 1 or 2
      ;;
      esac
    • for - Used to loop for all cases of a condition. In the example below, it is used to copy all files found in /mnt/floppy to the /etc directory. The lines were numbered for reference with descriptions:
      1. The for loop statement will loop until all files have been found.
      2. A test to be sure the file is a normal file and not a directory.
      3. A comment line.
      4. This line extracts the name of the file from its full path pointed to by the variable $i and puts it in the variable $filename. The method used here is called parameter expansion and is documented in the bash man page. For more information on parameter expansion read the "Linux Programmer's Guide".
      5. This line sends a statement to the standard output, telling what file is being copied.
      6. This line performs the copy command using the -p option to preserve file attributes. Note: Much ability to perform script programming is couched in the ability to know and use the various commands, programs and tools available in Linux rather than a strict understanding of syntax. This is obvious to anyone who reads the system startup script files in /etc/rc.d and associated directories.
      7. This line ends the if statement.
      8. This line ends the for statement.
      1.  for i in /mnt/floppy/*; do
      2. if [ -f $i ]; then
      3. # if the file is there
      4. filename=${i#/mnt/floppy/}
      5. echo copying $i to /etc/$filename
      6. cp -p $i /etc/$filename
      7. fi
      8. done
    • until - Cycles through a loop until some condition is met. The syntax for the command is shown below:
      until [ expression ]
      do
      statements
      done
    • while - Cycles through a loop while some condition is met. The below example will cycle through a loop forever:
      while [ 1 ]
      do
      statement(s)
      done

    Tests

    There is a function provided by bash called test which returns a true or false value depending on the result of the tested expression. Its syntax is:

    test expression

    It can also be implied as follows:

    [ expression ]

    The tests below are test conditions provided by the shell:

    • -b file = True if the file exists and is block special file.
    • -c file = True if the file exists and is character special file.
    • -d file = True if the file exists and is a directory.
    • -e file = True if the file exists.
    • -f file = True if the file exists and is a regular file
    • -g file = True if the file exists and the set-group-id bit is set.
    • -k file = True if the files' "sticky" bit is set.
    • -L file = True if the file exists and is a symbolic link.
    • -p file = True if the file exists and is a named pipe.
    • -r file = True if the file exists and is readable.
    • -s file = True if the file exists and its size is greater than zero.
    • -s file = True if the file exists and is a socket.
    • -t fd = True if the file descriptor is opened on a terminal.
    • -u file = True if the file exists and its set-user-id bit is set.
    • -w file = True if the file exists and is writable.
    • -x file = True if the file exists and is executable.
    • -O file = True if the file exists and is owned by the effective user id.
    • -G file = True if the file exists and is owned by the effective group id.
    • file1 –nt file2 = True if file1 is newer, by modification date, than file2.
    • file1 ot file2 = True if file1 is older than file2.
    • file1 ef file2 = True if file1 and file2 have the same device and inode numbers.
    • -z string = True if the length of the string is 0.
    • -n string = True if the length of the string is non-zero.
    • string1 = string2 = True if the strings are equal.
    • string1 != string2 = True if the strings are not equal.
    • !expr = True if the expr evaluates to false.
    • expr1 –a expr2 = True if both expr1 and expr2 are true.
    • expr1 –o expr2 = True is either expr1 or expr2 is true.

    The syntax is :

    arg1 OP arg2

    where OP is one of –eq, -ne, -lt, -le, -gt, or –ge. Arg1 and arg2 may be positive or negative integers or the special expression "–l string" which evaluates to the length of string.

    An example script

    The file below is an example file which demonstrates some of the testing as talked above along with several looping and control statements.

    #! /bin/bash	
    # Use the bash shell to run the script
    # This is an example file to take entries from the user
    # entries Version 1.0 May 22, 2000
    DONE=no
    ENTRIES="hello bye ls 1"
    while [ $DONE = no ]
    do
    echo Valid entries are: $ENTRIES
    read ENTRY # Read the variable ENTRY from the user
    case $ENTRY in
    1)
    pwd
    ;;
    hello)
    echo How are you?
    ;;
    bye)
    echo exiting...
    DONE=yes
    ;;
    ls)
    ls -al |more
    ;;
    *)
    echo $ENTRY is an unrecognized command.
    ;;
    esac
    done

    gdb debugger commands

    See the gdb(1) man page for more information on the gdb program. Below are listed some commands:

    • file - Loads the exe file.
    • kill - Terminates the program being debugged.
    • list - List sections of the source code.
    • next - Advances one line of source code in the current function and steps into other functions.
    • run - Executes the program that is being debugged.
    • quit - Ends gdb.
    • watch - Enables you to tell the value of a variable when it changes.
    • break - Set a break point.
    • make - Remake the exe program without quitting gdb.
    • shell - Run a UNIX shell command without leaving gdb.
  • 相关阅读:
    Oracle:connect by用法
    spring boot:分别在jar内部和外部使用配置文件(spring boot v2.5.4)
    spring boot:tomcat的accesslog按日期存放(spring boot v2.5.4)
    java 15:用jstack查看线程信息
    java 15:配置gc log
    mybatis: foreach实现in集合查询(mybatis 3.5.7)
    java 15: 查看jdk默认的gc和正在使用的gc
    spring boot: 定时任务处理订单(spring boot v2.5.4)
    java 15: jstat查看gc状态
    java 15: jstat查看类或编译器信息
  • 原文地址:https://www.cnblogs.com/diyunpeng/p/1614572.html
Copyright © 2020-2023  润新知