fish shell disable all prompts temporarily - prompt

I wanted to create a simple text entry system to provide input to a script. I created a helper function:
function get_input -a prompt var_name -d 'get user input and place it in var_name'
echo -n "$prompt"
read --global $var_name
echo ""
end
but I have a fairly lengthy prompt setup, so my read prompt looks ugly:
tsrep prod2 d235108 ~> nsstltlb13 d235108#nsda3bpldv40 ~/.c/f/p/fishdots_notes> get_input 'hello world' charlie
hello world
tsrep prod2 d235108 ~> nsstltlb13 read> bonjour le monde!
So I tried to disable the fish_prompt function, using a rename:
function get_input -a prompt var_name -d 'get user input and place it in var_name'
functions -c fish_prompt fish_prompt_tmp
functions -e fish_prompt
echo -n "$prompt"
read --global $var_name
echo ""
functions -c fish_prompt_tmp fish_prompt
functions -e fish_prompt_tmp
end
but it had absolutely no effect.
What am I missing?

read uses its own prompt, and does not call fish_prompt.
You can specify the prompt for read with an option:
read --global --prompt-str="$prompt" $var_name
You can also use a real command:
read --global --prompt='echo something: ' $var_name

Related

How do -s and -p alter the read command?

I'm trying to interpret this block of code. Searched google to see what these commands mean and no luck. I put my interpretation of what each line/block means to me. If I am wrong, please correct me. I am new to unix commands. Code:
#!/bin/bash
# input 1st command line argument for the version.
export VERSION=$1
# if user didn't input a version, print the echo message and exit (not sure what -n means but I am assuming)
if [[ ! -n "$VERSION" ]]; then
echo "Missing Version"
exit 1
fi
# creating variable UNAME that tells who the person is (their name)
export UNAME='whoami'
# no idea what -s and -p mean but i think this prints the message "enter password for $UNAME" and stores it in a new variable named PASSWORD. the $UNAME will print whatever whoami said.
read -s -p "Enter password for $UNAME: " PASSWORD
echo ""
The -p flag issues a prompt before reading input into a variable
The -s flag stop the typed response from being shown (i.e. for a sensitive password)
More information is available here:
https://linuxhint.com/bash_read_command/
-p
prompt output the string PROMPT without a trailing newline before
attempting to read.
-s
do not echo input coming from a terminal.

Linux shell script "read" command

So, I'm new to scripting, and I'm having some problems. The command I need to execute is:
read -p Enter_the_DEVICE_Bssid "device1" ;
read -p Enter_the_DEVICE_Bssid "device2" ;
read -p Enter_the_DEVICE_Bssid "device3"
That command works, but when I set it as a variable ie:
com="read -p Enter_the_DEVICE_Bssid "device1" ;
read -p Enter_the_DEVICE_Bssid "device2" ;
read -p Enter_the_DEVICE_Bssid "device3""
and execute it as: $com it does not work. Probably because the read command is trying to set my input to the variables device1 and ; .
Any ideas on how to fix it?
You're running into problems with the order in which things are expanded by the shell.
A simpler example:
$ command='echo one ; echo two'
$ $command
one ; echo two
The semicolon in the value of $command is taken as part of the argument to echo, not as a delimiter between two echo commands.
There might be a way to resolve this so it works the way you want, but why bother? Just define a shell function. Using my simple example:
$ command() { echo one ; echo two ; }
$ command
one
two
$
Or using yours:
com() {
read -p "Enter_the_DEVICE_Bssid: " device1
read -p "Enter_the_DEVICE_Bssid: " device2
read -p "Enter_the_DEVICE_Bssid: " device3
}
Note that I've added ": " at the end of the prompts. I've also removed the unnecessary semicolons and the quotation marks around the variable names (since the argument has to be a valid variable name, it doesn't need to be quoted).
You are not completing the quotes.
com="read -p Enter_the_DEVICE_Bssid "device1"
Quotes always look for a pair and you are missing that.
> com="read -p Enter_the_DEVICE_Bssid: device1"
> $com
Enter_the_DEVICE_Bssid:abc123
> echo $device1
abc123
Here I am using bash shell.

read -p returns "read: no query process" using korn shell ksh

created a simple shell file that contains this:
read -p ThePrompt TheSomthing
echo $TheSomething
Run it, and it returns
-ksh[1]: read: no query process
I've tried single quotes, double quotes around ThePrompt and the man page specifically says "-p" is to use a prompt but it is not working for me. Can anyone tell me what I'm doing wrong? Thanks!
In Ksh you can use this format:
echo "ThePrompt\c"
read TheSomthing
echo $TheSomething
From the googled man page:
The -un and -p options cause input to be read from file descriptor n or the current co-process (see Co-Processes above for comments on this), respectively. If the -s option is used, input is saved to the history file.
To use a prompt, write this instead:
read TheSomething?'ThePrompt'
I found a word around:
echo -n 'prompt: '
read input1
echo -n 'prompt: '
read input2
.
.
.
I don't know why the -p doesn't work as described in the man page. If anyone out there has insights, please reply.
Thanks!
Sorry for reviving this question, but I do my shell scripts in KSH, so I was in the same predicament, until I came with this.
My solution to capture a single character:
$> echo -e "My prompt: \c" ; read -n 1 -s -r FOO ; echo -e "\b"
My prompt:
$> echo $FOO
d
$>
For a longer string remove the "-n 1" from the read command:
$> echo -e "My prompt: \c" ; read -s -r FOO ; echo -e "\b"
My prompt:
$> echo $FOO
this is my entry!!!
$>
I hope this is what you were looking for... Cheers!

save wild-card in variable in shell script and evaluate/expand them at runtime

I am having trouble running the script below (in Cygwin on win 7 mind you).
Lets call it "myscript.sh"
When I run it, the following is what I input:
yearmonth: 2011-03
daypattern: 2{5,6,7}
logfilename: error*
query: WARN
#! /bin/bash
yearmonth=''
daypattern=''
logfilename=''
sPath=''
q=''
echo -n "yearmonth: "
read yearmonth
echo -n "daypattern: "
read daypattern
echo -n "logfilename: "
read logfilename
echo -n "query: "
read q
cat "$yearmonth/$daypattern/$logfilename" | grep --color $q
The output I get is:
cat: /2011-03/2{5,6,7}/error* No such
directory of file exists.
However, if I enter daypattern=25 OR daypattern=26 etc. the script will work.
Also, of course if I type the command in the shell itself, the wildcards are expanded as expected.
But this is not what I want.
I want to be able to PROMPT the user to enter the expressions as they need, and then later, in the script, execute these commands.
Any ideas how this can be possible?
Your help is much appreciated.
Try eval, this should work for the {a,d} and * cases
eval grep --color $q ${yearmonth}/${daypattern}/${logfilename}
Use quote to prevent wildcard expansion:
$ a="*.py"
$ echo $a
google.py pair.py recipe-523047-1.py
$ echo "$a"
*.py

How to open an editor from a bash function?

I have a simple function to open an editor:
open_an_editor()
{
nano "$1"
}
If called like open_an_editor file.ext, it works. But if I need to get some output from the function — smth=$(open_an_editor file.ext) — I cannot see the editor, script just stucks. What am I missing here?
Update: I am trying to write a function which would ask the user to write a value in editor, if it wasn't given in script arguments.
#!/bin/bash
open_an_editor()
{
if [ "$1" ]
then
echo "$1"
return 0
fi
tmpf=$(mktemp -t pref)
echo "default value, please edit" > "$tmpf"
# and here the editor should show up,
# allowing user to edit the value and save it
# this will stuck without showing the editor:
#nano "$tmpf"
# but this, with the help of Kimvais, works perfectly:
nano "$tmpf" 3>&1 1>&2 2>&3
cat "$tmpf"
rm "$tmpf"
}
something=$(open_an_editor "$1")
# and then I can do something useful with that value,
# for example count chars in it
echo -n "$something" | wc -c
So, if the script was called with an argument ./script.sh "A value", the function would just use that and immediately echo 7 bytes. But if called without arguments ./script.sh — nano should pop up.
If the input you need is the edited file, then you obviously need to cat filename after you do the open_an_editor filename
If you actually need the output of the editor, then you need to swap stderr and stdin i.e:
nano "$1" 3>&1 1>&2 2>&3
If yo need 'friendly' user input, see this question on how to use whiptail
if you need to get output from function and store in variable, you just display what's in file.
open_an_editor()
{
cat "$1"
}
smth=$(open_an_editor file.txt)
If all you want is for a user to enter a value then read is enough:
OLDIFS="$IFS"
IFS=$'\n'
read -p "Enter a value: " -e somevar
IFS="$OLDIFS"
echo "$somevar"

Resources