shell檔案存在的判斷 shell數組

來源:互聯網
上載者:User

判斷檔案:

    WORKDIR=/home/tmp
    LOCAL_LIST_FILE=$WORKDIR/local.list

    #if no local.txt file, generate a new one
    if [ ! -f  "$LOCAL_LIST_FILE" ]; then
      touch $LOCAL_LIST_FILE
    fi

    注意:一定要有空格,判斷檔案是否存在,如果不存在,就產生一個新的

    if [ ! -d /mnt ]        # be sure the directory /mnt exists
       then
           mkdir /mnt
    fi
   
    目錄。/mnt是否存在,不存在就組建目錄/mnt


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.

Shell中的資料問題

   ArrayName=("element 1" "element 2" "element 3")  #數組定義
   echo ${#ArrayName[@]}


  echo "The number of elements in the array is ${#ArrayName[*]}"    
   
    例子
    #!/bin/bash
    # define array
    # name server names FQDN
    NAMESERVERS=("ns1.nixcraft.net." "ns2.nixcraft.net." "ns3.nixcraft.net.")
 
    # get length of an array
     tLen=${#NAMESERVERS[@]}
 
    # use for loop read all nameservers
    for (( i=0; i<${tLen}; i++ ));
    do
      echo ${NAMESERVERS[$i]}
    done

   Sample output:

     ns1.nixcraft.net.
     ns2.nixcraft.net.
     ns3.nixcraft.net.

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.