Page 1 of 3 123 LastLast
Results 1 to 10 of 23

Thread: Bash Scripting (CLI)...Please Help

  1. #1
    Join Date
    Aug 2014
    Beans
    181
    Distro
    Lubuntu 20.04 Focal Fossa

    Bash Scripting (CLI)...Please Help

    I didn't know which forum to post this in or how to make it "easy on the eyes"

    The section below isn't exactly a "step by step" easy to follow guide.
    It's a section at the Bash Scripting link in my signature.

    I will be using this post as a guide for posting my questions.

    Scripting


    NOTE: The commands given in the scripting section are to be put into the text editor and not in the terminal unless instructed otherwise.
    Bash is primarily a scripting language, so it would be a crime not to talk about scripting. Let's dive straight in with a bash script. More precisely the infamous "Hello World" script. You can create a bash script by opening your favorite text editor to edit your script and then saving it (typically the .sh file extension is used for your reference, but is not necessary. In our examples, we will be using the .sh extension).

    #!/bin/bash

    echo "Hello, World"The first line of the script just defines which interpreter to use. NOTE: There is no leading whitespace before #!/bin/bash. That's it, simple as that. To run a bash script you first have to have the correct file permissions. We do this with chmod command in terminal (change mode) as follows:
    chmod a+x /where/i/saved/it/hello_world.sh #Gives everyone execute permissions
    # OR
    chmod 700 /where/i/saved/it/hello_world.sh #Gives read,write,execute permissions to the OwnerThis will give the file the appropriate permissions so that it can be executed. Now open a terminal and run the script like this:
    /where/i/saved/it/hello_world.shHopefully you should have seen it print Hello, World onto your screen. If so well done! That is your first Bash script.

    Last edited by wyattwhiteeagle; August 23rd, 2021 at 06:18 AM.

  2. #2
    Join Date
    Aug 2014
    Beans
    181
    Distro
    Lubuntu 20.04 Focal Fossa

    Re: Bash Scripting (CLI)...Please Help

    The way I understood it, I open gedit, pasted the below making sure there was no "white space", clicked to save, named it "hello_world.sh" and saved it.
    It saved to /home/wyatt.
    When I ran /home/wyatt/hello_world.sh it said, "bash: /home/wyatt/hello_world.sh: Permission denied"

    No where in the Guide does it say what to do if it doesn't "print on the screen"

    The guide said that chmod will give permissions.

    What did I do wrong?
    How should it look?
    How should I have ran it?

    #!/bin/bash


    echo "Hello, World"
    chmod a+x /where/i/saved/it/hello_world.sh #Gives everyone execute permissions
    # OR
    chmod 700 /where/i/saved/it/hello_world.sh #Gives read,write,execute permissions to the Owner

    wyatt@wyatt-Aspire-E1-532:~$ /home/wyatt/hello_world.sh
    bash: /home/wyatt/hello_world.sh: Permission denied
    wyatt@wyatt-Aspire-E1-532:~$
    Last edited by wyattwhiteeagle; August 23rd, 2021 at 06:42 AM.

  3. #3
    Join Date
    Apr 2011
    Location
    Mystletainn Kick!
    Beans
    12,307
    Distro
    Ubuntu

    Re: Bash Scripting (CLI)...Please Help

    Those are two separate things.
    The first is the script
    Code:
    #!/bin/bash
    
    echo "Hello, World"
    which you copy into a file and name that file something like hello-world.sh

    The second is setting the required permissions for the file in order to run it.
    Code:
    chmod a+x /where/i/saved/it/hello_world.sh #Gives everyone execute permissions
    # OR
    chmod 700 /where/i/saved/it/hello_world.sh #Gives read,write,execute permissions to the Owner
    The important part of this section are the parts before the # symbols as those parts are not read by the system.
    They are typically used for comments about the commands like they do here.
    So the actual commands would look this this
    Code:
    chmod a+x /where/i/saved/it/hello_world.sh
    or
    Code:
    chmod 700 /where/i/saved/it/hello_world.sh
    You only need to run one of those chmod commands.
    Last edited by deadflowr; August 23rd, 2021 at 06:45 AM.
    Splat Double Splat Triple Splat
    Earn Your Keep
    Don't mind me, I'm only passing through.
    Once in a blue moon, I'm actually helpful
    .

  4. #4
    Join Date
    Aug 2014
    Beans
    181
    Distro
    Lubuntu 20.04 Focal Fossa

    Re: Bash Scripting (CLI)...Please Help

    I changed it to...
    Code:
    #!/bin/bash         
    
    echo "Hello, World"
    chmod a+x /where/i/saved/it/hello_world.sh
    Ran this in terminal
    Code:
    /home/wyatt/hello_world.sh
    Terminal still says "Permission denied"

  5. #5
    Join Date
    Apr 2011
    Location
    Mystletainn Kick!
    Beans
    12,307
    Distro
    Ubuntu

    Re: Bash Scripting (CLI)...Please Help

    No.
    The sections I posted are as they should be, don't combine things that aren't already combined.

    The first part
    Code:
    #!/bin/bash
    
    echo "Hello, World"
    is to be saved as is to a file.
    That's it.
    That's the script.

    The second part
    Code:
    chmod a+x /where/i/saved/it/hello_world.sh
    is to be run inside a terminal.
    This command is what sets to required settings in order to run the script.

    Perhaps I should have clarified the point of running the chmod command in a terminal.
    But I thought the wiki already explained that.
    Splat Double Splat Triple Splat
    Earn Your Keep
    Don't mind me, I'm only passing through.
    Once in a blue moon, I'm actually helpful
    .

  6. #6
    Join Date
    Aug 2014
    Beans
    181
    Distro
    Lubuntu 20.04 Focal Fossa

    Re: Bash Scripting (CLI)...Please Help

    Quote Originally Posted by deadflowr View Post
    Perhaps I should have clarified the point of running the chmod command in a terminal.
    But I thought the wiki already explained that.
    Thank you.
    I will try that.

    The Wiki says the below and so when I ran that, the terminal said "No such file or directory"

    Now open a terminal and run the script like this:
    Code:
    /where/i/saved/it/hello_world.sh
    Hopefully you should have seen it print Hello, World onto your screen. If so well done! That is your first Bash script.
    Last edited by wyattwhiteeagle; August 23rd, 2021 at 07:33 AM.

  7. #7
    Join Date
    Apr 2011
    Location
    Mystletainn Kick!
    Beans
    12,307
    Distro
    Ubuntu

    Re: Bash Scripting (CLI)...Please Help

    The /where/i/saved/it/hello_world.sh is only an example name.
    Unless you created it there is no such location as /where/i/saved/it.

    What name did you save the file as?
    And do you know where you saved the file?
    Splat Double Splat Triple Splat
    Earn Your Keep
    Don't mind me, I'm only passing through.
    Once in a blue moon, I'm actually helpful
    .

  8. #8
    Join Date
    Aug 2014
    Beans
    181
    Distro
    Lubuntu 20.04 Focal Fossa

    Re: Bash Scripting (CLI)...Please Help

    Quote Originally Posted by deadflowr View Post
    What name did you save the file as?
    And do you know where you saved the file?
    I must be either missing something or doing something wrong.

    I named the file as hello_world.sh and saved it in /home/wyatt

    I changed it to this...
    #!/bin/bash


    echo "Hello, World"
    Changed to this and entered into the terminal...
    chmod a+x /home/wyatt/hello_world.sh
    And Terminal says...
    wyatt@wyatt-Aspire-E1-532:~$ chmod a+x /home/wyatt/hello_world.sh
    wyatt@wyatt-Aspire-E1-532:~$
    That's all...terminal said nothing else and nothing else seems to have happened

    Then I changed the a+x to 700...same thing...nothing from terminal and nothing happened
    Last edited by wyattwhiteeagle; August 23rd, 2021 at 08:11 AM.

  9. #9
    Join Date
    Nov 2007
    Location
    London, England
    Beans
    7,302
    Distro
    Xubuntu 20.04 Focal Fossa

    Re: Bash Scripting (CLI)...Please Help

    This is all just basic stuff that a beginner has to get through. Don't worry, once you know what's going wrong it will all quickly make sense.
    Two things I was told very early on when I started:
    1- Linux generally says nothing if there's no problem. There's no "Yes I did that OK" message.
    2 - Linux is case sensitive - this catches windows users out a lot. hello_world.sh and Hello_world.sh are different files, and both could exist next to each other.
    So the face that this command didn't complain means it was successful, and execute permission has now been applied to that script file.
    Code:
    wyatt@wyatt-Aspire-E1-532:~$ chmod a+x /home/wyatt/hello_world.sh
    wyatt@wyatt-Aspire-E1-532:~$
    You should now be able to run the script with this command:
    Code:
    wyatt@wyatt-Aspire-E1-532:~$ /home/wyatt/hello_world.sh

  10. #10
    Join Date
    Aug 2014
    Beans
    181
    Distro
    Lubuntu 20.04 Focal Fossa

    Re: Bash Scripting (CLI)...Please Help

    Quote Originally Posted by The Cog View Post
    This is all just basic stuff that a beginner has to get through. Don't worry, once you know what's going wrong it will all quickly make sense.
    Two things I was told very early on when I started:
    1- Linux generally says nothing if there's no problem. There's no "Yes I did that OK" message.
    2 - Linux is case sensitive - this catches windows users out a lot. hello_world.sh and Hello_world.sh are different files, and both could exist next to each other.
    So the face that this command didn't complain means it was successful, and execute permission has now been applied to that script file.
    Code:
    wyatt@wyatt-Aspire-E1-532:~$ chmod a+x /home/wyatt/hello_world.sh
    wyatt@wyatt-Aspire-E1-532:~$
    You should now be able to run the script with this command:
    Code:
    wyatt@wyatt-Aspire-E1-532:~$ /home/wyatt/hello_world.sh
    And we have success

    Thank you so much for that info, it's enlightening


    So...a recap...is this pretty much how it goes?

    "Hello World" Basic Scripting

    The Rules

    In a text editor
    Code:
    1. #!/bin/bash must be the very first thing done in a text editor.
    2. Must skip a line before typing a command
    3. Command line must start with a command such as the word echo
    In the terminal
    Code:
    1. chmod is a terminal command as though it's like someone's name inside the terminal
    2. a+x is asking that person to do something specific
    3. 700 (same as a+x and asks the same thing done)
    4. a+x must be followed by the file location and filename ( /home/wyatt/file.sh )
    5. The person, request, path, and filename must be on the same line
    Instructions
    The Script

    In a prefered text editor. (Your choice. Doesn't matter which one...gedit; LibreOffice Writer)
    Code:
    1. Immediately type #!/bin/bash
    2. Press enter twice
    3. Type whatever command needed. (In this case, the command echo)
    4. Hit the spacebar once
    5. Type "Hello World" (be sure to include the quotation marks)
    6. Click to save the file
    7. Name the file including the extension as part of the filename, like this .sh (note the entire filename...in this case...file.sh)
    8. Choose whare you want to save the file (note the location)
    9. Save the file
    Now in terminal
    Activating The Script

    Code:
    1. type chmod a+x /home/wyatt/file.sh (where the file is saved and the entire filename)
    2. Hit enter
    3. Type /home/wyatt/file.sh (the path and filename)
    4. Hit enter
    The Terminal should say Hello World
    Last edited by wyattwhiteeagle; August 23rd, 2021 at 10:46 AM.

Page 1 of 3 123 LastLast

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
  •