From: Alvin SIU on
Hi all,

I have a problem.
The situation is complicate. But, I will use a simpler example to
explain.


I have a directory having files:
amanda.txt apple.txt anita.txt barbara.txt bonnie.txt betty.txt

Then, I have a ksh shell scipt called viewfile.ksh

When I issue the following :
viewfile.ksh a*

It will display a menu like this:
1. amanda.txt
2. anita.txt
3. apple.txt
File filter : a*
Which file you want to view ?

Then, when I input 1, it will open vi to view the file amanda.txt
After exiting vi, the same menu will re-appear for you to choose next
file to view.

The filtering string [ a* ] is got from the last line of the fc -l
command.

The script works fine in the ksh in AIX.
My default login shell is ksh.


Then, I get another machine with Linux. My default login shell is
bash.

I copy this script to that new machine and simply add a first line :
#!/bin/ksh
to make the script to run under ksh.


The problem is here.

If I am inside bash (e.g. after login),
I can use the fc -l to find the [ pattern ] of any command.
For example, If I issue ls -l a*
The fc -l will show the line [ ls -l a* ]
and I can get the pattern [ a* ]

But, since viewfile.ksh is run under ksh. Inside the ksh, the fc -l
returns nothing.
So, I cannot use the fc -l method inside the viewfile.ksh to get the
pattern [ a* ]

My questions are:

1.
Actually, this is no big deal to me.
I also try to tail the ~/.bash_history in order to get the pattern
[ a* ].
But, this method does not work.
The pattern is not there, just the command name.

I just want to know whether there is something else can solve this
problem.


2.
I know that the shell will do a [ expand ] of [ a* ] to become a list
of
files as the parameters of the script viewfile.ksh

It seems to me that, there is nothing to store the [ before expand ]
pattern [ a* ].
The only way is to use fc -l to get the latest command to get the
pattern.

Will there be any new things now to store the [ before expand ]
pattern ?



Welcome to have a discuss and brain storm on this topic.

Bye Bye
Alvin SIU
From: mop2 on
An idea, not very elegant, but it works.
The alias line must be inserted in /etc/profile or ~/.profile
I think this solution, with alias and function concepts, is ok for
bash and ksh93.

$ cat viewfile.ksh
#!/bin/ksh
echo "Filter: $*"
ls $*

$ alias viewfile='v(){ viewfile.ksh $*;set +f;};set -f;v'

$ viewfile e*.txt
Filter: e*.txt
energy.txt english.txt

Another solution can be the removal of the first line of viewfile.ksh.
In this way, the current interactive shell will be used, I think, but
the script has to be adapted to work properly with any of
the two shells.

From: pk on
Alvin SIU wrote:

> When I issue the following :
> viewfile.ksh a*

Well, probably a dirty trick (whose applicability in your case I don't
know), however it should not be too difficult to call it using 'a*' (with
single quotes) and do the expansion inside, eg

$ cat filter.sh
#!/bin/sh

filter=$1 # save filter
set -- $1 # set positional parameters

printf "Filter is %s\n" "$filter"
n=1
for i in "$@"; do
printf "Argument %d is %s\n" "$n" "$i"
n=$((n+1))
done

$ ls a*
a b c d.txt a.sh a.txt
$ ./filter.sh 'a*'
Filter: a*
Argument 1 is a b c d.txt
Argument 2 is a.sh
Argument 3 is a.txt

--
All the commands are tested with bash and GNU tools, so they may use
nonstandard features. I try to mention when something is nonstandard (if
I'm aware of that), but I may miss something. Corrections are welcome.
From: Alvin SIU on
On Mar 30, 2:15 pm, mop2 <mop2bky4mz5tyjwa8ersp7hrg5u...(a)gmail.com>
wrote:
> An idea, not very elegant, but it works.
> The alias line must be inserted in /etc/profile or ~/.profile
> I think this solution, with alias and function concepts, is ok for
> bash and ksh93.
>
> $ cat viewfile.ksh
> #!/bin/ksh
> echo "Filter: $*"
> ls $*
>
> $ alias viewfile='v(){ viewfile.ksh $*;set +f;};set -f;v'
>
> $ viewfile e*.txt
> Filter: e*.txt
> energy.txt  english.txt
>
> Another solution can be the removal of the first line of viewfile.ksh.
> In this way, the current interactive shell will be used, I think, but
> the script has to be adapted to work properly with any of
> the two shells.

Thanks mops.
The v() method really works.

But, I have to modify a little to handle the toggle of the name
substitution.
Ha Ha, anyway, this is just another small headache which can be
solved.

Bye.
Alvin SIU