LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 04-18-2012, 01:38 PM   #1
roopakl
Member
 
Registered: Sep 2011
Posts: 92

Rep: Reputation: Disabled
print user's input with color+bold+italic in bash


Hi..All,
Is it possible to print out the user's inputs with color+bold+italic?
I would like to print the user's all inputs in color+bold+italic while he/she answering to "read" command. Here is a small script.
Code:
#!/bin/bash
read -p "Enter the file extension that you want to search
$(printf "Example-  \"*.jpg\" or \"*.iso\" and so on\n"): " ext
echo "You have choose \"$ext\" "
echo
read -p "Enter the path that you want to search in
$(printf "Example-  \"/var/www\" or \"/home\" and so on\n") : " path
echo "You have choose to search in \"$path\" "
Now whatever user answers, I wanted it to be in redcolor+bold+italic
Code:
# bash test
Enter the file extension that you want to search
Example-  "*.jpg" or "*.iso" and so on: *.avi
You have choose "*.avi" 

Enter the path that you want to search in
Example-  "/var/www" or "/home" and so on : /home
You have choose to search in "/home"
In the above I have selected *.avi and /home. So I wanted it to print out with redcolor+bold+italic while the user is answering to this script's question. I don't know how it can be done with bash. So please help me.
Thanks in advance for your kind help.

Last edited by roopakl; 04-18-2012 at 10:27 PM.
 
Old 04-19-2012, 05:04 AM   #2
DaneM
Member
 
Registered: Oct 2003
Location: Chico, CA, USA
Distribution: Linux Mint
Posts: 848

Rep: Reputation: 107Reputation: 107
Cool topic!

Hello, roopakl.

This is something I've really only dealt with in terms of user variables, but to my knowledge, the same principles apply. I'll try to solve your riddle. ;-)

I'll be pulling a lot of info from here:
http://ubuntuforums.org/showpost.php...7&postcount=72
and
http://www.understudy.net/custom.html
and
http://www.faqs.org/docs/abs/HTML/colorizing.html
and
http://stackoverflow.com/questions/5...rs-with-printf

Since scripts like .bashrc, which are sourced instead of executed (fine difference, as you probably know) are just BASH (or SH) scripts that are using variables with color values, I see no reason why you couldn't do the same thing with an echo command or similar. Here's an example that might work. (Keep in mind that my BASH is a bit rusty. I've never actually used printf in BASH, so please bear with my syntax.)

Code:
#/bin/bash
read -p "Enter the file extension that you want to search
$(printf "%b" "Example-  \"*.jpg\" or \"*.iso\" and so on:\E[1;3;31m") " ext
echo -e "\E[0mYou have chosen \"\E[1;3;31m$ext\E[0m\""

"%b" in printf is for interpreting escape codes, as is "-e" in echo. "\E[<number>;<number>;<number>m" is an escape sequence for "format; format; color". You can have as few as zero formats, or as many as you want. The sequence must end with "m". Different sequences can do the same job; you can substitute "\e" or "\[033" for "\E", but I think there are slight differences. Also, some sequences must end with "\]", and I'm not entirely sure why the above ones don't need it. I'm sure you can figure this out on your own (and tell me!) if you find it necessary to understand these things.

At this point, I must point out that, while the above code is correct for "bold; italic; red", italic doesn't show up correctly on my console. Unless your target machine/software deals with it better than mine does, I suggest you use different formatting.

One final thing: the "0m" sequences are to reset the colors/formatting before continuing the script. Without it, you'll have your entire script be the format and color of your last escape/color sequence.

Let me know how this works out for you!

--Dane

Last edited by DaneM; 04-19-2012 at 05:08 AM.
 
1 members found this post helpful.
Old 04-19-2012, 11:26 AM   #3
roopakl
Member
 
Registered: Sep 2011
Posts: 92

Original Poster
Rep: Reputation: Disabled
Thank you very much DaneM,
Excellent!!!
Wow, you could help me to make colorful for the user inputs.
I checked it, for me both bold and italic are not working and showing only red color. But this is enough for me. Thanks a lot.
I will follow the links that you are given.
Once again thank you very much for your kind help.

Last edited by roopakl; 04-19-2012 at 11:28 AM.
 
Old 04-19-2012, 11:30 AM   #4
DaneM
Member
 
Registered: Oct 2003
Location: Chico, CA, USA
Distribution: Linux Mint
Posts: 848

Rep: Reputation: 107Reputation: 107
Thumbs up Success!

You're quite welcome!

I'm glad it worked out well for you! I'm sure that if bold/italic becomes important in the future, you can find a terminal that works well for it. (Konsole does bold but not italic; I don't know about others.) Also, there are other styles (noted in the links) that could get you a similar effect if they work.

Have a good one.

--Dane
 
Old 04-20-2012, 10:04 AM   #5
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,576
Blog Entries: 31

Rep: Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195
Quote:
Originally Posted by DaneM View Post
Konsole does bold but not italic; I don't know about others
rxvt-unicode (a.k.a. urxvt) does bold, italic and underlining. In vim, the Light and Dark colour variants look different from the normal ones.
 
1 members found this post helpful.
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
how to print part of text of a text file in bold and color using shellscript swapna sree Linux - Newbie 25 07-12-2011 12:19 PM
I want my script output in bold and color fonts gdan2000 Linux - General 6 03-16-2009 07:29 AM
User input into Bash scripts and checking validity of user input?? helptonewbie Programming 8 07-07-2008 07:40 PM
bold italic and underline with kmail zosimus Linux - Software 10 04-08-2005 09:53 AM
how do bold and italic in vim? lugoteehalt Linux - Newbie 3 04-19-2004 08:17 AM


All times are GMT -5. The time now is 05:43 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration