使用ls命令一次显示文件数

时间:2014-11-19 22:14:14

标签: bash

我有一个bash脚本,要求此人首先选择显示当前目录中文件的顺序。接下来,我让他们从使用select循环创建的列表中选择一个文件。在他们选择文件后,它会询问他们想要使用哪种类型的命令。我的问题是我想一次只向用户显示一定数量的文件。假设我想一次列出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

1 个答案:

答案 0 :(得分:0)

您最好的解决方案是将文件列表读入一个数组,然后实现一个简单的寻呼机。然后,您可以使用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