Linux - NewbieThis 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
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
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.
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.
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.
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.
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.
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:
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.
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.
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?
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.
Last edited by Slightly Disoriented; 11-23-2010 at 05:30 PM.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.