Results 1 to 4 of 4

Thread: Bash Scripts not working

  1. #1
    Join Date
    Apr 2014
    Beans
    1

    Bash Scripts not working

    I tried testing a small script by running from my home area:
    #!/bin/bash
    cd Downloads

    I made it executable by chmod +x first_script
    but when I tried running it with ./first_script, it didn't run

    What am I missing here???

  2. #2
    Join Date
    Feb 2009
    Location
    Dallas, TX
    Beans
    7,790
    Distro
    Ubuntu 16.04 Xenial Xerus

    Re: Bash Scripts not working

    Hi sarthak2. Welcome to the forum

    It does work, but since scripts run on a different shell (one is created to interpret the particular script), when the script ends that shell is destroyed, and you go back to the one is running interactively on the command line.

    You can confirm this by adding another command at the end like 'pwd', or even a 'ls', so you can 'see' that it is actually changing directories.

    There are ways to run the script in the same interactive shell the command line is running. I remember a couple:

    This:
    Code:
    . ./first_script
    or this:
    Code:
    source ./first_script
    Hope that helps. Let us know if you have more questions.
    Regards.
    Last edited by papibe; April 26th, 2014 at 05:31 AM. Reason: spelling

  3. #3
    Join Date
    Aug 2013
    Beans
    4,941

    Re: Bash Scripts not working

    What do you expect it to do? the command cd Desktop works in a terminal session so running it does nothing.
    Code:
    #!/bin/bash
    gnome-terminal &&
    cd Downloads
    This will open a new terminal and cd into Desktop. Instead of running it in the terminal you can just right click it and choose run. You need to set up Natilus to run script though, open Nautilus, go to Files > Prefernces > Behaviour > Executable text file and choose "ask each time" (Edit > Preferences > ... in 14.04)

  4. #4
    Join Date
    Aug 2009
    Beans
    57

    Re: Bash Scripts not working

    Quote Originally Posted by sarthak2 View Post
    I tried testing a small script by running from my home area:
    #!/bin/bash
    cd Downloads
    Further to the suggestion to use another command to indicate that your script has completed, I often use "read" at the end of a script (see, for example, http://linuxnorth.wordpress.com/2012...ind-ing-files/) so that it effectively pauses before completing, and an echo command to positively indicate that the script has finished. So, you could add:

    echo "Shell command complete"
    read

    to your script. The message will be displayed and you can close the terminal window.

Tags for this Thread

Bookmarks

Posting Permissions

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