LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 08-08-2018, 01:29 PM   #1
close.code
LQ Newbie
 
Registered: Aug 2018
Posts: 1

Rep: Reputation: Disabled
Post Is there a way to script any Linux commands to prompt the user for input?


Is there a way to script any Linux commands to prompt the user for input?
For example:
chage command has many options
Options: -d, --lastday LAST_DAY set date of last password change to LAST_DAY -E, --expiredate EXPIRE_DATE set account expiration date to EXPIRE_DATE -h, --help display this help message and exit -I, --inactive INACTIVE set password inactive after expiration to INACTIVE -l, --list show account aging information -m, --mindays MIN_DAYS set minimum number of days before password change to MIN_DAYS -M, --maxdays MAX_DAYS set maximim number of days before password change to MAX_DAYS -R, --root CHROOT_DIR directory to chroot into -W, --warndays WARN_DAYS set expiration warning days to WARN_DAYS

I like to type in chage and get the above option to allow me to select option(s)
this will help me if I do not remember all the option for type in chage -h every time.

Please email me your answer
Thx. Reza
 
Old 08-08-2018, 01:48 PM   #2
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: FreeBSD/Slackware-14.2+/ArcoLinux
Posts: 9,067

Rep: Reputation: 1900Reputation: 1900Reputation: 1900Reputation: 1900Reputation: 1900Reputation: 1900Reputation: 1900Reputation: 1900Reputation: 1900Reputation: 1900Reputation: 1900
Code:
#!/bin/bash

read -p "enter your name " responce

case $responce in
bob) echo "why is your name $responce" ;;
sally) echo "what are you $responce , doing here?" ;;
esac
of course that is just a working template.
 
1 members found this post helpful.
Old 08-08-2018, 02:20 PM   #3
kilgoretrout
Senior Member
 
Registered: Oct 2003
Posts: 2,700

Rep: Reputation: 244Reputation: 244Reputation: 244
I've been using yad dialog boxes a lot in my bash scripts lately:

https://www.thelinuxrain.com/article...buttons-of-yad

https://www.systutorials.com/docs/linux/man/1-yad/

You can code every option in chage to a button in a yad dialog box. That may be a little more than you want. You could also do a data entry box in yad with the box listing the chage options in a numbered fashion. You will probably have to install yad but it is commonly available in most distros.

You can also do similar dialog boxes with whiptail:

http://xmodulo.com/create-dialog-box...ll-script.html

Last edited by kilgoretrout; 08-08-2018 at 02:27 PM.
 
1 members found this post helpful.
Old 08-08-2018, 02:50 PM   #4
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 21,808

Rep: Reputation: 5771Reputation: 5771Reputation: 5771Reputation: 5771Reputation: 5771Reputation: 5771Reputation: 5771Reputation: 5771Reputation: 5771Reputation: 5771Reputation: 5771
Quote:
Originally Posted by close.code View Post
Is there a way to script any Linux commands to prompt the user for input?
For example:
chage command has many options
Options: -d, --lastday LAST_DAY set date of last password change to LAST_DAY -E, --expiredate EXPIRE_DATE set account expiration date to EXPIRE_DATE -h, --help display this help message and exit -I, --inactive INACTIVE set password inactive after expiration to INACTIVE -l, --list show account aging information -m, --mindays MIN_DAYS set minimum number of days before password change to MIN_DAYS -M, --maxdays MAX_DAYS set maximim number of days before password change to MAX_DAYS -R, --root CHROOT_DIR directory to chroot into -W, --warndays WARN_DAYS set expiration warning days to WARN_DAYS

I like to type in chage and get the above option to allow me to select option(s) this will help me if I do not remember all the option for type in chage -h every time.
While the basic question has been answered, reading what you're going to use it for leaves me to ask "Why?" If you can't remember the options, typing "chage -h" (or "man <command>") will tell you the options. And this script will do exactly the same thing, except it won't prompt you for a "-h". Which seems pointless to go through all this work for every command, just to omit a "-h". And it only gets worse if you have to page through MANY options for different programs. It would make far more sense to write a small script that has the options you frequently use in it, and take the input as an argument.

What is your actual goal here, past one command?
Quote:
Please email me your answer Thx. Reza
No, we aren't going to email you answers; if you want to participate, you need to do it in the forums. Read the LQ Rules, and the "How to ask a smart question" and "Question Guidelines" links. We are happy to try to help you, but we aren't going to provide personalized, free, one-on-one email support.
 
2 members found this post helpful.
Old 08-08-2018, 02:52 PM   #5
rtmistler
Moderator
 
Registered: Mar 2011
Location: MA, USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 7,992
Blog Entries: 13

Rep: Reputation: 3443Reputation: 3443Reputation: 3443Reputation: 3443Reputation: 3443Reputation: 3443Reputation: 3443Reputation: 3443Reputation: 3443Reputation: 3443Reputation: 3443
Quote:
Originally Posted by close.code View Post
Please email me your answer
Please note that this is not the practice on LQ. (As also noted by TB0ne)


The LQ forum is intended to help solve questions and make those Q's and A's available to the community at large, i.e. everyone. Please do not attempt to subvert this in any fashion and keep technical questions and their answers within LQ threads.


I normally do what BW-userx showed. I also don't do much stdin from within a script, but instead pass an argument.

If you check the link in my signature for My Bash Blog, I believe you'll see some examples of that same type of script programming, including some debugging ideas. Meanwhile those examples should be no different than what was posted already.

Last edited by rtmistler; 08-08-2018 at 02:55 PM. Reason: Noting my redundancy and adding a reference
 
Old 08-08-2018, 05:54 PM   #6
Mike_Walsh
Member
 
Registered: Jul 2017
Location: King's Lynn, UK
Distribution: Nowt but Puppies....
Posts: 361

Rep: Reputation: 158Reputation: 158
I'll agree with kilgoretrout. YAD is definitely the way to go. Ok, strictly speaking, it's cheating; one of the big attractions of Linux is that of how powerful it can be, running solely from the terminal. YAD is primarily for creating GUI dialog boxes ( l Windoze....urck; leaves a nasty taste in my mouth, saying that word), but the range of options is truly astonishing, especially with the last couple of releases (0.39.0, 0.40.0). One of my personal favourites is

Code:
--close-on-unfocus
.....your dialog box will auto-close when the 'focus' shifts to another window. Especially nifty when you combine the above with

Code:
--no-buttons
..which does away with the 'Yes', 'No', 'OK', etc., buttons you normally get at the foot of the dialog.

The link at the foot of the page from The Linux Rain, as given in kilgoretrout's post, will take you to the YAD tutorial by Smokey01, one of our long-standing Puppy Forum members. This work has been referenced by many other blog-writers in recent years; Grant has written tons of applications for Puppy since he started with it back in 2006, and makes liberal use of YAD.

It's easy enough to compile yourself, if you want the latest version; very few Linux distros seem to ship with the newest release, nor is it generally available in their repositories. The source is available from here:-

https://sourceforge.net/projects/yad-dialog/


Mike.

Last edited by Mike_Walsh; 08-10-2018 at 10:10 AM.
 
Old 08-09-2018, 09:01 AM   #7
Shadow_7
Senior Member
 
Registered: Feb 2003
Distribution: debian
Posts: 3,911
Blog Entries: 1

Rep: Reputation: 828Reputation: 828Reputation: 828Reputation: 828Reputation: 828Reputation: 828Reputation: 828
There's generally a --help feature to list common options, I tend to do that before commands that I haven't run in a while. Or check the manpage. To have the command prompt for the command, you need a script with the same name and a $PATH that checks your version first. Not really something that one who's going to write a script to checkbox options before running something is going to setup. If it's something you're going to do often you're more likely to setup an alias with the desired default options.

$ alias
$ help alias

With prefixing a command with \ to avoid using an alias.
 
Old 08-09-2018, 09:17 AM   #8
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 18,058

Rep: Reputation: 2895Reputation: 2895Reputation: 2895Reputation: 2895Reputation: 2895Reputation: 2895Reputation: 2895Reputation: 2895Reputation: 2895Reputation: 2895Reputation: 2895
Quote:
Originally Posted by Shadow_7 View Post
With prefixing a command with \ to avoid using an alias.
This is such a useful tip ... took me years to learn of it - now I use it all the time; especially when dumb-ass colours are invoked ... <grrr>
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Bash Script For Reading User Input then Compressing That Input to Tar file braveranger Linux - Newbie 13 11-15-2017 09:36 AM
Using read to prompt for editable user input in Bash 3 woodson2 Programming 4 11-28-2012 11:18 AM
Shell script to input values to a utility automatically at the prompt sunrexstar Linux - Newbie 1 07-17-2012 11:56 AM
Bash scripting with sudo and password prompt, and accepting user input. Jetso Programming 14 07-31-2011 12:31 PM
Shell script to prompt for input kent2612 Linux - Newbie 11 06-23-2011 02:38 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 01:12 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration