How do i make the kdialog a certain width - linux

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.

Related

Make a bash script that refresh values without scrolling down the terminal like TOP command

How to set a bash script to refresh the screen with variable values like TOP command?
To show the variable value I use:
echo "$var"
But it prints a new line, instead of I would like something that refresh the screen
How to achieve this?
The easiest way is to just use watch:
watch date
You can also just call clear before each iteration. Here, all the output from clear and any commands are collected as a form of double buffering to reduce flickering:
show_things() {
date
uname -a
echo "Your lucky number is $RANDOM"
}
while sleep 1
do
printf '%s\n' "$(clear; show_things)"
done

Why is formatting for blocks of text showing up weirdly?

I was wondering if you could help me with something.
I want to write a menu for one of the scripts I'm working on. Currently, my script is:
echo "########################################"
echo "# Script tasks #"
echo "# #"
echo "# 1 Show running processes #"
echo "# 2 Show logged in users #"
... (continued)
echo "########################################"
However, when I run this inside of my script, for some reason some of the # signs at the end of the line get either indented into the box, or pushed further out, resulting in the right side of the box looking very bad and not well thought-out. I would like the right side of the box to look even (i.e., actually like a box).
I'm using Bash on Ubuntu 14.04 LTS and gedit as my text editor.
This isn't your question, but the way to do menus in a shell script is with the select command
# an array with the menu choices
choices=(
"Show running processes"
"Show logged in users"
"..."
)
# define the prompt
PS3="What is your choice? "
# display and get user input
# select is an infinite loop: `break` when you've done something successfully
select choice in "${choices[#]}"; do
case "$choice" in
"Show running processes")
some code here
break
;;
"Show logged in users")
some code here
break
;;
*)
echo "Please select a number from the menu."
# do not break, select will re-display
;;
esac
done
# if you need the choice afterward, you still have it
echo "$choice"

display a variable with whiptail

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.

current directory doesn't appear in title bar when running under screen

My xterm $prompt variable in my .tcshrc is:
set prompt="%{\033]0;%m:%~\007%}%{^[[;37;1m%}%B%{^[[;34;1m%}%m%{^[[;34;1m%}:%b%c%# "
The highlighted part above (%{\033]0;%m:%~\007%}) puts the hostname (%m) and the current directory (%~) in the title bar. (At least I think that that's what puts it in the title bar; it's been a while since I fiddled with this prompt).
When I run screen, however, the current directory stops getting updated when I change directories.
My questions:
How can I make this prompt work in screen?
Is there a better way to display the current directory in the title bar?
I am running linux with xterm and tcsh.
I think there is no direct way, because of the way screen works. However screen can display its own status bar, that you can define in .screenrc. Here's mine for instance :
hardstatus alwayslastline
hardstatus string '%{= kG}[ %{G}%H %{g}][%=%{=kw}%?%-Lw%?%{r}(%{W}%n*%f%t%?(%u)%?%{r})%{w}%?%+Lw%?%?%= %{g}][%{B}%Y-%m-%d %{W}%c %{g}]'
Firstly, to make it work you must check where exactly is the line with set prompt=blah-blah in your .tcshrc. For example, the code below that perfectly works in plain xterm would not work under screen in xterm:
switch ($TERM)
case "xterm*":
set prompt="%{\033]0;${HOME:t}#%m:%l:%c08\007%}%{\033[36m%}%l:%c02%#%{\033[0m%} "
# update xterm title to display current cmd in it
alias postcmd 'echo -n "\033]0;${HOME:t}#`hostname`:${tty} \!#:q\007"'
...
because screen by default sets $TERM variable to screen and not xterm! So you must add:
case "screen":
# lame, but prevents an error in screen after 'su - root'
if (! $?WINDOW) setenv WINDOW 1
set prompt="%{\033]0;${HOME:t}#%m:${WINDOW}:%c08\007%}%{\033[36m%}%c02%#%{\033[0m%} "
alias postcmd 'echo -n "\033]0;${HOME:t}#`hostname`:${WINDOW} \!#:q\007"'
...
Secondly, make sure yo have this line in ~/.screenrc:
termcapinfo xterm* 'hs:ts=\E]2;:fs=\007:ds=\E]2;\007'

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