LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 12-12-2004, 04:03 PM   #1
rootking
Member
 
Registered: Sep 2002
Distribution: FreeBSD
Posts: 70

Rep: Reputation: 15
Question BASH Scripting question


I'm creating a BASH script to make transitioning to different wireless networks easier. I want to make it so that when I ask the user a question and he inputs his answer that it will convert what he's inputed to lower case. That way if they accidentaly input a capital the script won't be messed up.

I'm also wondering if anyone knows how to make a BASH script that will show three dots (...) repeating over and over until a certain process finishes. I basically want them to be like when a program is loading and it shows the dots repeat until it finishes. This will make the script more animated.

If anyone can help me with either of these I would be greatly appreciated.

thanks
rootking
 
Old 12-12-2004, 05:32 PM   #2
twantrd
Senior Member
 
Registered: Nov 2002
Location: CA
Distribution: redhat 7.3
Posts: 1,440

Rep: Reputation: 52
For the lower case string question:

1. Save his answer to a variable
2. use 'sed' to replace his input string to lower caes
3. Then check if his answer is valid.

That's what I would do.

As for the progress meter, I'm dying to know how to do that in bash as well.

-twantrd
 
Old 12-12-2004, 06:29 PM   #3
bigrigdriver
LQ Addict
 
Registered: Jul 2002
Location: East Centra Illinois, USA
Distribution: Debian stable
Posts: 5,908

Rep: Reputation: 356Reputation: 356Reputation: 356Reputation: 356
Or, you could pipe the user input through tolower, which will convert whatever it's given to lower case. The companion function is toupper, to convert to upper case.
 
Old 12-12-2004, 10:31 PM   #4
homey
Senior Member
 
Registered: Oct 2003
Posts: 3,057

Rep: Reputation: 61
Here's what the progress dots may look like. You can control the speed by increasing the sleep command. Also, the duration is controlled by ( num< ? )

Code:
#!/bin/bash

echo -n "Program is loading, please wait "
for (( num=1; num<20; num++ )) do
echo -n .
sleep 1
done
echo " Done"
echo
 
Old 12-13-2004, 01:48 AM   #5
twantrd
Senior Member
 
Registered: Nov 2002
Location: CA
Distribution: redhat 7.3
Posts: 1,440

Rep: Reputation: 52
For the progress script, it will print "done" after 20 seconds. What if the program lasts more than 20 seconds (ie. rm -rf /).

-twantrd
 
Old 12-13-2004, 05:20 AM   #6
/bin/bash
Senior Member
 
Registered: Jul 2003
Location: Indiana
Distribution: Mandrake Slackware-current QNX4.25
Posts: 1,802

Rep: Reputation: 47
Code:
#!/bin/bash

Timer()
{
$PROCESS &
until [ z"`pidof $PROCESS`" = "z" ]
  do sleep 1 && echo -n "."
done
}

PROCESS="sleep 10"
Timer
Of course you will replace PROCESS with whatever program you are running. I just use sleep 10 to show how it works.
 
Old 12-13-2004, 07:40 PM   #7
twantrd
Senior Member
 
Registered: Nov 2002
Location: CA
Distribution: redhat 7.3
Posts: 1,440

Rep: Reputation: 52
Thanks for the input /bin/bash but it didn't work. I have a shell script called "apache_log_check" that just looks at the apache error_log and report anything it finds via mail command.

When I replace $PROCESS with the location of my script, I see no progress lines '...'. Can it display the progress of shell scripts?

-twantrd
 
Old 12-13-2004, 11:13 PM   #8
rootking
Member
 
Registered: Sep 2002
Distribution: FreeBSD
Posts: 70

Original Poster
Rep: Reputation: 15
thanks a lot guys. It works great. I still need to find toupper. I can't get a man page on it and I don't see it in YAST to install as well as syntax on google. any ideas? My next research is finding out how to change the color of text.

thank again
rootking
 
Old 12-14-2004, 01:23 AM   #9
heema
Senior Member
 
Registered: Sep 2003
Location: Egypt
Distribution: Arch
Posts: 1,528

Rep: Reputation: 47
changing the text is not hard

this example changes the text to red

echo -e "\033[0;31mHelloo\033[0m"

but i cant remember the page that i got it from as the value 33 is red but i dont know the other colours
 
Old 12-14-2004, 01:44 AM   #10
rootking
Member
 
Registered: Sep 2002
Distribution: FreeBSD
Posts: 70

Original Poster
Rep: Reputation: 15
Quote:
Originally posted by heema
changing the text is not hard

this example changes the text to red

echo -e "\033[0;31mHelloo\033[0m"

but i cant remember the page that i got it from as the value 33 is red but i dont know the other colours
Yeah, I figured it out. I just need to figure out how to change the input to lower case with tolower. I don't see how to use that command anywhere in SuSE.
 
Old 12-14-2004, 02:19 AM   #11
rhoekstra
Member
 
Registered: Aug 2004
Location: The Netherlands
Distribution: RedHat 2, 3, 4, 5, Fedora, SuSE, Gentoo
Posts: 372

Rep: Reputation: 42
Quote:
Originally posted by rootking
thanks a lot guys. It works great. I still need to find toupper. I can't get a man page on it and I don't see it in YAST to install as well as syntax on google. any ideas? My next research is finding out how to change the color of text.

thank again
rootking
rootking, try 'tr'.

'echo Hello | tr "a-z" "A-Z" ' Produces: HELLO
'echo Hello | tr "A-Z" "a-z" ' Produces: hello

Hope this helps

Coloring is done with ANSI codes, as commented above by others.
two ways to have the ANSI 'escape' character: echo -e "\033[0m" could do it (-e allowing escape codes,
or echo "^V<esc>[0m" (press Cntl-V followed by ESCAPE) to have it produce the same result.

Colors?

0 = default color
30 = black
31 = red
32 = green
33 = brown (or: dark yellow)
34 = blue
35 = magenta
36 = cyan
37 = grey
30;1 = dark grey (actually: bright black)
31;1 = bright red
32;1 = bright green
33;1 = yellow (actually: bright brown)
34;1 = bright blue
35;1 = bright magenta
36;1 = bright cyan
37;1 = white (actually: bright grey)

replace the thirty numbers to fourty numbers to change the background coloring

Check it out with:
Code:
for i in `seq 40 47`;do 
 for j in `seq 30 37`;do 
  echo -e "\033[${i};${j}mColor front=$j, back=$i\033[0m"
 done
done

Last edited by rhoekstra; 12-14-2004 at 02:34 AM.
 
Old 12-14-2004, 10:56 PM   #12
rootking
Member
 
Registered: Sep 2002
Distribution: FreeBSD
Posts: 70

Original Poster
Rep: Reputation: 15
Quote:
Originally posted by rhoekstra
rootking, try 'tr'.

'echo Hello | tr "a-z" "A-Z" ' Produces: HELLO
'echo Hello | tr "A-Z" "a-z" ' Produces: hello

Hope this helps

Coloring is done with ANSI codes, as commented above by others.
two ways to have the ANSI 'escape' character: echo -e "\033[0m" could do it (-e allowing escape codes,
or echo "^V<esc>[0m" (press Cntl-V followed by ESCAPE) to have it produce the same result.

Colors?

0 = default color
30 = black
31 = red
32 = green
33 = brown (or: dark yellow)
34 = blue
35 = magenta
36 = cyan
37 = grey
30;1 = dark grey (actually: bright black)
31;1 = bright red
32;1 = bright green
33;1 = yellow (actually: bright brown)
34;1 = bright blue
35;1 = bright magenta
36;1 = bright cyan
37;1 = white (actually: bright grey)

replace the thirty numbers to fourty numbers to change the background coloring

Check it out with:
Code:
for i in `seq 40 47`;do 
 for j in `seq 30 37`;do 
  echo -e "\033[${i};${j}mColor front=$j, back=$i\033[0m"
 done
done
Alright, Thanks for your help. Here's what I need to do specifically with the tr section. I need to convert the input a user gives to lower case. Here is what I have and it's not working. I feel like the logic is correct but the syntax might not be. How can you help me.

Code:
read -p "Would you like to load one of these? [y/n] " QUEST1
QUEST2=echo -n $QUEST1 | `tr "A-Z" "a-z"`
if [ "$QUEST2" = y ];
then

# the rest of the script
I ask the user a question and take their response with the read command and place it in the form of the variable QUEST1. I then assign a new variable QUEST2 the output of QUEST1 turned into lower case. I finally have an if statement that takes the value they chose of "y" or "n" and carries out the rest of the command accordingly. What am I doing wrong here. Perhaps I don't need to use any of this tr stuff and there is a way to specify in my if statement to check for Y or y. This way it will cover the bases. I'm not sure if I can do an or with the if statement.

thanks
rootking
 
Old 12-15-2004, 01:56 AM   #13
rhoekstra
Member
 
Registered: Aug 2004
Location: The Netherlands
Distribution: RedHat 2, 3, 4, 5, Fedora, SuSE, Gentoo
Posts: 372

Rep: Reputation: 42
Quote:
Code:
read -p "Would you like to load one of these? [y/n] " QUEST1
QUEST2=echo -n $QUEST1 | `tr "A-Z" "a-z"`
if [ "$QUEST2" = y ];
then

# the rest of the script
I see backticks with the tr command...

Try this and find the difference (look at the backticks):

Code:
read -p "Would you like to load one of these? [y/n] " QUEST1
QUEST2=`echo -n $QUEST1 | tr "A-Z" "a-z"`
if [ "$QUEST2" = y ];
then

# the rest of the script
 
Old 12-15-2004, 08:39 AM   #14
/bin/bash
Senior Member
 
Registered: Jul 2003
Location: Indiana
Distribution: Mandrake Slackware-current QNX4.25
Posts: 1,802

Rep: Reputation: 47
When I replace $PROCESS with the location of my script, I see no progress lines '...'. Can it display the progress of shell scripts?

-twantrd


No not the way it is written. The pid of a shell script is the pid of the shell. So you may have several bash shells running and you would need to look through /proc to find the one that is running your script. One way to do it would be to use the $! variable. In bash $! refers to the last process put in the background. So you could modify the script like this to make it work:

Code:
#!/bin/bash

Timer()
{
$PROCESS &
PROC_PID=`echo $!`

while [ -d /proc/$PROC_PID ]
  do sleep 1 && echo -n "."
done
}

PROCESS="/path/to/script"
Timer
That should work with scripts.

Last edited by /bin/bash; 12-15-2004 at 08:44 AM.
 
Old 12-15-2004, 12:16 PM   #15
twantrd
Senior Member
 
Registered: Nov 2002
Location: CA
Distribution: redhat 7.3
Posts: 1,440

Rep: Reputation: 52
Ahhh. Thank you very much /bin/bash!! I have been trying to write a progress bar like so for a while and failed.

-twantrd
 
  


Reply



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
Bash scripting question ajaye1971 Linux - Newbie 1 11-16-2005 07:29 PM
Bash scripting question. pete1234 Programming 23 09-22-2005 06:52 PM
bash scripting question mehesque Programming 2 03-07-2004 01:37 PM
Bash scripting question. welby Linux - Software 1 01-14-2004 10:05 AM
Bash Scripting Question RefriedBean Programming 6 09-11-2002 09:06 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 05:56 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
Open Source Consulting | Domain Registration