I am intending to underline the user input but after doing research, I still cant find the way to do it.
So far this is what I have got from doing research, but it still doesn't work.
Anyone knows how to solve it? or rather the proper method to do it?
What if I am using read -p "What is your name: " name instead of the one below? As what I am expecting for the output is.
What is your name? (the input from user and is underlined)
function underlineInput()
{
PS1='\033[4;47;40m'
}
function UNunderlineInput()
{
PS1='\033[0;47;40m'
}
function hi()
{
echo "Please enter your name: "
underlineInput
read input
underlineInput
}
Thanks to those who helped in advance!
Cheers!
Check out this link here
But its just:
$ echo -e "\033[4mThis is a underlined line.\033[0m"
The key parts are the \033[4m to underline and \033[0m to change it back.
Put the escape sequence to turn on underlining at the end of the prompt, then send the back-to-normal-sequence afterward:
read -p $'Please enter your name: \033[4m' name
printf '\033[0m' # Use printf instead of echo to avoid newline, and it translates escape without $''
BTW, if you want this script to work on other types of terminal as well, you can use the terminfo database to get the relevant escape (/whatever) sequences. The terminfo capability names for underlining on and off are "smul" and "rmul":
read -p "Please enter your name: $(tput smul)" name
tput rmul
Related
I haven't found the answer to this question anywhere so i am just going to ask it here.
I am looking for something like touch "..."
, is there something like a special character that replaces the enter key ?
I know this is really not useful for anything, but i am still wondering if it is possible.
To create a file with a\nb as name from the shell, do v.gr.
touch 'a
b'
You can check the file has a new line char into its name by issuing next command:
for fn in *; do echo "filename=<$fn>"; done
I've made a small function that I'm using when testing a script.
However I'm not sure it displays all characters, like spaces, quote, and stuff like that.
This is the function:
dbug () {
# showing result during debug
zenity --width 600 --height 100 --error --text="$#" --title="debug display"
}
And anywhere in the script where i want to check some variables I just add the line: dbug $variable and the pop-up window will show the result..
The question is, will the "$#" show all characters from the "call" command, or is something more fancy necessary?
One implementation that's capable of displaying nonprintable characters unambiguously would be:
dbug() {
local text_q
printf -v text_q '%q ' "$#"
zenity --width=600 --height=100 --text="$text_q" --title="debug display"
}
This will transform tabs to $'\t', newlines to $'\n'; will display spaces either within quotes or proceeded by backslashes; will double-up backslashes that are intended to be literal (when not displaying them in single quotes); and will otherwise ensure that the data is displayed is in a format where, if fed back to the shell as source code, it will evaluate to its literal values.
I'm using read builtin to read a variable, but I'd like to let the input appears on the next line, that is, the prompt output a new line, but neither of the two works:
$ read -p "Please input:\n" name
Please input:\n
$ read -p 'Please input:\n" name
Please input:\n
As you see new line escape sequence is not interpreted even in the double quote case. So is there anyway to do that?
You can separate the prompt from the actual read :
echo "Please input:"
read name
You can put both on a single line :
echo "Please input:" ; read name
You can also use a different form of quoting :
read -p $'Please input\n' name
This is barely shorter, and many would probably find it a bit less readable, but that is a matter of taste.
I have been trying to get linux to count the characters in a string and then out put them i want the user to be able to enter a string and the amount of characters in a string to be outputed however i have a limited understanding of linux so i really need your help thank you!
so far i have got this:
#!/bin/bash
x="This is a test"
y="${x//[^s]}"
echo "$y"
echo "${#y}"
but that only does it for one type of character and it's not in a while loop that will allow the user to quit if they wan to if you can help it would be appretiated
an example input would be "i like pie"
i would want the program to output "the string you have entered has 10 characters
You can use read to get input from user and then use ${#var} to get the length:
#!/bin/bash
read -p "Enter some input text: " input
echo "# of chars in input: ${#input}"
I want to ask the user to enter a few lines of text, it can by anything and I want to store it as a variable that I can call later on. I don't want to create multiple read commands, just one that can hold multiple paragraphs if needed.
I tried this:
echo "Enter your your paragraph:"
read -d '' -n 1 message
while read -d '' -n 1 -t 2 c
do
message+=$c
done
echo ""
echo "$message"
the output is always put into one line of text without spaces or anything. It would look like this when I run the code and enter a few lines of code:
Enter your broadcast message (When done, wait 2 seconds):
This is supposed to be a sentence.
And so is this.
Thisissupposedtobeasentence.Andsoisthis.
It should output the two sentences on sperate lines and with spaces included.
Don't use read for this; requiring all typing to be done without any two-second pauses (and conversely, forcing a wait of two seconds to complete the input) is not very user-friendly. Instead, just read input directly from standard input, which for interactive use simply requires an EOF (Control-d) to finish the input.
c=$(</dev/stdin)
read uses the characters in $IFS as word delimiters. Change your read statement to:
IFS= read -r -d '' -n 1 -t 2 c