The UNIX and Linux Forums

The UNIX and Linux Forums (http://www.unix.com/index.php)
-   Shell Programming and Scripting (http://www.unix.com/shell-programming-and-scripting/)
-   -   Injecting into shell history with perl? (http://www.unix.com/shell-programming-and-scripting/160342-injecting-into-shell-history-perl.html)

siegfried 05-25-2011 01:41 PM

Injecting into shell history with perl?
 
How do I determine what my default shell is? If I'm root, how do I determine what my user's default shell is?

I think I'm running bash by default.

How do I write a perl program that can inject history into the shell's history so it can be retrieved with the history | grep command?

I have an old fashioned console (non-GUI) style perl program that asks dozens of questions. Incorporating readline might mitigate this a bit. What I would like to do is enhance the logic of this program to check ARGV before prompting the console (user) for information and then, after all the questions have been answered, artificially inject the completed command line into the shell history so the command can be re-run again with identical inputs by just hitting the up-arrow. This will save them from having to type all that stuff in again.

Can this be done?
Thanks!
Siegfried

Shell_Life 05-25-2011 02:34 PM

The type of shell you have is recorded in the "/etc/passwd" file.

To view your shell, you can issue:
Code:

egrep 'userid' /etc/passwd
Most systems have a global variable (SHELL) set, then you can issue:
Code:

echo $SHELL
As for being able to recall command history:
Code:

set -o vi

turk451 05-25-2011 02:50 PM

Quote:

I have an old fashioned console (non-GUI) style perl program that asks dozens of questions. Incorporating readline might mitigate this a bit. What I would like to do is enhance the logic of this program to check ARGV before prompting the console (user) for information and then, after all the questions have been answered, artificially inject the completed command line into the shell history so the command can be re-run again with identical inputs by just hitting the up-arrow. This will save them from having to type all that stuff in again.
I don't think it's a good idea to hack the history to implement another hack in your program.

The ARGV stuff should be easy. Just check the length of ARGV array to see if you got enough command-line inputs. Also, you may want to validate the input data just as a good practice. Even if a user has supplied enough arguments, they could still be badly formatted. Also, be sure to display to the user what the inputs should be.

As for repeated executions of the same script with different inputs, you could easily modify the script to function in a loop, and provide some option for the used to update each value every time the loop runs. What I imagine is a prompt that will default to the previous values used for each parameter of the call, then ask for an updated value. If the user enters nothing, then it would just use the default. If the user enters some new value, it would use that new value.

siegfried 05-25-2011 11:22 PM

Long command lines
 
This program prompts for a lot of numeric IP addresses among other things. Composing a command line with a couple of dozen command line arguments could get a little tedious. Sure I can do syntax checking after the perl program starts, but then the user will have to abort (^C) and hit up arrow and fix the command line argument. Ugghhh...

The user could issue no command line arguments and let my perl prompt for everything and check the syntax of each of the responses and then I could write a script file with all these arguments should they want to do it again. But then the user has to run an editor to make changes and I was hoping to not have to burden the user with that.

Hmmm... I cannot think of any other options. I guess this is why XWindows was invented...

Thanks!
Siegfried


All times are GMT -4. The time now is 04:19 PM.

Linux and Unix Supported by: vBulletin
Search Engine Optimisation provided by DragonByte SEO v1.1.4 (Pro) - vBulletin Mods & Addons Copyright © 2014 DragonByte Technologies Ltd.
The UNIX and Linux Forums Content Copyright ©1993-2013. All Rights Reserved.
Forum Operations by The UNIX and Linux Forums