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 - 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 11-22-2010, 04:56 PM   #1
Slightly Disoriented
Member
 
Registered: Sep 2010
Distribution: Ubuntu 10.10
Posts: 87

Rep: Reputation: 2
Bash scripting.


Okay, I am trying to learn Bash. I have really no other programming experiance besides HTML and a little bit of BASIC. I have wrote a script to do something simple. Just mount my NTFS Windows partition, update my system, then run a couple of my of programs that I usually run. I have two questions. One, I want to give it the option to ask me to ask me if I want to update my system, and when I try that, it doesn't. Could you please look at the script and advise me? Also, I want it to start Google Chrome, Thunderbird, and then Rythmbox. Although, even though I think the script is correctly, it doens't work, it starts Chrome, and thats it. I think it has something to do with the Terminal because it also will not clear when I give it the command to in the script. I have the script attached so if anyone wants to help me, it's attached.
 
Old 11-22-2010, 04:59 PM   #2
Slightly Disoriented
Member
 
Registered: Sep 2010
Distribution: Ubuntu 10.10
Posts: 87

Original Poster
Rep: Reputation: 2
Whoops, forgot attach. Here you go. It would not let me upload a .sh, so it's .txt
Attached Files
File Type: txt Main.txt (1.8 KB, 17 views)
 
Old 11-22-2010, 05:09 PM   #3
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 374Reputation: 374Reputation: 374Reputation: 374
Code:
sudo mount /dev/sda3 /media/OS & echo "OS will be mounted"
Two things:
1. Unless you have sudo configured to allow your account to execute the mount command with no password, the script will pause--waiting for you to enter your password so sudo can authenticate

2. You don't want the single ampersand here. You want a double-ampersand. The double will cause the script to execute the "OS will be mounted" command only if the earlier mount command was successful. The single ampersand as you have it causes the sudo-mount command to execute in the background.

Code:
sudo apt-get update && gnome-terminal sudo apt-get upgrade
Not really a problem, but you should probably set up password-less permission for apt-get as well (as in #1 above)

Code:
clear & echo "No programs will be killed"
Same deal as #2 above, but not very problematic because clear doesn't require user input.

Code:
echo "Google Chrome will be started."
google-chrome
You need to add a single ampersand after "google-chrome"--this causes chrome to start in the background and the script continues. Without it, your script will wait until chrome is closed before continuing.

Similarly,
Code:
thunderbird
and
Code:
rhythmbox
Also need a single ampersand after each to allow them to execute in the background and allow your script to continue.

EDIT: perhaps it's easier to do it this way--changes are in red:
Code:
#!/bin/bash
#Program that mounts OS, then starts desired programs, based on users response. Also grabs updates, and has the option of upgrading.
#Unmounts just because.
sudo umount /media/OS
#Mounts OS
clear 
#Asks User if OS should be mounted.
echo "Would you like to mount OS? (y/n)"
read -t3 ifmount
if [ $ifmount = "y" ]
then
#	Perhaps this would be better:
#	echo "OS will be mounted"
#	sudo mount /dev/sda3 /media/OS
	sudo mount /dev/sda3 /media/OS && echo "OS will be mounted"
else
	echo "OS will NOT be mounted."
fi
read -t3 
clear
#Asks, and if instructed to, updates and upgrades.
echo "Would you like to update & upgrade the system? (y/n)"
read -t3 ifupgrade 
if [ $upgrade = "y" ]
then
	sudo apt-get update && gnome-terminal sudo apt-get upgrade
else	
	echo "System will not be upgraded." 
fi
clear
#Will killall open programs. 
echo "Would you like to end all programs? (y/n) "
read ifkill
if [ $ifkill = "y" ]
then  
	killall chrome 
	killall thunderbird
	killall gimp
	killall firefox
	killall ooffice
	killall inkscape
	killall rhythmbox
	killall gnibbles
	killall xpenguins
else 
	clear && echo "No programs will be killed"
fi
clear
# Starts main part of program, where is starts program.
echo "Would you like to start chrome? (y/n)"
read -t3 ifchrome
if [ $ifchrome != "n" or "N" ] 
then
	clear
	echo "Google Chrome will not opened."
else
	clear
	echo "Google Chrome will be started."
	google-chrome &
fi
clear
	
#Starts Thunderbird.
echo "Would you like to start Thunderbird?"
read ifthndrbrd
if [ $ifthndrbrd == "y" ] 
then
	echo "Thunderbird Email client will now be started."
	read -t3
	clear
	thunderbird &
else 
	echo "Thunderbird will be not be started."
	fi
read -t3
clear

#Last part of the script.
echo "Would you like to run Rhythmbox."
read ifrytmbx
if [ $ifrytmbx == "y" or "Y" ]
then
	clear
	echo "Rhythmbox will be started."
	rhythmbox &
else 
	clear
	echo "Rhythmbox will not be started."
fi
exit

Last edited by Dark_Helmet; 11-22-2010 at 05:17 PM.
 
Old 11-22-2010, 06:11 PM   #4
Slightly Disoriented
Member
 
Registered: Sep 2010
Distribution: Ubuntu 10.10
Posts: 87

Original Poster
Rep: Reputation: 2
I have put relatively the same thing, but still problems arise. I attached a newer version of Main.txt if you want to look. Here are the problems.
A.) Even though a & is at the end of the google-chrome line, it is not continuing the script, and Chrome starts full screen.
B.) After the if google-chrome line, it does not echo the Thunderbird line. Although if I press y it runs thunderbird, and if I press n it doesn't. I'm going to get what it outputs and put it in a .txt file and attach it it that is useful.
Attached Files
File Type: txt Main.txt (1.8 KB, 19 views)
 
Old 11-22-2010, 06:22 PM   #5
Slightly Disoriented
Member
 
Registered: Sep 2010
Distribution: Ubuntu 10.10
Posts: 87

Original Poster
Rep: Reputation: 2
Here is the results of when I launch the script. They are attached.

P.S. I never knew & were called ampersands... Learn something every day :P
Attached Files
File Type: txt Main_Output.txt (5.4 KB, 8 views)
 
Old 11-22-2010, 06:49 PM   #6
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 374Reputation: 374Reputation: 374Reputation: 374
Something doesn't mesh. The output you gave includes this line:
Quote:
Mount success
But that message is not anywhere in the updated script.

That aside, I re-worked your script to be a little more consistent in getting input and how it displays output.

Code:
#!/bin/bash
#Program that mounts OS, then starts desired programs, based on users response. Also grabs updates, and has the option of upgrading.
#Unmounts just because.
sudo umount /media/OS

#Mounts OS
clear 
#Asks User if OS should be mounted.
echo "Would you like to mount OS? (y/n)"
read -t3 ifmount
if [ "$ifmount" = "y" ]
then
	echo "OS will be mounted"
	sudo mount /dev/sda3 /media/OS
else
	echo "OS will NOT be mounted."
fi
read -t3 
clear

#Asks, and if instructed to, updates and upgrades.
echo "Would you like to update & upgrade the system? (y/n)"
read -t3 ifupgrade 
if [ "$upgrade" = "y" ]
then
	echo "System will be upgraded." 
	sudo apt-get update && gnome-terminal sudo apt-get upgrade
else	
	echo "System will not be upgraded." 
fi
read -t3 
clear

#Will killall open programs. 
echo "Would you like to end all programs? (y/n) "
read -t3 ifkill
if [ "$ifkill" = "y" ]
then
	echo "Killing programs..."
	killall thunderbird
	killall gimp
	killall firefox
	killall ooffice
	killall inkscape
	killall rhythmbox
	killall gnibbles
	killall xpenguins
else 
	echo "No programs will be killed"
fi
read -t3
clear

# Starts main part of program, where is starts program.
echo "Would you like to start chrome? (y/n)"
read -t3 ifchrome
if [ "$ifchrome" == "y" ] 
then
	echo "Google Chrome will be started."
#	chromium-browser &
	google-chrome &
else
	echo "Google Chrome will not be started."
fi
read -t3
clear
	
#Starts Thunderbird.
echo "Would you like to start Thunderbird?"
read -t3 ifthndrbrd
if [ "$ifthndrbrd" = "y" ] 
then
	echo "Thunderbird Email client will now be started."
	thunderbird &
else 
	echo "Thunderbird will be not be started."
fi
read -t3
clear

#Last part of the script.
echo "Would you like to run Rhythmbox."
read -t3 ifrytmbx
if [ "$ifrytmbx" == "y" ]
then
	echo "Rhythmbox will be started."
	rhythmbox &
else 
	echo "Rhythmbox will not be started."
fi
read -t3
clear
exit
All the reads have a timeout of 3 seconds. If you type nothing, the script will do nothing.

I removed lots of clears. This way, you can see the question, the response, and the script's taken-action for a moment before it proceeds to the next section.

I changed the chrome section so that it defaults to not launching chrome with no input.

My system does not have Google Chrome, but I do have Chromium. You can see that I commented out the chromium-browser line since that's what I had to use on my end.

I ran the script and tested against Chromium and Thunderbird. Both launched and the script continued as expected.

Try with Thunderbird on your end. If Thunderbird works, then maybe there is a problem with the google-chrome command you're using. Can you execute it on the command line manually? Does it give the result you want? Does it ask for user input?

Last edited by Dark_Helmet; 11-22-2010 at 06:56 PM.
 
Old 11-22-2010, 06:55 PM   #7
Slightly Disoriented
Member
 
Registered: Sep 2010
Distribution: Ubuntu 10.10
Posts: 87

Original Poster
Rep: Reputation: 2
I haven't tried it yet, but I want to say thank you so much for doing this. You didn't have to look over the whole script like you did! Thank you very much. I'm going to try it now.

P.S The "Mount Success" line was something I added after I made the Main.txt, but that was all I added.
 
Old 11-22-2010, 07:05 PM   #8
Slightly Disoriented
Member
 
Registered: Sep 2010
Distribution: Ubuntu 10.10
Posts: 87

Original Poster
Rep: Reputation: 2
Still hanging up at Google Chrome... Hmmm Did this happen to you?
 
Old 11-22-2010, 07:10 PM   #9
sys64738
Member
 
Registered: May 2008
Location: NRW/Germany
Posts: 105

Rep: Reputation: 30
Hi
I figured out that one thing doesn't work.
When I call from a bash session:
Code:
sudo apt-get update && gnome-terminal sudo apt-get upgrade
and updates are available just a new gnome terminal is opened. I think the line has to be:
Code:
sudo apt-get update && sudo apt-get upgrade
 
Old 11-22-2010, 07:31 PM   #10
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 374Reputation: 374Reputation: 374Reputation: 374
Quote:
Originally Posted by Slightly Disoriented
Still hanging up at Google Chrome... Hmmm Did this happen to you?
No, but like I said, I do not have Google Chrome. There is no google-chrome executable on my machine. I had to use chromium-browser.

You need to open a terminal and run these tests manually.

1. execute: google-chrome
Does it start Chrome?
Does it ask for any user input?
You should not get a command prompt back until Chrome is closed

2. execute: google-chrome &
Does it start Chrome?
Does it ask for any user input?
You should get a command prompt almost immediately after pressing enter.

If you do not get a command prompt back immediately in #2, then that is your problem. If that is the case, there is something peculiar about google-chrome. You may need to read man pages or other documentation. Since I don't have it installed, I can't be of much help with it.

Quote:
Originally Posted by sys64738
just a new gnome terminal is opened
I think you're right. Another option aside from what you suggest could be to do this:
Code:
sudo apt-get update && gnome-terminal -e "sudo apt-get upgrade"
it all depends on what behavior you want.

Last edited by Dark_Helmet; 11-22-2010 at 07:33 PM.
 
Old 11-23-2010, 04:15 PM   #11
Slightly Disoriented
Member
 
Registered: Sep 2010
Distribution: Ubuntu 10.10
Posts: 87

Original Poster
Rep: Reputation: 2
Mmmm, with Chrome it's the other way around. With the ampersand it doesn't give me a command prompt back and vice versa... I'm going to continue to work on it.
 
Old 11-23-2010, 04:33 PM   #12
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 374Reputation: 374Reputation: 374Reputation: 374
Well, if that's the case, then try running the script and launch google-chrome without the ampersand. It's worth a shot. There have been other changes made to the script. So there's a chance that you won't run into the same problem you described in your original post.

If you still have problems, I'll see if I can find some other trick to get past this hurdle.
 
Old 11-23-2010, 04:52 PM   #13
Slightly Disoriented
Member
 
Registered: Sep 2010
Distribution: Ubuntu 10.10
Posts: 87

Original Poster
Rep: Reputation: 2
That seemed to work! Thank you so much. Now one last thing... Is there a way to have the script launch when I start my GNOME session. Like, to where there is a terminal open asking the first question when I'm logged in?
 
Old 11-23-2010, 05:02 PM   #14
Slightly Disoriented
Member
 
Registered: Sep 2010
Distribution: Ubuntu 10.10
Posts: 87

Original Poster
Rep: Reputation: 2
Well nevermind. When ever kill all the programs it doesn't work, but I then I don't everything works fine... Any clues to as why? Here is the EXACT copy of the script, I haven't changed anything but what you said.
Attached Files
File Type: txt Main.txt (1.9 KB, 13 views)

Last edited by Slightly Disoriented; 11-23-2010 at 05:30 PM.
 
Old 11-23-2010, 05:45 PM   #15
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 374Reputation: 374Reputation: 374Reputation: 374
Ok, hang on a sec. We need to get on the same page so to speak.

The script you attached is not the same as the script I included in post #6 (the most recent one with the script). If I change something, then I'll copy-paste it into the thread between code tags. If you change something, do the same.

You use code tags like so:
[code]
#!/bin/bash
#Program that mounts OS ...
...
exit
[/code]


You won't need to attach files anymore, and copy-pasting can be done directly through the web browser.

Code:
#!/bin/bash
#Program that mounts OS, then starts desired programs, based on users response. Also grabs updates, and has the option of upgrading.
#Unmounts just because.
sudo umount /media/OS

#Mounts OS
clear 
#Asks User if OS should be mounted.
echo "Would you like to mount OS? (y/n)"
read -t3 ifmount
if [ "$ifmount" = "y" ]
then
	echo "OS will be mounted"
	sudo mount /dev/sda3 /media/OS
else
	echo "OS will NOT be mounted."
fi
read -t3 
clear

#Asks, and if instructed to, updates and upgrades.
echo "Would you like to update & upgrade the system? (y/n)"
read -t3 ifupgrade 
if [ "$upgrade" = "y" ]
then
	echo "System will be upgraded." 
	sudo apt-get update && gnome-terminal sudo apt-get upgrade
else	
	echo "System will not be upgraded." 
fi
read -t3 
clear

#Will killall open programs. 
echo "Would you like to end all programs? (y/n) "
read -t3 ifkill
if [ "$ifkill" = "y" ]
then
	echo "Killing programs..."
	killall thunderbird
	killall gimp
	killall firefox
	killall ooffice
	killall inkscape
	killall rhythmbox
	killall gnibbles
	killall xpenguins
else 
	echo "No programs will be killed"
fi
read -t3
clear

# Starts main part of program, where is starts program.
echo "Would you like to start chrome? (y/n)"
read -t3 ifchrome
if [ "$ifchrome" == "y" ] 
then
	echo "Google Chrome will be started."
#	chromium-browser &
	google-chrome
else
	echo "Google Chrome will not be started."
fi
read -t3
clear
	
#Starts Thunderbird.
echo "Would you like to start Thunderbird?"
read -t3 ifthndrbrd
if [ "$ifthndrbrd" = "y" ] 
then
	echo "Thunderbird Email client will now be started."
	thunderbird &
else 
	echo "Thunderbird will be not be started."
fi
read -t3
clear

#Last part of the script.
echo "Would you like to run Rhythmbox."
read -t3 ifrytmbx
if [ "$ifrytmbx" == "y" ]
then
	echo "Rhythmbox will be started."
	rhythmbox &
else 
	echo "Rhythmbox will not be started."
fi
read -t3
clear
exit

On that note, copy-paste the script I just included above into a new file. We'll work from that new file.

I removed the ampersand from the google-chrome command. Other than that, it's the same script as from post #6 earlier. Try running with this script by executing the new file that you copy-pasted it into. Then let me know the results.
 
  


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 Help TechVandal Programming 5 03-01-2009 03:22 PM
Reading a bash variable in bash scripting problem freeindy Programming 3 11-27-2008 02:29 AM
Help with bash scripting amishtechie Programming 5 09-28-2008 04:59 AM
new to bash scripting peok Programming 2 07-15-2006 02:46 AM
Bash scripting kbeaver Programming 5 07-18-2003 08:35 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 12:50 PM.

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