Find the answer to your Linux question:
Results 1 to 3 of 3
so I'm really new to shell scripting and I've been studying and playing around trying to write a couple of useful shell scripts for myself. today I wrote my first, ...
  1. #1
    Just Joined! LinuxZealot's Avatar
    Join Date
    Jul 2007
    Location
    beautiful Seattle Washington
    Posts
    34

    exit status issues from a noob

    so I'm really new to shell scripting and I've been studying and playing around trying to write a couple of useful shell scripts for myself. today I wrote my first, original and applicable start up script to open the terminal upon login. the problem is that upon reboot the command opens several successive terminals and keeps repeating itself until an infinite number of terminal windows are open. this sucks and I would like to fix it. the following is a copy of my script


    #!bin/bash
    #this program ought to open the terminal for me upon startup...cross your
    #fingers.
    gnome-terminal
    #now let's see if I can teach this dog a new trick
    echo "Hello Cyrus!"
    #end of script.


    the goal being that a terminal window would open and say hello to me (because I'm just that kind of nerd)

    I'm sure it's a simple solution and I've searched several templates and examples hoping to find a clue, but like I said, I'm really new to this.

    anybody have any info that could point me in the right direction???

  2. #2
    Linux Guru anomie's Avatar
    Join Date
    Mar 2005
    Location
    Texas
    Posts
    1,692
    First, your sh-bang line needs to read:
    #!/bin/bash

    Second, where are you calling the script from - ~/.bash_profile? ~/.profile? ~/.bashrc?

    You see what the problem is here? When you open a terminal, you're telling it to open a gnome-terminal. For every gnome-terminal that opens, you're telling it to open another on top of it.

  3. #3
    Trusted Penguin Cabhan's Avatar
    Join Date
    Jan 2005
    Location
    Seattle, WA, USA
    Posts
    3,230
    To be a bit more specific:

    Every time a new terminal is opened (be it through logging in, ssh'ing in, switching user, running a terminal program, etc.), one of ~/.bash_profile or ~/.bashrc is run (assuming Bash shell: tcsh, for instance, has its own files). Therefore, as anomie says, you run gnome-terminal and it reads ~/.bashrc, which runs gnome-terminal, which reads ~/.bashrc, and so on, ad infinitum.

    There is another flaw here. Your system is going to try to run gnome-terminal whenever you login anywhere, even if that was in text mode.

    The solution is to, instead of executing this in ~/.bash_profile or ~/.bashrc is to execute it upon X login (or X startup). Well, there are two ways to go about this. The first is to go to Gnome's preferences, to the "Sessions" option, and tell it to launch a gnome-terminal on startup. The second is to put the command instead into ~/.xprofile. The commands in this file are executed after X starts up, which is exactly what you want. In your case, you could either invoke the script you've written, or put those commands directly into the ~/.xprofile file. Make sure that you put an '&' after the command call, so that the script is backgrounded and other commands can run instantly.

    The last thing to discuss is your echo message. This is going to print "Hello Cyrus!" to the terminal that controls the script. Well, in this particular case, the script is being executed by whatever is executing ~/.xprofile, and that has no controlling terminal (unless, again, you were running X manually). So this message will never be seen. What you want to do is to have the gnome-terminal instance print the message. You can do this very easily, by saying:
    Code:
    gnome-terminal -e 'echo "Hello Cyrus!"'
    This tells gnome-terminal to run the given command, rather than just opening and waiting.

    Does all of that make sense?
    DISTRO=Arch
    Registered Linux User #388732

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...