I found out about Dialogs, so I'm updating menus today. So far, so good.
I came to one where I need to collect a user's input.
I have
dialog --title " INPUT FILE NAME: " --inputbox "$(ls)" 30 40 2> answer
This will send the users input to a file named "answer"
I have tried
dialog --title " INPUT FILE NAME: " --inputbox "$(ls)" 30 40 2> $answer
but that doesnt seem to do anything.
I tried
answer=$(dialog --title " INPUT FILE NAME: " --inputbox "$(ls)" 30 40 2)
but there is some kind of error.
The manual page (for dialog) tells the story:
Some widgets, e.g., checklist, will write text to dialog's output.
Normally that is the standard error, but there are options for changing
this: "--output-fd", "--stderr" and "--stdout". No text is written if
the Cancel button (or ESC) is pressed; dialog exits immediately in that.
The reason dialog uses the standard error by default for its output is that it uses the curses/ncurses library, which normally prints its output (for the screen updates) to the standard output. To change dialog's behavior (and write to the standard output), use the "--stdout" option.
Interestingly (though it may appear an obvious problem to solve because it complicates scripting), the Xdialog program implemented this option first; it seemed a Good Thing to add to dialog (see changelog).
Done
dialog --title " INPUT FILE NAME: " --inputbox "$(ls )" 30 40 2>answer
ans=$(cat answer)
rm answer
Related
I'd like to know whether a user performed a double click in a certain application (specifically, evince). I wrote some code to check if evince is running and then whether it is focused application. However, I do not know how to check whether double-click was performed.
pids=$(xdotool search --class "evince")
curr_id=$(xdotool getwindowfocus)
if echo ${pids[#]} | grep -q -w $curr_id; then
echo "is in array"
else
echo "is not in array"
fi
I am a Java developer but I'm ready to do the coding in bash or Python.
I'm a Debian Stretch user coming from Windows. In Windows with the launchy app (also available for Linux), I had a method of entering text into launchy that was then appended to the end of a .txt or .md file.
To do this in Windows, I created a file called note.bat that contained the following:
echo %*>>"C:\collectednotes.md"
I'd make launchy aware of note.bat by adding its containing folder to “Launchy” → “Settings” → “Catalog” and adding filetype *.bat.
From there, I'd launch launchy, type note, hit Tab, enter some text, hit Enter, and then the text would be added to the end of collectednotes.md.
A mostly working process is detailed in my answer below. I'll give the green checkmark answer to anyone that can adjust this process (via note.sh and/or launchy plugin setup detailed below) to appropriately handle all special characters.
This may contain the solution to this question:
Which characters need to be escaped in Bash? How do we know it?
Solved (almost). I'm keeping this question unanswered and will give to whoever completes the remaining ~5%. Steps to get the 95% solution with xfce4-terminal version 0.8.3-1:
Install launchy and launchy-plugins (both are version 2.5-4 for me):
apt-get install launchy
apt-get install launchy-plugins
Open terminal to default location of ~/ and create collectednotes.md:
echo "# Launchy Notes Collected Here" > collectednotes.md
Create note.sh shell script:
echo '#!/bin/sh' > note.sh
Create shell script line 2:
echo ALL_ARGUMENTS='"$#"' >> note.sh
Create shell script line 3:
echo 'echo "$ALL_ARGUMENTS" >> ~/collectednotes.md' >> note.sh
If you open note.sh, it will look like:
#!/bin/sh
ALL_ARGUMENTS="$#"
echo "$ALL_ARGUMENTS" >> ~/collectednotes.md
Make note.sh executable:
chmod +x note.sh
Launch launchy and click the gear icon in upper right for settings. If consistency with launchy for Windows is desired, set launchy Hotkey to Alt+Space. If you receive a keyboard shortcut conflict message as I do on Debian with Xfce, first go to Settings, Window Manager, Keyboard tab, and clear Alt+Space as the shortcut for Window operations menu.
Next in launchy settings, go to Plugins tab and enable the plugin Runner. Click the + button and create a new Runner custom command as follows:
- Name: note
- Program: /home/YOURUSERNAMEHERE/note.sh (launchy does not like Program path of ~/note.sh, so a specific path with username is required)
- Arguments: '$$'
Click the Catalog tab and hit Rescan Catalog just in case. Hit OK to close launchy settings.
Now let's test it.
- launch launchy with Alt+Space or your hotkey
- type note (You may have to wait 10 second or so on first run. You'll know things are as expected when you see the text "note" with a orange/yellow icon containing silhouette of a person.)
- hit Tab
- enter some text (no need for single or double quotes or escapes), e.g.: Remember to donate at least $3 to Josh at Launchy https://www.launchy.net/donate.php
- hit Enter
Now open collectednotes.md to confirm the text was captured.
The remaining issues seem to be dealing with single quotes and double quotes. For example, consider the following note:
I don't know what I'd do without Launchy.
Which results in the following in collectednotes.md:
I dont know what Id do without Launchy.
Or:
Would David Allen like universal text capture from anywhere in Linux? My bet is "yes!"
Results in the following in collectednotes.md:
Would David Allen like universal text capture from anywhere in Linux? My bet is \yes!\
Single quoting and/or double quoting the input to launchy doesn't solve it. Note the launchy Runner custom plugin construction component of '$$' is a piece of this puzzle.
I'll give the answer to anyone that can adjust this process (via note.sh and/or launchy plugin setup) to appropriately handle all special characters. Maybe this would add proper escapes to user input with something like gsub.
The rationale for being exacting regarding proper character handling is that this process is useful for copying and logging random chunks of text from web pages, but if common characters like single quotes are not handled as expected, confidence in the system is much reduced.
I very frequently work on multiple items in parallel and end up running some long tests or regression after working on something. I usually add a mail -s "foo" id < /dev/null at the end to know when a task ends so that I don't have to baby sit a long test or regression.
I was trying to automate this, where in I don't have to type the mail every time, just call a script (alias this to fewer characters - optimizing on how much I type) and the script figures out the test/regression from the cwd and sends an email. I thought it would be useful to send the terminal title or the screen session name in the email. Is there a way I can extract the terminal title or Linux screen window name?
You can find the screen session name, when attached, in $STY (the window number is available in $WINDOW if you need it):
$ echo "$STY"
6367.sessionname
You can find the uniquely identifying tty/pty device with tty:
$ tty
/dev/pts/34
Titles and such are features of xterm and not of the terminal itself, so terminals programs have a hard time deducing it.
Here's an example using both of the above to show the screen name if any, or the tty device otherwise:
mail -s "${STY:-$(tty)} done" < /dev/null
Screen also has a "wait for silence" feature where you can get a notification when processes in other screen windows have stopped outputting.
I am building a interactive bash script and I have been using whiptail for input and message boxes. However the infoboxes will not always work. (Works on my active server but not on my VM. Same Ubuntu 14.04 accessed with Putty in windows)
I am trying to switch to dialog but the code I was using with whiptail to output to a variable rather than a file does not seem to work in dialog.
UNAME=$(whiptail--inputbox "Enter the user you want your scripts to run as. (Case sensitive, Must exist)" 10 50 --title "System Username" 3>&1 1>&2 2>&3)
I tried changing whiptail to dialog and I get a box and can enter and submit data but then the variable is not set. I got this from another forum and there was no real description of whats actually happening here. All I know is it sets the variable with the input data rather than what is normally output to stderr.
dialog appears to be sensitive to option ordering. Put the --title option before the --inputbox option and it should work (at least it does here).
I'm working on an embedded linux system; my console is a serial port and there is a VGA video output at /dev/tty0.
I am trying to use the 'dialog' (1) utility to display menu-like displays on the VGA screen.
I can get dialog output on the console:
dialog --inputbox "Hello Dialog World" 10 30
and I can get something similar on the VGA screen by redirection:
dialog --inputbox "Hello Dialog World" 10 30 >/dev/tty0
But I want to use the '--output-fd' parameter to dialog to achieve a similar result and I can't get it to work.
I've tried things in a bash script like:
exec 4>/dev/tty0
dialog --output-fd 4 --inputbox "Hello Dialog World" 10 30
exec 4>&-
but this seems to write to the console as previously.
I seem to be misunderstanding the operation of the --output-fd parameter - can anyone help?
(in actual fact, I want to use the python-dialog wrapper for dialog; but I need to understand how to use the underlying dialog utility before I take the next step)
Quote from the manual page:
Some widgets, e.g., checklist, will write text to dialog’s output.
Normally that is the standard error, but there are options for
changing this: "--out-put-fd", "--stderr" and "--stdout".
This means that the --output-fd option does not specify a file descriptor for any output but only for the data generated by some widgets.