display a variable with whiptail - linux

I want to display the value of a real-time variable in a whiptail interface.
I my incremented variable like that =
#!/bin/bash
i=1
while test $i -ne 51
do
echo "$i"
i=$(($i + 1)) #icremente i
done
how I can display the real-time value in a GUI with whiptail?

The usual way would be to pipe the results to a --gauge option (gauge widget). whiptail's manual page lists that.
whiptail implements a subset of dialog's options; if you were using dialog, one might suggest displaying successive --infobox messages (also part of the subset), but the effect with xterm may not be good -- too much flashing (dialog cancels the switch to xterm's alternate screen which many terminal descriptions use).
Here is a sample using the latter:
#! /bin/sh
: ${PROG=whiptail}
left=10
unit="seconds"
while test $left != 0
do
$PROG --title "INFO BOX" "$#" \
--infobox "Hi, this is $left $unit to read this..." 10 52
left=`expr $left - 1`
test $left = 1 && unit="second"
sleep 1
done
If you were to use that in xterm without disabling the alternate screen switching (e.g., setting the resource *titeInhibit:false) it would not work well, of course.

Related

Notify when gpio value is changed

I'm currently trying to poll gpio value with only shell script.
I basically developped the script with a test file before using /sys/class/gpio/gpioxx/value
This is the solution I found :
#!/bin/bash
SCRIPT_DIR=$(dirname $(readlink -f $0))
FILE_NAME=$SCRIPT_DIR"/fileTest"
while true
do
inotifywait -qq -e modify $FILE_NAME
read val < $FILE_NAME
echo $val
### do something here ###
done
This is working with a basic file but I have two problems with this solution.
1 - The "modify" event is triggered when the file is saved, not when the content of the file has changed. So if I'm writing the same value in the file, the event is triggered but it should not.
2 - I remarqued that this solution doesn't works for gpios, if I'm using a simple ascii file it works but when I use inotifywait on /sys/class/gpio/gpioxx/value it depends.
If I use echo value > /sys/class/gpio/gpioxx/value the event is detected, but if I configure the pin as an input and connect it to 3v3 or 0V nothing is triggered.
Does someone know how I could trigger this change using only scripts?
From linux/Documentation/gpio/gpio-legacy.txt:
"/sys/class/gpio/gpioN/edge"
... reads as either "none", "rising", "falling", or
"both". Write these strings to select the signal edge(s)
that will make poll(2) on the "value" file return.
So you can do:
echo input > /sys/class/gpio/gpioN/direction
echo both > /sys/class/gpio/gpioN/edge
Now, you have to find a command that call poll (or pselect) on /sys/class/gpio/gpioN/value. (I will update my answer if I find one)
This is tight loop solution (which is more resource intensive), but will do the trick if you got nothing better:
gpio_value=$(cat /sys/class/gpio/gpio82/value)
while true; do
value=$(cat /sys/class/gpio/gpio82/value)
if [[ $gpio_value != $value ]]; then
gpio_value=$value
echo "$(date +'%T.%N') value changed to $gpio_value"
fi
done
Example output:
13:09:52.527811324 value changed to 1
13:09:52.775153524 value changed to 0
13:09:55.439330380 value changed to 1
13:09:55.711569164 value changed to 0
13:09:56.211028463 value changed to 1
13:09:57.082968491 value changed to 0
I use it for debug purposes.
Actually, I usually use this one-liner more often:
printf " Press any key to stop...\n GPIO value: " ; until $(read -r -t 0 -n 1 -s key); do printf "\033[2D$(cat /sys/class/gpio/gpio82/value) " ; done ; echo
Again, for debug purposes.
You may use libgpiod that provide some useful tools to monitor GPIOs. However, you need to use new GPIO API available from Linux 4.8.

If I create a menu in Bash is command style history possible

I have no problem creating the menu - that is not what this question is about.
What happens however is I go from having the arrow keys be useful (scroll up and down to get access to previous commands I've run at the command line) to completely useless (^[[A^[[A^[[A^[[B^[[C^[[D^[[C)
Is there any way to encapsulate that behaviour into a menu?
E.g. can I use the scroll up and down keys to access previous options I've selected. (It's a BIG menu and I have MANY options like dev.client.alpha or dev.otherclient.beta etc...)
I supposed I could break each one into separate files and just use the command line diredtly OR I could pass an augment to the menu so as to call: ~/menu dev.clint.alpha directly from the command line.
Just curious is anyone else has (had) this itch and if anything has ever been done about it?
Menu I'm presently using is done basically as follows:
while :
clear
do
echo "$MENU"
read CHOICE ARG1 ARG2 ARG3 ARG4 overflow
case $CHOICE in
command.a)
# do stuff here
;;
command.b)
# do different stuff here
;;
*) # catch all...
continue
;;
esac
done
clear
You can do what you want by enabling readline on your read,
and appending each choice reply to the internal history. You can even save
the history to a file so when you rerun the script you have the old
history. For example:
HISTFILE=/tmp/myhistory
history -r # read old history
while :
do echo "MENU. a b q"
read -e # sets REPLY, enables readline
history -s "$REPLY" # add to history
history -w # save to file
set -- $REPLY
CHOICE=$1; shift
ARG1=$1 ARG2=$2 ARG3=$3 ARG4=$4; shift 4
overflow="$*"
case $CHOICE in
a) echo do stuff here $ARG1 ;;
b) echo do different stuff here ;;
q) exit ;;
*) echo catch all...
continue ;;
esac
done

bash script loses focus when displaying images

I am trying to figure out how to not lose focus in a shell script while simultaneously displaying an image. User input may come at any time, but seeing the photo taken, would seem important.
To Clarify, I have no problem outputing an image. display works fine, as does animate, and feh, etc.. what i need is for the shellscript to still process user input, (in this example, "t") while displaying the last image taken, for an undefined amount of time.
I'm writing in bash, in Linux.
Heres an example of what I'm trying:
#!/bin/bash
i=0
capture() {
cd ~/Desktop/ani
streamer -c /dev/video0 -s 800x600 -o outfile$i.jpeg
display outfile$i.jpeg &
let i++
}
while true; do
#clear
read -rsn1 input
if [ "$input" = "t" ]; then
capture
else
exit
fi
done
In the actual script I may continue to take photos, so I want to continue listening for user input. I can imagine a couple ways to do this, but I cannot figure it out.
To continue listening user input. you can do like
while true; do
#clear
read -p "Your input: " input
if [ "$input" == "t" ]; then
capture
fi
done
A rather ugly way to solve this: install the utility wmctrl (in debian/Ubuntu, sudo apt-get install wmctrl). Then, after your display command, add:
sleep 1
wmctrl -i -a "$WINDOWID"
This will sleep for one second (to leave some time to the display command to finish loading—tune this value to whatever feels right to you). Then, wmctrl will use the value of the variable WINDOWID (that is hopefully set by your terminal emulator) as a numeric value (-i) and raise the window and give it focus (-a).

Linux Script to execute something when F1 is hitter

I have this script start.sh
#!/bin/bash
while[1]
do
read -sn3 key
if [$key=="\033[[A"]
then
./test1
else
./test2
fi
done
I want to set up a forever loop check see if F1 key pressed. If pressed execute test1 else test2. I did start.sh & running in background so other programs can run.
I got error
while [1] command not found
syntax error near unexpected token 'do'
[f==\033]: command not found
Also where is this read command located? I type which read, it didn't find it.
Also, if try ./start.sh & it gives totally different behavior. I enter a key and it says that key is not found. I though & just run the script at background
There are several basic syntax problems in your code (consider using shellcheck before posting to clean up these things), but the approach itself is flawed. Hitting "q" and "F1" produces different length inputs.
Here's a script relying on the fact that escape sequences all come in the same read call, which is dirty but effective:
#!/bin/bash
readkey() {
local key settings
settings=$(stty -g) # save terminal settings
stty -icanon -echo min 0 # disable buffering/echo, allow read to poll
dd count=1 > /dev/null 2>&1 # Throw away anything currently in the buffer
stty min 1 # Don't allow read to poll anymore
key=$(dd count=1 2> /dev/null) # do a single read(2) call
stty "$settings" # restore terminal settings
printf "%s" "$key"
}
# Get the F1 key sequence from termcap, fall back on Linux console
# TERM has to be set correctly for this to work.
f1=$(tput kf1) || f1=$'\033[[A'
while true
do
echo "Hit F1 to party, or any other key to continue"
key=$(readkey)
if [[ $key == "$f1" ]]
then
echo "Party!"
else
echo "Continuing..."
fi
done
Should be
while :
or
while true
Try this:
#!/bin/bash
while true
do
read -sn3 key
if [ "$key" = "$(tput kf1)" ]
then
./test1
else
./test2
fi
done
It is more robust to use tput to generate the control sequence, you can see a full list in man terminfo. If tput isn't available, you can use $'\eOP' for most terminal emulators or $'\e[[A' for the Linux console (the $ is necessary with the string to make bash interpret escape sequences).
read is a bash builtin command - try help read.

How do i make the kdialog a certain width

Im trying to make my first bash script and im trying to use kdialog .
How do i make the progress bar a certain width
here is my attempt
dbusRef=`kdialog --title "Sweet As Buckup Demon" --progressbar "Initializing" 8`
qdbus $dbusRef Set "" value 1
qdbus $dbusRef setLabelText "Getting web site folder and creating mysqldump and grabbing configuration files from the apache Server"
cp -rf /usr/local/websites/sweetassurfwear /home/brett/sweetback/
sleep 4
qdbus $dbusRef Set "" value 2
cd /home/brett/sweetback/
And so on.. if you need the entire script i will post it
Basically at each part of the process it out puts text on the progress bar but the dialog keeps changing width.
How do i make the size standard
Use the --geometry argument, e.g.
kdialog --geometry 300x300+300+300 --title "Sweet As Buckup Demon" --progressbar "Initializing"
You can see the documentation for this option by typing:
kdialog --help-all|grep geometry
I was trying to use the --geometry option to widen an input box dialog and it wouldn't work. Eventually I found out that adding spaces to the end of the input box label forces the dialog to be wider, e.g. if you use
kdialog --title "Input dialog" --inputbox "Input"
You may get something like this:
But if you add extra spaces at the of the input box label:
# The expression $(printf "%0.s " {1..70}) adds 70 space characters to the label
kdialog --title "Input dialog" --inputbox "Input $(printf "%0.s " {1..70})"
You'll get something like this:
Unfortunately kdialog --geometry doesn't work. For a working example of how to fix up the geometry for a kdialog,
see https://github.com/rparkins999/kde-slow-start-wrapper.
Note this example sets the position: the width or height can be set by replacing the first or second -1 in the
wmctrl -r $mytitle -e
command.
I discovered a quirk in kdialog.
Try to start kdialog with the option
--progressbar "$(echo -e "\t\t\tYour window title here\t\t\t")"
Change the number of tabs (\t) in the window title to adjust the window width.

Resources