Results 1 to 10 of 10

Thread: Shell creation help.

  1. #1
    Join Date
    Jul 2012
    Beans
    123

    Post Shell creation help.

    I was reading up how to create a shell script on http://linuxcommand.org and noticed something, there's a part for an example shell script, as follows.
    Code:
    #!/bin/bash
    # My first script
    
    echo "Hello World!"
    Let's say I input that in gedit, will the '#!/bin/bash' part tell gedit to save it there? The site really didn't go in detail.

  2. #2
    Join Date
    Jul 2007
    Location
    Poland
    Beans
    4,399
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: Shell creation help.

    no, that 1st line tells the system which interpreter should be used to run the script. Extension of the filename doesn't matter, all these .sh, .py (python) carry no meaningful information but are used by convention so the user can identify them without looking at contents. Simply put that hashbang line defines which 'family' the file belongs to. /bin/bash is the physical location of the bash executable that will be used to interpret the script.

    In your case running the script with ./script.sh would be equivalent to bash script.sh. System looks at the first line, sees bash - bash it is, here you go. You can override that default if you want by explicitly calling some other interpreter, eg. sh script.sh but it doesn't make much sense to run scripts in their non-native environments (there are differences in syntax and feature sets, simple shell script may work, but bash has a lot of nifty features that will fail when tried in sh)


    you work with shell scripts like with any other plain text files. The only difference is that shell scripts can be run when the hashbang line is present and the file itself is marked as executable.
    Last edited by Vaphell; October 9th, 2012 at 06:31 PM.
    if your question is answered, mark the thread as [SOLVED]. Thx.
    To post code or command output, use [code] tags.
    Check your bash script here // BashFAQ // BashPitfalls

  3. #3
    Join Date
    May 2010
    Location
    Tewkesbury uk
    Beans
    7,916
    Distro
    Ubuntu Development Release

    Re: Shell creation help.

    Hi

    Let's say I input that in gedit, will the '#!/bin/bash' part tell gedit to save it there?
    I'm not 100% sure what you are asking,bit if i understand you correctly, that does not tell gedit where to save the script. You need to tell gedit where to save the file and give it a file name.

    Code:
    #!/bin/bash
    What this does is to specify what shell should be used to run the script when the script is run as an executable file with the executable bit set in it's permissions and you do not specify which shell to run it with from the command line.

    Code:
    matthew-Aspire-7540:/home/matthew % cat script 
    #!/bin/bash
    # My first script
    
    echo "Hello World!"
    
    matthew-Aspire-7540:/home/matthew % chmod 755 script 
    matthew-Aspire-7540:/home/matthew % ll script      
    -rwxr-xr-x 1 matthew matthew 52 Oct  9 18:18 script*
    matthew-Aspire-7540:/home/matthew % ./script 
    Hello World!
    Without the executable bit set you cannot run the script that way......

    Code:
    matthew-Aspire-7540:/home/matthew % chmod 664 script
    matthew-Aspire-7540:/home/matthew % ll script 
    -rw-rw-r-- 1 matthew matthew 52 Oct  9 18:18 script
    matthew-Aspire-7540:/home/matthew % ./script
    zsh: permission denied: ./script
    matthew-Aspire-7540:/home/matthew %
    ....but you can run it without the executable bit set script if you state the shell to run it with though on the command line.

    Code:
    matthew-Aspire-7540:/home/matthew % ll script 
    -rw-rw-r-- 1 matthew matthew 52 Oct  9 18:18 script
    matthew-Aspire-7540:/home/matthew % bash script
    Hello World!
    matthew-Aspire-7540:/home/matthew %
    There are a number of shells you can use to run scripts and by putting the shebang (!#) and the path to the shell as the first line of your script, you specify which shell to run the script when the script is run as an executable file.

    You can get an idea of the shells on your system by looking at /etc/shells. Here's mine

    Code:
    matthew-Aspire-7540:/home/matthew % cat /etc/shells 
    # /etc/shells: valid login shells
    /bin/sh
    /bin/dash
    /bin/bash
    /bin/rbash
    /usr/bin/screen
    /usr/bin/tmux
    /bin/zsh
    /usr/bin/zsh
    /bin/csh
    /bin/tcsh
    /usr/bin/tcsh
    matthew-Aspire-7540:/home/matthew %
    You can specify different shell to run your script by changing the shebang line (i.e #!/bin/dash) and, when run as an executable script, it will run the script using that shell.

    This, below, will change the shell to dash in the file, check it and run the script using the dash shell.

    Code:
    matthew-Aspire-7540:/home/matthew % sed -i 's/bash/dash/' script 
    matthew-Aspire-7540:/home/matthew % head -n1 script 
    #!/bin/dash
    matthew-Aspire-7540:/home/matthew % ./script 
    Hello World!
    matthew-Aspire-7540:/home/matthew %
    Note there are differences between the shells and some commands wont work in some shells.

    csh and tsch even have a completely different syntax more in line with the C programming language.

    I hope i understood your question correctly and i hope that made sense.

    Kind regards
    If you believe everything you read, you better not read. ~ Japanese Proverb

    If you don't read the newspaper, you're uninformed. If you read the newspaper, you're mis-informed. - Mark Twain

  4. #4
    Join Date
    Jul 2012
    Beans
    123

    Re: Shell creation help.

    Quote Originally Posted by matt_symes View Post
    Hi

    I'm not 100% sure what you are asking,bit if i understand you correctly, that does not tell gedit where to save the script. You need to tell gedit where to save the file and give it a file name.

    Code:
    #!/bin/bash
    What this does is to specify what shell should be used to run the script when the script is run as an executable file with the executable bit set in it's permissions and you do not specify which shell to run it with from the command line.

    Code:
    matthew-Aspire-7540:/home/matthew % cat script 
    #!/bin/bash
    # My first script
    
    echo "Hello World!"
    
    matthew-Aspire-7540:/home/matthew % chmod 755 script 
    matthew-Aspire-7540:/home/matthew % ll script      
    -rwxr-xr-x 1 matthew matthew 52 Oct  9 18:18 script*
    matthew-Aspire-7540:/home/matthew % ./script 
    Hello World!
    Without the executable bit set you cannot run the script that way......

    Code:
    matthew-Aspire-7540:/home/matthew % chmod 664 script
    matthew-Aspire-7540:/home/matthew % ll script 
    -rw-rw-r-- 1 matthew matthew 52 Oct  9 18:18 script
    matthew-Aspire-7540:/home/matthew % ./script
    zsh: permission denied: ./script
    matthew-Aspire-7540:/home/matthew %
    ....but you can run it without the executable bit set script if you state the shell to run it with though on the command line.

    Code:
    matthew-Aspire-7540:/home/matthew % ll script 
    -rw-rw-r-- 1 matthew matthew 52 Oct  9 18:18 script
    matthew-Aspire-7540:/home/matthew % bash script
    Hello World!
    matthew-Aspire-7540:/home/matthew %
    There are a number of shells you can use to run scripts and by putting the shebang (!#) and the path to the shell as the first line of your script, you specify which shell to run the script when the script is run as an executable file.

    You can get an idea of the shells on your system by looking at /etc/shells. Here's mine

    Code:
    matthew-Aspire-7540:/home/matthew % cat /etc/shells 
    # /etc/shells: valid login shells
    /bin/sh
    /bin/dash
    /bin/bash
    /bin/rbash
    /usr/bin/screen
    /usr/bin/tmux
    /bin/zsh
    /usr/bin/zsh
    /bin/csh
    /bin/tcsh
    /usr/bin/tcsh
    matthew-Aspire-7540:/home/matthew %
    You can specify different shell to run your script by changing the shebang line (i.e #!/bin/dash) and, when run as an executable script, it will run the script using that shell.

    This, below, will change the shell to dash in the file, check it and run the script using the dash shell.

    Code:
    matthew-Aspire-7540:/home/matthew % sed -i 's/bash/dash/' script 
    matthew-Aspire-7540:/home/matthew % head -n1 script 
    #!/bin/dash
    matthew-Aspire-7540:/home/matthew % ./script 
    Hello World!
    matthew-Aspire-7540:/home/matthew %
    Note there are differences between the shells and some commands wont work in some shells.

    csh and tsch even have a completely different syntax more in line with the C programming language.

    I hope i understood your question correctly and i hope that made sense.

    Kind regards
    I know it's kind of late to reply but, I did everything you did, I actually saved the file through gedit, put it on the desktop,changed permissions to 755, and ran it. At 13 years old I made my first script! And I'd like to thank you so so much on walking me through it!

  5. #5
    hakermania's Avatar
    hakermania is offline Τώρα ξέρεις τι γράφω εδώ!
    Join Date
    Aug 2009
    Location
    Greece
    Beans
    1,703
    Distro
    Ubuntu Development Release

    Re: Shell creation help.

    13 yo is a good age to get your hands dirty with this stuff... Gongrats!
    Website

    Wallch (Wallpaper Changer): Sourceforge | Launchpad

  6. #6
    Join Date
    Jun 2011
    Location
    United Kingdom
    Beans
    Hidden!
    Distro
    Lubuntu Development Release

    Re: Shell creation help.

    You don't know what you're getting yourself into...before you know it, you'll be humming the free software song at your console, with a huge UNIX beard and sandals.

    Seriously though, well done and keep it going.

  7. #7
    Join Date
    Jul 2012
    Beans
    123

    Re: Shell creation help.

    Quote Originally Posted by MG&TL View Post
    You don't know what you're getting yourself into...before you know it, you'll be humming the free software song at your console, with a huge UNIX beard and sandals.

    Seriously though, well done and keep it going.
    Now you're making me look up that song.....

  8. #8
    Join Date
    Jul 2012
    Beans
    123

    Re: Shell creation help.

    Are there any differences in using different shells? Or is bash the best for basic scripts?

  9. #9
    Join Date
    May 2009
    Location
    Fareham, UK
    Beans
    Hidden!
    Distro
    Xubuntu 14.04 Trusty Tahr

    Re: Shell creation help.

    There is no best shell to use, it's down to personal preference but bash is a good shell to learn because a) its basic syntax is quick and easy to learn b) it has lots of clever advanced features so will last forever to learn it c) its the standard shell your computer uses in a terminal so if you learn to bash script you will become more competent at using the terminal to control your computer

    Python is another good language to learn for the same first 2 reasons, the thing I like best about bash though is almost immediately you can start using it for real world scenarios like running automated backups of your system and having certain programs run on login.

    I have one script which runs when I login on my system which will run every script I place in a specified folder, in that folder I have scripts to start banshee playing music in the background, start empathy in the background, back up my data folder on a schedule, set the volume to a reasonable level, start transmission, move photos out of my dropbox folder and sort them into appropriate folders of ~/Picutres/year/month the list goes on and on all done very simply using bash scripts
    Catch me on Freenode - imark

  10. #10
    Join Date
    Sep 2006
    Beans
    7,625
    Distro
    Lubuntu Development Release

    Re: Shell creation help.

    Quote Originally Posted by linuxvstheworld View Post
    Are there any differences in using different shells? Or is bash the best for basic scripts?
    bash and ksh are large, fancy and can do more. But if you use sh (dash on Ubuntu), the script is more likely to be POSIX compliant and thus portable to other non-Ubuntu systems like BSD and Solaris. sh is also a little faster than bash.

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
  •