Learn About Computers

Would you like to react to this message? Create an account in a few clicks or log in to continue.
Learn About Computers

Teaching about Computers


    17. First Basic Script & Changing Permissions with Chmod

    avatar
    Aof
    Admin


    Posts : 97
    Join date : 2018-03-24

    17. First Basic Script & Changing Permissions with Chmod Empty 17. First Basic Script & Changing Permissions with Chmod

    Post by Aof Thu May 24, 2018 3:12 am


    We create our first Linux Script, which asks the user a question,
    collects their answer, and performs an action after it checks the answer
    to see if the IF Statement is true or else.
    =*=
    Contents of script1.sh

    Code:
    #!/bin/bash
    #The above is called, "Shebang" or "bang line" all scripts under Linux execute using the interpreter specified on a first line.
    ae=yes
    #creates a VAR using the interpreter specificied on a first line.
    echo "Hey Guys & Gals"
    #echoes "Hey Guys & Gals"
    read -p "are you ready to learn?" a1
    #Asks people if they're ready to learn,
    #Stores the input in a VAR named a1.
    sleep 02
    #waits 2 seconds.
    if [[ $a1 == $ae ]];then
    #compares the 2 VARS to see if their values match, if they do, then it
    #echoes the below message.
    echo "Let us begin."
    else
    #else statements says that the above VARS do not match.
    echo "No it does not."
    #echoes, "No it does not."
    sleep 02
    #waits 2 seconds
    gnome-terminal;firefox-esr
    #issues the commands "gnome-terminal" and "firefox-esr"
    fi
    #finishes the If Statement, all if statements must be finished.

    =*=
    From the directory that you saved the nano script1.sh
    You can change the permissions of the script, to make it executable.
    1. chmod 550 script1.sh = makes the file executable for the admin and the user, but gives public 0 permissions.
    2. ls = the file now should be green if it's executable to you.
    3. ./script1.sh = starts the script, and requests user input.
    4. If the user says "yes", it echoes the message, "Let us begin."
    5. If the user puts anything but "yes", The script will echo "No it does not" then open the gnome-terminal and firefox-esr.
    For more information regarding chmod commands view the example below.
    chmod xxx fileorfoldername
    1st digit changes permissions of administrative users.
    2nd digit changes permissions of user groups that you've created.
    3rd digit changes the permissions of the public.
    0 = no access
    2 = write only
    3 = write & execute
    4 = read only
    5 = read & execute
    6 = read & write
    7 = full access
    "chmod 740 folder" would give the administrative root user full access to the folder, the user groups read only access, and the public 0 access to a folder called folder.
    "chmod 620 file" would give administrator root user full access to the file,
    the user groups read only access, and the public 0 access to a file called file.


      Current date/time is Mon May 06, 2024 5:40 pm