簡體   English   中英

使用ls命令一次顯示文件數

[英]Display number of files at a time using the ls command

我有一個bash腳本,要求該人首先選擇顯示當前目錄中文件的順序。 接下來,讓他們從使用選擇循環創建的列表中選擇一個文件。 他們選擇文件后,會詢問他們要使用哪種命令。 我的問題是我想一次只向用戶顯示一定數量的文件。 假設我要一次列出23個文件。 我該怎么做?

對不起,如果馬虎

    ORDER=("name" "age" "size")
    select OPT4 in "${ORDER[@]}"
    do
            if [[ $OPT4 == "name" ]]
            then
                    ARRAY=( $( ls . ) )
    #               select OPT2 in "${ARRAY[@]}"
    #               do
    #                       echo $OPT2
    #               done
            fi
            if [[ $OPT4 == "age" ]]
            then
                    ARRAY=( $( ls -rt ) )
    #               select OPT2 in "${ARRAY[@]}"
    #               do
    #                       echo $OPT2
    #               done
            fi
            if [[ $OPT4 == "size" ]]
            then
                    ARRAY=( $( ls -S ) )
    #               select OPT2 in "${ARRAY[@]}"
    #               do
    #                       echo $OPT2
    #               done
            fi
    echo $OPT4
    #ARRAY=( $( ls -S ) )
    select OPT2 in "${ARRAY[@]}"
    do
            OPTIONS=("author" "type" "copy" "ren" "move" "del" "copy!" "ren!" "move!" "help")
            select OPT1 in "${OPTIONS[@]}"
            do
    if [[ $OPT1 == "copy!" || $OPT1 == "move!" || $OPT1 == "ren!" ]]
                    then
                            select OPT3 in "${ARRAY[@]}"
                            do
                            echo $OPT3
                            break
                            done
                    fi
                    if [[ $OPT1 == "copy" || $OPT == "move" || $OPT1 == "ren" ]]
                    then
                            echo -n "Enter a file destination: "
                            read OPT3
                    fi
                    case $OPT1 in
                            $AUTHOR)
                                    echo  "Last, First";;
                            $TYPE)
                                    exist
                                    file
                                    order66;;
                            $COPY)
                                    exist
                                    file
                                    order66;;
                            $RENAME)
                                    # mv &>/dev/null $2 $3;;
                                    exist
                                    file
                                    order66;;
                            $MOVE)
                                    # mv &>/dev/null $2 $3;;
                                    exist
                                    file
                                    order66;;
                            $DELETE)
                                    # rm -f &>/dev/null $2;;
                                    exist
                                    file
                                    order66;;
                            $FORCE_COPY)
                                exist
                                file
                                order67;;
                        $FORCE_MOVE)
                                exist
                                file
                                order67;;
                        $FORCE_RENAME)
                                exist
                                file
                                order67;;
                        $HELP)
                                echo -e $MESSAGE;;
                        *)
                                echo -e $MESSAGE;;
                esac
                exit
        done
done
done

最好的解決方案是將文件列表讀入數組,然后實現一個簡單的尋呼機。 然后,您可以使用c-style for loop來跟蹤文件索引並根據循環索引的值進行操作。 下面顯示了尋呼機的實現。 您將要對索引執行的操作只是添加到read語句中(例如: read -p "(#)Select File (f)orward (b)ack : " ans ),並包含else以處理# )。 我在分頁器示例下面發布了用於文件名選擇的其他代碼。

每頁顯示的文件數由pg_size變量控制。 根據需要進行調整。 同樣,您可以通過調整填充文件數組的find語句來控制是否遞歸讀取文件:

#!/bin/bash

declare -i pg_size=4

file_array=( $(find "$1" -maxdepth 1 -type f) )

for ((i=0; i<${#file_array[@]}; i++)); do

    echo "  ${i}.   ${file_array[$i]}"

    if [ "$i" -gt 0 -a $(((i+1)%pg_size)) -eq 0 ]; then
        read -p "(f)orward (b)ack : " ans
        if [ "$ans" = 'b' ]; then
            [ "$i" -gt "$((pg_size))" ] && ((i = i - (2*pg_size))) || ((i = i - pg_size))
        fi
    fi

done

輸出示例:

$ ./lspager.sh tmp
  0.   tmp/File1950text.doc
  1.   tmp/vcs1dump
  2.   tmp/File2014text.xls
  3.   tmp/File307list.cvs
(f)orward (b)ack : b
  0.   tmp/File1950text.doc
  1.   tmp/vcs1dump
  2.   tmp/File2014text.xls
  3.   tmp/File307list.cvs
(f)orward (b)ack : f
  4.   tmp/vcsa1dump
  5.   tmp/File256name.txt
  6.   tmp/README.txt
  7.   tmp/dl
(f)orward (b)ack : b
  0.   tmp/File1950text.doc
  1.   tmp/vcs1dump
  2.   tmp/File2014text.xls
  3.   tmp/File307list.cvs
(f)orward (b)ack : f
  4.   tmp/vcsa1dump
  5.   tmp/File256name.txt
  6.   tmp/README.txt
  7.   tmp/dl
(f)orward (b)ack : f
  8.   tmp/File1949text.doc
  9.   tmp/vcsadump
  10.   tmp/vcsdump
  11.   tmp/helloworld.txt
(f)orward (b)ack : f
  12.   tmp/File1951text.dat

要實現文件名選擇,只需添加到read並實現測試以驗證范圍內的數字答案並突破循環:

#!/bin/bash

declare -i pg_size=4

file_array=( $(find "$1" -maxdepth 1 -type f) )

for ((i=0; i<${#file_array[@]}; i++)); do

    echo "  ${i}.   ${file_array[$i]}"

    if [ "$i" -gt 0 -a $(((i+1)%pg_size)) -eq 0 ]; then
        read -p "(#)Select file (f)orward (b)ack : " ans
        if [ "$ans" = 'b' ]; then
            [ "$i" -gt "$((pg_size))" ] && ((i = i - (2*pg_size))) || ((i = i - pg_size))
        elif [[ "$ans" =~ [0-9] ]]; then  # note character class, [[, and =~ are bash only
            [ "$ans" -le "$i" ] && echo "You chose '$ans' - ${file_array[$ans]}" && break
        fi
    fi

done

輸出:

$ ./lspager.sh tmp
  0.   tmp/File1950text.doc
  1.   tmp/vcs1dump
  2.   tmp/File2014text.xls
  3.   tmp/File307list.cvs
(#)Select file (f)orward (b)ack : f
  4.   tmp/vcsa1dump
  5.   tmp/File256name.txt
  6.   tmp/README.txt
  7.   tmp/dl
(#)Select file (f)orward (b)ack : f
  8.   tmp/File1949text.doc
  9.   tmp/vcsadump
  10.   tmp/vcsdump
  11.   tmp/helloworld.txt
(#)Select file (f)orward (b)ack : b
  4.   tmp/vcsa1dump
  5.   tmp/File256name.txt
  6.   tmp/README.txt
  7.   tmp/dl
(#)Select file (f)orward (b)ack : 5
You chose '5' - tmp/File256name.txt

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM