[英]Display number of files at a time using the ls command
I have a bash script that ask the person to first select an order at which the files in the current directory are displayed. 我有一个bash脚本,要求该人首先选择显示当前目录中文件的顺序。 Next I have them choose a file from a list that is created using a select loop.
接下来,让他们从使用选择循环创建的列表中选择一个文件。 After they select a file it will ask them what type of command they would like to use.
他们选择文件后,会询问他们要使用哪种命令。 My issue is that I want to display only a certain number of files to the user at a time.
我的问题是我想一次只向用户显示一定数量的文件。 Let's say I want to list 23 files at a time.
假设我要一次列出23个文件。 How would I do that?
我该怎么做?
Sorry if it is sloppy 对不起,如果马虎
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
Your best solution is to read the list of files into an array, and then implement a simple pager. 最好的解决方案是将文件列表读入数组,然后实现一个简单的寻呼机。 You can then use a
c-style for loop
to keep track of the file indexes and act on the value of the loop index. 然后,您可以使用
c-style for loop
来跟踪文件索引并根据循环索引的值进行操作。 The following shows implementation of the pager. 下面显示了寻呼机的实现。 What you will do to act on the index is just add to the read statement (eg:
read -p "(#)Select File (f)orward (b)ack : " ans
) and include an else
to handle the #
). 您将要对索引执行的操作只是添加到read语句中(例如:
read -p "(#)Select File (f)orward (b)ack : " ans
),并包含else
以处理#
)。 I've posted the additional code for filename selection below the pager example. 我在分页器示例下面发布了用于文件名选择的其他代码。
The number of files shown per-page is controlled with the pg_size
variable. 每页显示的文件数由
pg_size
变量控制。 Adjust as necessary. 根据需要进行调整。 Likewise, you can control whether files are read recursively by adjusting the
find
statement filling the file array: 同样,您可以通过调整填充文件数组的
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
output example: 输出示例:
$ ./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
To implement the filename selection, simple add to the read
and implement a test to validate a numeric answer within range and break out of the loop: 要实现文件名选择,只需添加到
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
output: 输出:
$ ./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.