0

Hi, I am new to Unix/Linux...

I have a TCSH shell script (on AIX4) in which, at a certain point, I want the user to be able to pass commands to the TCSH interpreter. What I do is I create the illusion that the user is at his usuall shell prompt :

echo -n `whoami`"> ";
set cmdinput = "$<";

After reading the standard input from key board, I want to "execute" within the shell, the command typed by user. Note that I run my script with >source myscript.tcsh to ensure it running from the current shell (unlike >tcsh myscript.tcsh). For the purpose of this post lets say I would like the user to be able to run the following 8 commands, the first three are from Unix while the last 5 are TCSH built-in (will use -> to denote what I would like the shell to return) :

>pwd                -> /usr
>ps                 -> PID    TTY  TIME CMD
                       23200  pts/1  0:00 ps 
                       28346  pts/1  0:00 -tcsh 
>ps -l              -> F      S      UID   PID  PPID   C PRI NI ADDR    SZ    WCHAN    TTY  TIME CMD
                       200001 A      384 25598 28346   1  60 20  ee3   256           pts/1  0:00 ps
                       240001 A      384 28346 30552   1  60 20 4f0b  1112           pts/1  0:00 tcsh"
>set x=10           -> 
>echo "x is"        -> x is
>echo $x            -> 10
>alias p pwd        -> 
>alias | grep pwd   -> p      pwd
>p                  -> /usr

So here are the 4 diffrent attemps to executes those commands with their respective output (will use -> to denote what the shell actually returns) :

$cmdinput;
    >pwd                -> /usr  
    >ps                 -> PID    TTY  TIME CMD
                           23200  pts/1  0:00 ps 
                           28346  pts/1  0:00 -tcsh 
    >ps -l              -> F      S      UID   PID  PPID   C PRI NI ADDR    SZ    WCHAN    TTY  TIME CMD
                           200001 A      384 25598 28346   1  60 20  ee3   256           pts/1  0:00 ps
                           240001 A      384 28346 30552   1  60 20 4f0b  1112           pts/1  0:00 tcsh"
    >set x=10           -> 
    >echo "x is"        -> "x is"
    >echo $x            -> $x
    >alias p pwd        -> 
    >alias | grep pwd   -> : Command not found.
    >p                  -> p: Command not found.

Result : The first 3 commands work even thoe the 3rd one has a space in it. The set command works, I confirmed by reading x afterwords, but 5th command returned unwated quotes and the lase one returned $x istead of its value. No succes with setting, reading or using alias.

`$cmdinput`;
    >pwd                -> /usr: Permission denied
    >ps                 -> PID: Command not found.
    >ps -l              -> F: Command not found
    >set x=10           -> : Command not found.
    >echo "x is"        -> "x: Command not found.
    >echo $x            -> $x: Command not found.
    >alias p pwd        -> : Command not found.
    >alias | grep pwd   -> : Command not found.
    >p                  -> p: Command not found.
                           : Command not found.

Result : Instead of outputting the result of the commands, it tried to run the results of the commands as if themselves were commands. No succes with setting, reading or using alias.

`echo $cmdinput`;
    >pwd                -> /usr  
    >ps                 -> PID    TTY  TIME CMD
                           23200  pts/1  0:00 ps 
                           28346  pts/1  0:00 -tcsh 
    >ps -l              -> F      S      UID   PID  PPID   C PRI NI ADDR    SZ    WCHAN    TTY  TIME CMD
                           200001 A      384 25598 28346   1  60 20  ee3   256           pts/1  0:00 ps
                           240001 A      384 28346 30552   1  60 20 4f0b  1112           pts/1  0:00 tcsh"
    >set x=10           -> set: Command not found.
    >echo "x is"        -> "x is"
    >echo $x            -> $x
    >alias p pwd        -> p: alias not found
                           pwd: alias not found
    >alias | grep pwd   -> /usr/bin/alias[53]: |: invalid alias name
    >p                  -> p: Command not found.

Result : The first 3 commands work even thoe the 3rd one has a space in it. The set command did not work even though it is shell built-in. 5th command returned unwated quotes and the lase one returned $x istead of its value (it was still set to 10 from previous attempt). No succes with setting, reading or using alias.

`echo "$cmdinput"`    
    >pwd                -> /usr  
    >ps                 -> PID    TTY  TIME CMD
                           23200  pts/1  0:00 ps 
                           28346  pts/1  0:00 -tcsh 
    >ps -l              -> F      S      UID   PID  PPID   C PRI NI ADDR    SZ    WCHAN    TTY  TIME CMD
                           200001 A      384 25598 28346   1  60 20  ee3   256           pts/1  0:00 ps
                           240001 A      384 28346 30552   1  60 20 4f0b  1112           pts/1  0:00 tcsh"
    >set x=10           -> set: Command not found.
    >echo "x is"        -> "x is"
    >echo $x            -> $x
    >alias p pwd        -> p: alias not found
                           pwd: alias not found
    >alias | grep pwd   -> /usr/bin/alias[53]: |: invalid alias name
    >p                  -> p: Command not found.

Result : Exactly the same as previous attempt.

Over all, I feel like my first attemps ($cmdinput;) is the closest even though I was not able to echo the value of a variable...
Any help is appreciated.

1
Contributor
1
Reply
2
Views
6 Years
Discussion Span
Last Post by etm9413
0

Hi, I am new to Unix/Linux...

I have a TCSH shell script (on AIX4) in which, at a certain point, I want the user to be able to pass commands to the TCSH interpreter. What I do is I create the illusion that the user is at his usuall shell prompt :

echo -n `whoami`"> ";
set cmdinput = "$<";

After reading the standard input from key board, I want to "execute" within the shell, the command typed by user. Note that I run my script with >source myscript.tcsh to ensure it running from the current shell (unlike >tcsh myscript.tcsh). For the purpose of this post lets say I would like the user to be able to run the following 8 commands, the first three are from Unix while the last 5 are TCSH built-in (will use -> to denote what I would like the shell to return) :

>pwd                -> /usr
>ps                 -> PID    TTY  TIME CMD
                       23200  pts/1  0:00 ps 
                       28346  pts/1  0:00 -tcsh 
>ps -l              -> F      S      UID   PID  PPID   C PRI NI ADDR    SZ    WCHAN    TTY  TIME CMD
                       200001 A      384 25598 28346   1  60 20  ee3   256           pts/1  0:00 ps
                       240001 A      384 28346 30552   1  60 20 4f0b  1112           pts/1  0:00 tcsh"
>set x=10           -> 
>echo "x is"        -> x is
>echo $x            -> 10
>alias p pwd        -> 
>alias | grep pwd   -> p      pwd
>p                  -> /usr

So here are the 4 diffrent attemps to executes those commands with their respective output (will use -> to denote what the shell actually returns) :

$cmdinput;
    >pwd                -> /usr  
    >ps                 -> PID    TTY  TIME CMD
                           23200  pts/1  0:00 ps 
                           28346  pts/1  0:00 -tcsh 
    >ps -l              -> F      S      UID   PID  PPID   C PRI NI ADDR    SZ    WCHAN    TTY  TIME CMD
                           200001 A      384 25598 28346   1  60 20  ee3   256           pts/1  0:00 ps
                           240001 A      384 28346 30552   1  60 20 4f0b  1112           pts/1  0:00 tcsh"
    >set x=10           -> 
    >echo "x is"        -> "x is"
    >echo $x            -> $x
    >alias p pwd        -> 
    >alias | grep pwd   -> : Command not found.
    >p                  -> p: Command not found.

Result : The first 3 commands work even thoe the 3rd one has a space in it. The set command works, I confirmed by reading x afterwords, but 5th command returned unwated quotes and the lase one returned $x istead of its value. No succes with setting, reading or using alias.

`$cmdinput`;
    >pwd                -> /usr: Permission denied
    >ps                 -> PID: Command not found.
    >ps -l              -> F: Command not found
    >set x=10           -> : Command not found.
    >echo "x is"        -> "x: Command not found.
    >echo $x            -> $x: Command not found.
    >alias p pwd        -> : Command not found.
    >alias | grep pwd   -> : Command not found.
    >p                  -> p: Command not found.
                           : Command not found.

Result : Instead of outputting the result of the commands, it tried to run the results of the commands as if themselves were commands. No succes with setting, reading or using alias.

`echo $cmdinput`;
    >pwd                -> /usr  
    >ps                 -> PID    TTY  TIME CMD
                           23200  pts/1  0:00 ps 
                           28346  pts/1  0:00 -tcsh 
    >ps -l              -> F      S      UID   PID  PPID   C PRI NI ADDR    SZ    WCHAN    TTY  TIME CMD
                           200001 A      384 25598 28346   1  60 20  ee3   256           pts/1  0:00 ps
                           240001 A      384 28346 30552   1  60 20 4f0b  1112           pts/1  0:00 tcsh"
    >set x=10           -> set: Command not found.
    >echo "x is"        -> "x is"
    >echo $x            -> $x
    >alias p pwd        -> p: alias not found
                           pwd: alias not found
    >alias | grep pwd   -> /usr/bin/alias[53]: |: invalid alias name
    >p                  -> p: Command not found.

Result : The first 3 commands work even thoe the 3rd one has a space in it. The set command did not work even though it is shell built-in. 5th command returned unwated quotes and the lase one returned $x istead of its value (it was still set to 10 from previous attempt). No succes with setting, reading or using alias.

`echo "$cmdinput"`    
    >pwd                -> /usr  
    >ps                 -> PID    TTY  TIME CMD
                           23200  pts/1  0:00 ps 
                           28346  pts/1  0:00 -tcsh 
    >ps -l              -> F      S      UID   PID  PPID   C PRI NI ADDR    SZ    WCHAN    TTY  TIME CMD
                           200001 A      384 25598 28346   1  60 20  ee3   256           pts/1  0:00 ps
                           240001 A      384 28346 30552   1  60 20 4f0b  1112           pts/1  0:00 tcsh"
    >set x=10           -> set: Command not found.
    >echo "x is"        -> "x is"
    >echo $x            -> $x
    >alias p pwd        -> p: alias not found
                           pwd: alias not found
    >alias | grep pwd   -> /usr/bin/alias[53]: |: invalid alias name
    >p                  -> p: Command not found.

Result : Exactly the same as previous attempt.

Over all, I feel like my first attemps ($cmdinput;) is the closest even though I was not able to echo the value of a variable...
Any help is appreciated.

Fixed using >eval $cmdinput !!

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.