Main Page | Report this Page
Linux Forum Index  »  Linux - Slackware Forum  »  Re. HOW2 use bash to create an action-menu?...
Page 1 of 1    

Re. HOW2 use bash to create an action-menu?...

Author Message
...
Posted: Sat Oct 31, 2009 7:09 am
Guest
Perhaps this previous post didn't get sent ?
Linux shell programming encourages successive refinement, which is
good, but it also encourages hacking instead of designing.
So please refine my version 4 -- below.
==================== I previously wrote:-
I understand that VT cursors can't 'step-up to a previously written line' ?
So to acheive the effect of 'selecting higher-lines' a screen re-write is needed?

---------- Here's the pseudo code of what I had in mind:-
Initialise TopLine AsSelected

REPEAT

write the 1st 8 directory entries, one per line and
mark the SelectedEntry, by some visible means, eg. a leading "->"

CASE ValidKey OF
"v" : IF SelectedEntry = FileType THEN less SelectedEntry END;
<CR>: IF SelectedEntry = FileType THEN Editor SelectedEntry
ELSE cd SelectedEntry END;
UNTIL ValidKey = "q"
-----------------
How would I start towards such a utility using only bash ?

== TIA

##!/bin/bash
#Oct 2009 > file manager to navigate dir-tree, view, edit, mkdir, cp,..etc.
# can replace 'nano' by own editor & 'w3m' by lynx, links ..etc.

write_menu ()

{

clear
# echo 'numbered lines of dir:-'
pwd

# add line-no & show some fields of 'ls -l'
ls -l |
awk '{print NR "\t" $1 " : " $5 " : " $9 }'

echo -n "Selection file/dir: "
read SELCTN

# extract the file/dir via the line-no/SELECTN
FileDir=` ls -l |
awk -v dirline="$SELCTN" 'NR==dirline { print $9 }' `

echo 'L[ynx, V[iew, E[dit, cD[ir', Q[uit, M[kdir, C[opy, U[cd ../
read ACTION
case $ACTION in
L | l) w3m $FileDir;;
U | u) cd ../;;
V | v) cat $FileDir;;
E | e) nano $FileDir;;
# for new file, nano allows chosen fileName to be entered at (no spam) 1st 'save'
D | d) cd $FileDir;;
M | m) echo -n 'new-dir name: '; read KEY; mkdir $KEY;;
C | c) echo -n 'dest-file-name: '; read KEY; cp $FileDir $KEY;;
Q | q) exit;;

*) echo -n "an unknown number of";;
esac
echo -n 'pwd=' ; pwd ;

}

until TEST-COMMANDS; do
#CONSEQUENT-COMMANDS;
write_menu
echo 'key to continue'
read Cont;

done
 
Loki Harfagr...
Posted: Sat Oct 31, 2009 11:50 am
Guest
Sat, 31 Oct 2009 17:09:14 +0000, no.top.post did cat :

Quote:
---------- Here's the pseudo code of what I had in mind:- Initialise
TopLine AsSelected

Wot? Your pseudo is now coded "top line ass elected" ?
that's a prize winning move...
--
Congratufatuations
 
Chris F.A. Johnson...
Posted: Sat Oct 31, 2009 12:54 pm
Guest
On 2009-10-31, no.top.post at (no spam) gmail.com wrote:
Quote:
Perhaps this previous post didn't get sent ?
Linux shell programming encourages successive refinement, which is
good, but it also encourages hacking instead of designing.
So please refine my version 4 -- below.
==================== I previously wrote:-
I understand that VT cursors can't 'step-up to a previously written line' ?
So to acheive the effect of 'selecting higher-lines' a screen re-write is needed?

printf '\e[A' ## move up one line
printf '\e[5A' ## move up two lines


Quote:
---------- Here's the pseudo code of what I had in mind:-
Initialise TopLine AsSelected

REPEAT

write the 1st 8 directory entries, one per line and
mark the SelectedEntry, by some visible means, eg. a leading "->"

CASE ValidKey OF
"v" : IF SelectedEntry = FileType THEN less SelectedEntry END;
CR>: IF SelectedEntry = FileType THEN Editor SelectedEntry
ELSE cd SelectedEntry END;
UNTIL ValidKey = "q"
-----------------
How would I start towards such a utility using only bash ?

== TIA

##!/bin/bash
#Oct 2009 > file manager to navigate dir-tree, view, edit, mkdir, cp,..etc.
# can replace 'nano' by own editor & 'w3m' by lynx, links ..etc.

write_menu ()

{

clear
# echo 'numbered lines of dir:-'
pwd

# add line-no & show some fields of 'ls -l'
ls -l |
awk '{print NR "\t" $1 " : " $5 " : " $9 }'

echo -n "Selection file/dir: "
read SELCTN

# extract the file/dir via the line-no/SELECTN
FileDir=` ls -l |
awk -v dirline="$SELCTN" 'NR==dirline { print $9 }' `

echo 'L[ynx, V[iew, E[dit, cD[ir', Q[uit, M[kdir, C[opy, U[cd ../
read ACTION
case $ACTION in
L | l) w3m $FileDir;;

That (and similar instances below) will file if $FileDir
contains whitespace.

Quote:
U | u) cd ../;;
V | v) cat $FileDir;;
E | e) nano $FileDir;;
# for new file, nano allows chosen fileName to be entered at (no spam) 1st 'save'
D | d) cd $FileDir;;
M | m) echo -n 'new-dir name: '; read KEY; mkdir $KEY;;

More portable is:

printf 'new-dir name: '; read KEY; mkdir "$KEY" ;;

In bash:

read -ep 'new-dir name: ' KEY; mkdir "$KEY" ;;

Don't forget to check that mkdir succeeded and deal with failure.

Quote:
C | c) echo -n 'dest-file-name: '; read KEY; cp $FileDir $KEY;;
Q | q) exit;;

*) echo -n "an unknown number of";;
esac
echo -n 'pwd=' ; pwd ;

POSIX shells have a PWD variable:

printf "%s" "$PWD"

Quote:
}

until TEST-COMMANDS; do
#CONSEQUENT-COMMANDS;
write_menu
echo 'key to continue'
read Cont;

done



--
Chris F.A. Johnson, author | <http://cfaj.freeshell.org>
Shell Scripting Recipes: | My code in this post, if any,
A Problem-Solution Approach | is released under the
2005, Apress | GNU General Public Licence
 
Chris F.A. Johnson...
Posted: Sat Oct 31, 2009 12:55 pm
Guest
On 2009-10-31, Chris F.A. Johnson wrote:
Quote:
On 2009-10-31, no.top.post at (no spam) gmail.com wrote:
Perhaps this previous post didn't get sent ?
Linux shell programming encourages successive refinement, which is
good, but it also encourages hacking instead of designing.
So please refine my version 4 -- below.
==================== I previously wrote:-
I understand that VT cursors can't 'step-up to a previously written line' ?
So to acheive the effect of 'selecting higher-lines' a screen re-write is needed?

printf '\e[A' ## move up one line
printf '\e[5A' ## move up two lines

Correction:

printf '\e[5A' ## move up five lines

--
Chris F.A. Johnson, author | <http://cfaj.freeshell.org>
Shell Scripting Recipes: | My code in this post, if any,
A Problem-Solution Approach | is released under the
2005, Apress | GNU General Public Licence
 
...
Posted: Sun Nov 01, 2009 12:06 pm
Guest
In article <7l3ffiF3bvl3gU1 at (no spam) mid.individual.net>, "Chris F.A. Johnson" <cfajohnson at (no spam) gmail.com> wrote:

Quote:
I understand that VT cursors can't 'step-up to a previously written line' ?
So to acheive the effect of 'selecting higher-lines' a screen re-write is needed?

printf '\e[A' ## move up one line
printf '\e[5A' ## move up five lines

Wow ! But I realised that even if I could hi-lite the stepped-up-to

line, it would be very difficult to map the cursor position to
the directory file/dir name. Altho' perhaps not ? It's amazing how
'writing a formal description of the task' sometimes reveals a
possible algorithm? Counting the multiple '\e[A' would tell the
tell the position in the "ls -l" list just as well as entering the
number manually does.
I think I've got the char-sequence to hi-lite or reverse-video,
somewhere.
Quote:
--snip --
clear
# echo 'numbered lines of dir:-'
pwd
More portable is:

printf 'new-dir name: '; read KEY; mkdir "$KEY" ;;

In bash:

read -ep 'new-dir name: ' KEY; mkdir "$KEY" ;;

OK, I'll try that.
Don't forget to check that mkdir succeeded and deal with failure.

OK but, a main beauty of mc-like-apps is the visual feedback.

You can/should confirm seeing your creations/intentions.
I favour automation which includes the human in the loop.
It's also nice to have some control, instead of that other OS's
wizard things, or as I've recently discovered like Debian's
Stalinist-school-teacher style.
Quote:
--snip --
echo -n 'pwd=' ; pwd ;

POSIX shells have a PWD variable:

printf "%s" "$PWD"

Thanks for the feedback. It's nice to learn/improve in small
increments.

== Chris Glur.
 
goarilla...
Posted: Mon Nov 02, 2009 4:35 pm
Guest
On Sun, 01 Nov 2009 22:06:41 +0000, no.top.post wrote:

Quote:
In article <7l3ffiF3bvl3gU1 at (no spam) mid.individual.net>, "Chris F.A. Johnson"
cfajohnson at (no spam) gmail.com> wrote:

I understand that VT cursors can't 'step-up to a previously written
line' ? So to acheive the effect of 'selecting higher-lines' a screen
re-write is needed?

printf '\e[A' ## move up one line
printf '\e[5A' ## move up five lines

Wow ! But I realised that even if I could hi-lite the stepped-up-to
line, it would be very difficult to map the cursor position to the
directory file/dir name. Altho' perhaps not ? It's amazing how 'writing
a formal description of the task' sometimes reveals a possible
algorithm? Counting the multiple '\e[A' would tell the tell the
position in the "ls -l" list just as well as entering the number
manually does.
I think I've got the char-sequence to hi-lite or reverse-video,
somewhere.
--snip --
clear
# echo 'numbered lines of dir:-'
pwd
More portable is:

printf 'new-dir name: '; read KEY; mkdir "$KEY" ;;

In bash:

read -ep 'new-dir name: ' KEY; mkdir "$KEY" ;;

OK, I'll try that.
Don't forget to check that mkdir succeeded and deal with failure.

OK but, a main beauty of mc-like-apps is the visual feedback. You
can/should confirm seeing your creations/intentions. I favour automation
which includes the human in the loop. It's also nice to have some
control, instead of that other OS's wizard things, or as I've recently
discovered like Debian's Stalinist-school-teacher style.
--snip --
echo -n 'pwd=' ; pwd ;

POSIX shells have a PWD variable:

printf "%s" "$PWD"

Thanks for the feedback. It's nice to learn/improve in small
increments.

== Chris Glur.

maybe you should use dialog
 
 
Page 1 of 1    
All times are GMT - 5 Hours
The time now is Wed Nov 25, 2009 9:00 am