Yad password dialog window cases - dialog

I'd like to create a password dialog window, that would execute different scripts according to password value.
For example, when user enter 123, then 123.sh will be executed, etc.
How can I do that?

man yad is a very good resource. Moreover you will find a lot of yad examples with explanation here : http://smokey01.com/yad/
A small demo of what you described:
$ echo "echo hello" >1.sh && chmod +x 1.sh
$ ./1.sh
hello
$ res=$(yad --entry --entry-text="giveme a number" --hide-text)
# yad window opens - type 1 & enter
$ ./"$res".sh
hello

Related

Send automatic input to a script called by another script in bash

I'm working on a bash script (my_script) in which I call many scripts, they all together automate a work flow.
But when I call one particular (ksh/bash) script (master_script) there are many inputs and checks taken (not arguments) in it.
It is slowing down the whole of the automation, as every time I have to super wise it and enter the values manually.
I have no option to modify or make a new script (work constraints)
Every time the questions are same. I am trying to take all the answers before executing master_script except one answer(whose value depends on the execution) and then feed it to the master_script at the correct time.
Is there a way we can pass the value to the master_script, during its execution from within my_script.? ./master_script<< EOF .. EOF will not help as I have to enter one answer myself.
The below is just an example and my creation, but depicts what exactly is my requirement.
Example code
my_script
#! /bin/bash
echo "Proceeding...."
#calling master_script
/master_script $arg1 $arg2
echo "Completed.."
echo "Executing other scripts"
/other_scripts"
Execution
$ sh ./my_script
Proceeding....
Started master_script..
Press Enter to Proceed MY_INPUT
Enter username to add (eg.user123) MY_UNAME
Enter preferred uid (eg.1234) MY_UID
Do you want to bla bla..(Y/n) MY_INPUT
Please select among the following
1.option1
2.Option2
Selection: MY_SELECTION
Please choose which extension to use
1.ext1
2.ext2
3.ext3
4.ext4
Do you want to bla bla 2..(Y/n) MY_INPUT
Ended master script
Completed..
Executing other scripts
Requirement
#! /bin/bash
echo "Proceeding...."
# get values for master script
read -p "Proceed(Y/n):" proceed1
read -p "Uname:" uname
read -p "Uid:" uid
read -p "bla bla (Y/n):" bla1
read -p "Selection(1/2):" selection1
read -p "bla bla 2(Y/n):" bla2
#calling master_script
./master_script $arg1 $arg2 {all_inputs}
#Silent Execution of master_script until choosing execution...
Please choose which extension to use
1. ext1
2. ext2
3. ext3
4. ext4
#Silent Execution of master_script after choosing ext and continue with other scripts
./other_scripts
echo "Completed.."
I've read about expect/send combination, but I'm unable to comprehend
how to use it. Any inputs will be greatly helpful
EDIT
I am also not sure about ./master_script<< EOF ... EOF as I have to enter one
answer in the middle of execution myself.
There is a solution using here documents and redirecting the input:
./master_script "$arg1" "$arg2" << ENDINPUT
$proceed1
$uname
$uid
$bla1
$selection1
ENDINPUT
Remark 1: the final ENDINPUT must start the line, don't indent! See Man bash
Remark 2: some scripts or programs check if the input comes from an actual terminal (calling isatty()), for instance when typing a password. It is still possible to automate the entries, but it is much more tricky.

Exec dialog command from Expect Script

I tested the line below in TCLSH and it works:
dialog --title "Text" --msgbox "Text" 8 60
However, if I try it in a Expect script with "exec", it will just hang:
exec dialog --title "Text" --msgbox "Text" 8 60
I did a little research, it seems there is no need to escape special characters, but maybe that is the issue?
Do you guys have any suggestion to make this code work? Thanks!
By default the interactive tclsh would behave like a shell (like Bash) and it'll handle unknown commands as external executables and auto exec them. That's why you can directly run dialog from within tclsh. This can be turned off by defining the global auto_noexec var. For example:
[bash] # tclsh
% echo hello world
hello world
% set auto_noexec "the value does not matter"
1
% echo hello world
invalid command name "echo"
%
For the exec command, by default it would not print the output to the terminal. You should use ># stdout or/and 2># stderr:
exec dialog --title Text --msgbox Text 8 60 ># stdout 2># stderr
Experimenting a bit, it will work as expected if you send stdout directly to the terminal
exec dialog --title "Text" --msgbox "Text" 8 60 >/dev/tty

Echo text that is user-editable

Is it possible to output text to a shell window, via bash script, that is user-editable? I essentially want to pre-fill certain information and give the user the ability to edit it if it's wrong.
For instance, if I were to write in a script:
echo -n "Enter your name: Anthony"
while read user_input
do
# do stuff with $user_input
done
How can I allow the user to inline edit the word Anthony only (aka, don't allow backspacing past the A in Anthony), and how can I store the value into a variable once the RETURN key is pressed?
EDIT
I'm looking for something similar to the -i option of read (see answer posted here), but this is only available on bash 4+. Is there an alternative for bash 3?
I needed similar setup recently so what I did was
$ cat a.sh
function input {
python -c '
import sys,readline
readline.set_startup_hook(lambda: readline.insert_text(sys.argv[2]))
sys.stderr.write(raw_input(sys.argv[1]))
' "$#" 3>&1 1>&2 2>&3
}
A=$( input 'question: ' default )
echo "A='$A'"
$ ./a.sh
question: default
A='default'
Well, it's not actually bash, but it made the job done.

Bash scripting on debian installer not accepting user input on preseeding

I have a very small script that needs to be run on debian installer: (via preseeding, pre installation script)
echo -n -e " # Your option [1] [2] [3]: "
read REPLY
if [ "$REPLY" == "1" ]
The script stops here and whatever I press is just displayed onto screen however it is not accepting the enter key. Normally, when you press 1 and press enter, the read should return 1 to $REPLY. But nothing happens. It keeps accepting user input but no further action happens.
Then, I switched to tty2 with ALT+F2 and run the script there, it was fine, it works as expected, when I press; it takes the input. Why tty1 is not accepting enter as usual?
Use debconf for that kind of configuration, it tackles exactly needs like yours.
Adapted example from the manual
Template file (debian/templates):
Template: your_package/select_option
Type: select
Choices: 1, 2, 3
Description: Which option?
Choose one of the options
Script (debian/config):
#!/bin/sh -e
# Source debconf library.
. /usr/share/debconf/confmodule
db_input medium your_package/select_option || true
db_go
# Check their answer.
db_get your_package/select_option
if [ "$RET" = "1" ]; then
# Do stuff
fi
Had the same problem (read not processing my input) with busybox on an embedded Linux.
Took me some time to realize that busybox's read is not CR-tolerant — my terminal program (used miniterm.py) sent CR/LF line ends by default; switching it to LF only solved my problem!
with bash interpreter, try replace read by :
builtin read
with other sh interpreter, specify the variable name :
read REPLY
The following script works fine for me:
#!/bin/sh
echo -n -e " # Your option [1] [2] [3]: "
read
case $REPLY in
1 )
echo "one" ;;
2 )
echo "two" ;;
3 )
echo "three" ;;
*)
echo "invalid" ;;
esac
It prints out one nicely if I choose 1. Any reason why you'd like to stick to if...fi?

How to show a GUI message box from a bash script in linux?

I'm writing a few little bash scripts under Ubuntu linux. I want to be able to run them from the GUI without needing a terminal window to enter any input or view any output.
So far the only input required is a password for sudo - and gksudo handles that fine.
But I haven't found an easy way to show a message box yet. Is there some kind of 'gkmessage' command available? I'd prefer something present in a default Ubuntu install, but I don't mind installing a new package if necessary.
In many Linux distros the notify-send command will throw one of those nice perishable notifications in the top right corner. Like so:
notify-send "My name is bash and I rock da house"
B.e.a.utiful!
I believe Zenity will do what you want. It's specifically designed for displaying GTK dialogs from the command line, and it's available as an Ubuntu package.
Everyone mentions zenity, there seem to be many others. A mixed up but interesting list is at http://alternativeto.net/software/zenity/
zenity:
First, an example of zenity featuring text formatting markup, window title, button label.
zenity \
--info \
--text="<span size=\"xx-large\">Time is $(date +%Hh%M).</span>\n\nGet your <b>coffee</b>." \
--title="Coffee time" \
--ok-label="Sip"
gxmessage:
gxmessage "my text"
xmessage:
xmessage is very old so it is stable and probably available in all distributions that use X (since it's distributed with X). It is customizable through X resources, for those that have been using Linux or Unix for long enough to know what it means (.Xdefaults, anyone ?).
xmessage -buttons Ok:0,"Not sure":1,Cancel:2 -default Ok -nearmouse "Is xmessage enough for the job ?" -timeout 10
kdialog (KDE tool):
kdialog --error "Some error occurred"
YAD (Yet Another Dialog):
Yad is included in newer Ubuntu versions. There is also this PPA: YAD: Zenity On Steroids [Display Graphical Dialogs From Shell Scripts] ~ Web Upd8: Ubuntu / Linux blog. Does not seem to auto-size dialogs.
echo My text | yad \
--text-info \
--width=400 \
--height=200
An bigger example
yad \
--title="Desktop entry editor" \
--text="Simple desktop entry editor" \
--form \
--field="Type:CB" \
--field="Name" \
--field="Generic name" \
--field="Comment" \
--field="Command:FL" \
--field="Icon" \
--field="In terminal:CHK" \
--field="Startup notify:CHK" "Application" "Name" "Generic name" "This is the comment" "/usr/bin/yad" "yad" FALSE TRUE \
--button="WebUpd8:2" \
--button="gtk-ok:0" \
--button="gtk-cancel:1"
Others not in Ubuntu standard repositories:
shellgui
xdialog
gtkdialog
Off-topic (for terminal):
whiptail --msgbox "my text" 10 20
dialog --msgbox "my text" 10 20
Feel free to edit.
The zenity application appears to be what you are looking for.
To take input from zenity, you can specify a variable and have the output of zenity --entry saved to it. It looks something like this:
my_variable=$(zenity --entry)
If you look at the value in my_variable now, it will be whatever was typed in the zenity pop up entry dialog.
If you want to give some sort of prompt as to what the user (or you) should enter in the dialog, add the --text switch with the label that you want. It looks something like this:
my_variable=$(zenity --entry --text="What's my variable:")
Zenity has lot of other nice options that are for specific tasks, so you might want to check those out as well with zenity --help. One example is the --calendar option that let's you select a date from a graphical calendar.
my_date=$(zenity --calendar)
Which gives a nicely formatted date based on what the user clicked on:
echo ${my_date}
gives:
08/05/2009
There are also options for slider selectors, errors, lists and so on.
Hope this helps.
I found the xmessage command, which is sort of good enough.
alert and notify-send seem to be the same thing. I use notify-send for non-input messages as it doesn't steal focus and I cannot find a way to stop zenity etc. from doing this.
e.g.
# This will display message and then disappear after a delay:
notify-send "job complete"
# This will display message and stay on-screen until clicked:
notify-send -u critical "job complete"
if nothing else is present. you can launch an xterm and echo in it, like this:
xterm -e bash -c 'echo "this is the message";echo;echo -n "press enter to continue "; stty sane -echo;answer=$( while ! head -c 1;do true ;done);'
Here's a little Tcl script that will do what you want. The Wish interpreter should be installed by default on Ubuntu.
#!/usr/bin/wish
pack [label .msg -text [lindex $argv 0]]
pack [entry .ent]
bind .ent <KeyPress-Return> { puts [.ent get]; destroy . }
focus .ent
Call it like this:
myanswer=`gui-prompt "type your answer and press enter"`
There is also dialog and the KDE version kdialog. dialog is used by slackware, so it might not be immediately available on other distributions.
How about Ubuntu's alert. It can be used after any operation to alert it finished and even show red cross icon if operaton was finnished with errors
ls -la; alert
Zenity is really the exact tool that I think that you are looking for.
or
zenity --help
You can use shellmarks to display a GUI dialog prior to your shell script running, that will allow the user to enter data that will be placed in the environment.
#!/bin/bash
echo "Hello ${name}"
exit 0
---
[name]
type="text"
label="Please enter your name"
required=true
Running script:
shellmarks hello.sh
If you enter "Steve" in the box and press run, the output will be
Hello Steve
Disclosure: I'm the author of Shellmarks
Kdialog and dialog are both good, but I'd recommend Zenity. Quick, easy, and much better looking the xmessage or dialog.
I'm liking what I'm seeing with script-dialog. It ticks all my boxes, plus some:
pop up GUI boxes, but has text-mode fallback
support for various sudo variants (gksudo, kde-sudo, ...)
can re-launch itself in terminal window
Indeed it's a wrapper for kdialog, zenity, dialog, whiptail and a custom fall-back.
Draw-back is that it doesn't have a CLI, but instead is meant to be sources into a bash script.

Resources