Exec dialog command from Expect Script - linux

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

Related

Xterm: How can I direct input from one terminal tab to another?

As I am a beginner coder, I apologize in advance for improper terminology.
This is the main script which calls the script ping.sh in a new tab.
#!/bin/bash
echo "The script is running!"
rm ping.txt
echo "Enter your desired IP address:"
read ADDRESS
osascript -e 'tell application "System Events" to tell application "Terminal"
do script "./ping.sh"
end tell'
echo "The script has ended!"
exit 0;
So, as I said the script ping.sh is called now. It goes like this.
#!/bin/bash
echo "Welcome to the new tab!"
ping -c 3 $ADDRESS > ping.txt
exit 0
The problem I have is that the read input from the first tab isn't recognizable in the second tab. Is there a way to solve this? I am probably missing a linking constructor or something like that. Please help!
I have no idea what osascript is, or how it works, but it might be helpful to know that shell scripts can access command line arguments with the special variables $1, $2, $3, etc.
This means you can rewrite your ping.sh script like so:
#!/bin/bash
echo "Welcome to the new tab!"
ping -c 3 "$1" > ping.txt
exit 0
And then call it like so:
#!/bin/bash
echo "Enter your desired IP address:"
read ADDRESS
./ping.sh "$ADDRESS"
Otherwise, to make sure subsequent commands have access to the same environment variables, you have to export them. From help export:
export: export [-fn] [name[=value] ...] or export -p
Set export attribute for shell variables.
Marks each NAME for automatic export to the environment of subsequently
executed commands. If VALUE is supplied, assign VALUE before exporting.
To make your original ping.sh work you could do the following:
#!/bin/bash
echo "Enter your desired IP address:"
read ADDRESS
export ADDRESS
./ping.sh

Random text in linux terminal

Sup guys. How I can make terminal to show the text that I want ? And how to edit the text that is already displayed
For example terminal is showing now:
user#host: sudo writetext
bash: writetext: command not found
How to edit this text to be displayed like this
user#host: sudo writetext 5
writelext line 1 executed
writelext line 2 executed
writelext line 3 executed
writelext line 4 executed
writelext line 5 executed
I don't need the program to work, i just need to know how to display random text in terminal
You can add an alias to the bashrc
vim ~/.bashrc
Go to the end of the file
add line: alias writeText='echo "write text executed"'
Then reload the bashrc with: source ~/.bashrc
After this you should be able to call the alias by typing in writeText
Here you can also add a more advanced echo function.
If you want to pass parameters you have to write a separate function as described here:
Passing argument to alias in bash
Write a shell script and add echo commands inside to display whatever u want to display
There are many ways to print text to stdout, you should read some man pages:
man echo
man print
man printf
more powerful tools:
sed, awk ...
Examples:
seq
kent$ seq -f "whatever %g" 5
whatever 1
whatever 2
whatever 3
whatever 4
whatever 5
awk
kent$ awk -v v=5 'BEGIN{for(i=1;i<=v;i++)print "whatever "i}'
whatever 1
whatever 2
whatever 3
whatever 4
whatever 5
If you're trying "make the terminal display text that I will type."
You could try read, assign a variable to the read and then echo it
read text
echo "${text}"

Yad password dialog window cases

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

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?

Resources