1846646 Members
2588 Online
110256 Solutions
New Discussion

Re: LS Script

 
SOLVED
Go to solution
Ashwin_4
Frequent Advisor

LS Script

Hi Masters,
Kindly help to write a script to get the file list with sequence no. If total no. of files to be selected is more than 10 then it should ask me two option whether to go next or want to select the file which was displayed using sequence no.


ls *A* --> total no of such files are 25
files should be displayed as below,
1. A1
2. A2
....
10. A10
Hit Enter for Next Page OR Select file no to display File

So if i press enter it should display next page and if i press file no it should display that filename.

I hope you understand my requirement.
Points will be given to all answers.

Thanks.

12 REPLIES 12
G. Vrijhoeven
Honored Contributor

Re: LS Script

Hi,

export COUNT=0
for i in `ls`
do
let COUNT="COUNT++1"
echo "$COUNT $i"
done


HTH,

Gideon
Elmar P. Kolkman
Honored Contributor

Re: LS Script

To number the files, you can use the nl command.
But you need to have it interactively. So it will become something like this:

cnt=1
ls *A* | while read file
do
filelist[$cnt]=$file
(( cnt = $cnt + 2 ))
done

page=1

while [ true ]
do
(( idx = ( $page - 1 )* 10 + 1 ))
while [ $idx -le (( $page * 10)) -a $idx -lt $cnt ]
do
printf "%4d %s\n",$idx,$filelist[$idx]
done

echo "Hit Enter for Next Page OR Select file no to display"
read answer
case "$answer"
"") (( page = $page + 1 ))
break
*) if [ -n "$filelist[$answer]" ]
then
cat $filelist[$answer]
else
echo "File does not exist"
fi
echo "Press return to continue"
read dummy
break
esac
done
Every problem has at least one solution. Only some solutions are harder to find.
Pete Randall
Outstanding Contributor

Re: LS Script

The simple solution:

clear; ll |nl |more -10



Pete

Pete
Pete Randall
Outstanding Contributor

Re: LS Script

Actually, that's only the ls display part and doesn't account for file display - sorry.


Pete

Pete
Ashwin_4
Frequent Advisor

Re: LS Script

Hi Elmar and all for your responses.
Elmar after executing your code i'm getting error at line 13.

Can you tell what could it be?

Thanks ..
Elmar P. Kolkman
Honored Contributor
Solution

Re: LS Script

Sorry, I didn't test what I wrote, just sent the idea of how I would solve your problem. Here is a version that is tested and works... until you have more then 1024 files in the directory. But that's easy to fix with the number of array elements in the set call. But would you walk more then 100 pages of 10 files ?!

#! /bin/ksh
set -A filelist 1024
cnt=0
ls *A* | while read file
do
(( cnt = $cnt + 1 ))
filelist[$cnt]=$file
done

echo $cnt

page=0
(( maxidx = $page * 10 ))

while [ $maxidx -lt $cnt ]
do
(( page = $page + 1 ))
(( maxidx = $maxidx + 10 ))
(( idx = $maxidx - 9 ))
[ $maxidx -gt $cnt ] && maxidx=$cnt
while [ $idx -le $maxidx ]
do
printf "%4d %s\n" $idx ${filelist[$idx]}
(( idx = $idx + 1 ))
done

echo "Hit Enter for Next Page OR Select file no to display"
read answer
case "$answer" in
"")
;;
*) if [ -n "${filelist[$answer]}" ]
then
cat ${filelist[$answer]}
else
echo "File does not exist"
fi
echo "Press return to continue"
read dummy
(( page = $page - 1 ))
(( maxidx = $page * 10 ))
;;
esac
done
Every problem has at least one solution. Only some solutions are harder to find.
Ashwin_4
Frequent Advisor

Re: LS Script

Hi Elmar,
I have 21 files in my dir. A1 to A21.
I'hv tested your script, which is failing to show the filename for which i have enter the sequence no.
./p1
21
1 A1
2 A10
3 A11
4 A12
5 A13
6 A14
7 A15
8 A16
9 A17
10 A18
Hit Enter for Next Page OR Select file no to display

11 A19
12 A2
13 A20
14 A21
15 A3
16 A4
17 A5
18 A6
19 A7
20 A8
Hit Enter for Next Page OR Select file no to display
12 <=== Input from me.
So output should be "A2"
But it is continuing the loop again.


Elmar P. Kolkman
Honored Contributor

Re: LS Script

Sorry... It displays the contents of the file... Can you change it yourself in the script? Second part of the case statement...
Every problem has at least one solution. Only some solutions are harder to find.
Jean-Louis Phelix
Honored Contributor

Re: LS Script

Hi,

This one simply uses the "select" function of sh. The only problem is that "Enter" redisplays current page, while any other input goes to next page (except if you select a number of course) :

#!/usr/bin/sh

LIST=$(ls -1 | tr '\012' ' ')
NB=10
PS3="Hit N for Next Page OR Select file no to display "
while [ ! -z "$LIST" ]
do
SUBLIST="$(echo $LIST | cut -d' ' -f1-10)"
LIST="$(echo $LIST | cut -d' ' -f11-)"
select FILE in $SUBLIST
do
[ -z "$FILE" ] && break
more $FILE
done
done

It works for me (© Bill McNAMARA ...)
Ashwin_4
Frequent Advisor

Re: LS Script

Hi Jean-Louis,
I'm getting syntax error while executing your code. Can you please test and correct it.

Also, my kind request to you is, Do you have any tutorial for reading more about shell scripting.

Thanks in advance.
Ashwin_4
Frequent Advisor

Re: LS Script

Hi Jean-Louis,
OOOPS Sorry! Your code is working fine on HP-UX, I'm getting syntax error in TRU-64 Unix.

Please tell me where can I read about "select" command and other features of "sh"
.

Thanks
Jean-Louis Phelix
Honored Contributor

Re: LS Script

Hi,

I'm quite surprised about the syntax error on TRUE64. 'select' is a standard builtin command of the posix shell, so a 'man sh-posix' gives you all I know about it, including PS3 usage and so on ...

Regards.
It works for me (© Bill McNAMARA ...)