<---- template headericclude ----->
Guide to create & run scripts (but not how to write scripts)
FedoraForum.org - Fedora Support Forums and Community
Results 1 to 2 of 2
  1. #1
    Join Date
    Aug 2016
    Location
    Iraq
    Posts
    1,434
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)

    Guide to create & run scripts (but not how to write scripts)

    Hi. I search about this in Internet & found 3 pages explain how. Each of them contain unnecessary details for common users. So, I decide to wright a collective tutorial from them. I wish I cover the topic appropriately.

    Creating, running a scripts in Linux:

    A shell script is a sequence of commands for which you have a repeated use. This sequence is typically executed by entering the name of the script on the command line. Alternatively, you can use scripts to automate tasks using the cron facility. Another use for scripts is in the UNIX boot and shutdown procedure, where operation of daemons and services are defined in init scripts.

    a. (Creating and naming):

    A shell script is a sequence of commands for which you have a repeated use. This sequence is typically executed by entering the name of the script on the command line. Alternatively, you can use scripts to automate tasks using the cron facility. Another use for scripts is in the UNIX boot and shutdown procedure, where operation of daemons and services are defined in init scripts.

    To create a shell script, open a new empty file in your editor. Any text editor will do: vim, emacs, gedit, dtpad et cetera are all valid. You might want to chose a more advanced editor like vim or emacs, however, because these can be configured to recognize shell and Bash syntax and can be a great help in preventing those errors that beginners frequently make, such as forgetting brackets and semi-colons.


    Syntax highlighting in vim

    In order to activate syntax highlighting in vim, use the command
    :syntax enable
    or
    :sy enable
    or
    :syn enable
    You can add this setting to your .vimrc file to make it permanent.

    Put UNIX commands in the new empty file, like you would enter them on the command line. Commands can be shell functions, shell built-ins, UNIX commands and other scripts.
    Give your script a sensible name that gives a hint about what the script does. Make sure that your script name does not conflict with existing commands. In order to ensure that no confusion can rise, script names often end in .sh; even so, there might be other scripts on your system with the same name as the one you chose. Check using which, whereis and other commands for finding information about programs and files:

    which -a script_name
    whereis script_name
    locate script_name

    b) Saving: it is better to create special folder for you scripts like:

    1) creating “Scripts” at your home directory.

    In this case, to avoid use of “cd” command, or writing path of script, every time you run scripts, you have to do either one of the following:

    - after creating directory ~/Scripts, add it to the contents of the PATH variable:
    export PATH="$PATH:~/Scripts"

    - A better way would be to edit your .bash_profile file to include the above command. That way, it would be done automatically every time you log in. Or,
    create a ~/bin and/or ~/.local/bin at your home directory, and as long as script has an executable bit and a valid shebang it can be invoked from any directory by its name for user owning that /home, similarly how system commands work. That works because bash is searching for command in locations specified in $PATH variable.

    2) But the best site to save scripts is /user/local/bin. But you need to be supper user for being able to do this, so type, 1st:

    sudo su
    yourpassword
    #
    The # prompt shows that you are now an administrator and can copy it:

    # cp scriptfile.sh /usr/local/bin

    Make the script executable:
    # chmod 755 /usr/local/bin/scriptname.sh

    Exit super user mode:
    # exit
    $

    Run the script:
    $ scriptname.sh

    Note 1: The PATH mechanism is used to find files to execute if you do not specify the path in the command (by prefixing the file name with ./ you specified the path).
    ./scriptname.sh (or “sudo ./scriptname.sh”) this will look for scriptname.sh in the current directory.
    ./scriptname.sh ( or “sudo scriptname.sh”) will look for scriptname.sh in the order specified in $PATH

    Note 2:

    - regarding path ~/bin, path ~/.local/bin
    $ scriptname.sh - works
    $ sudo scriptname.sh - no works
    # scriptname.sh - no works

    - regarding path /usr/local/bin
    $ scriptname.sh - works
    $ sudo scriptname.sh - works
    # scriptname.sh - works

    c) Running scripts:

    Prerequisite

    Before you can run the .sh file, you need to make it executable:

    1. Right-click on the file
    2. Select Properties
    3. Select Permissions
    4. In the Permissions tab, check Allow executing file as program:

    Or you can make file executable, simply, by following commands (you must be inside folder containing script or write it's path within commands):

    chmod +x scriptname.sh
    or
    chmod 755 scriptname.sh
    or
    chmod 700 scriptname.sh

    - The “+x” will give you execute permission only.
    - The "755" will give you (owner of script) read, write, and execute permission. Everybody else will get only read and execute permission.
    - The chmod +x gives execute permission to all leaving read and write permissions unchanged, while 755 ensures that only the owner has write permission (while giving read and execute to the group and others). Hence 755 is more secure choice.
    - If you want your script to be private (i.e., only you can read and execute), use "700" instead (most secure choice).

    Warning

    Make sure you trust the source where you got the file from. It could be a virus.

    The very simple way

    1. Close the Properties window and
    2. double-click the file.
    3. A dialog will pop up giving you the option to run the script in a terminal. Click run in terminal:

    This has problem. The terminal will close immediately and you will not be able to see the output.

    The simple way

    1. Open Applications -> Accessories -> Terminal
    2. Drag and drop the .sh file into the terminal and press Enter

    The way professionals do it

    1. Open Applications -> Accessories -> Terminal
    2. Find where the .sh file
    Use the ls and cd commands
    ls will list the files and folders in the current folder. Give it a try: type "ls" and press Enter.
    Once you see the folder that you want to go in to, run cd, followed by a space, followed by a folder name
    If you when into a folder that you did not want, run cd .. to go one level up
    3. Run the .sh file
    Once you can see for example scriptname.sh with ls run this:
    ./scriptname.sh


    -------------------------------
    You can get above guide as PDF as attachments with this post (it contain pictures).
    Attached Files Attached Files
    Last edited by User808; 24th December 2016 at 08:35 PM. Reason: Update guide & new PDF

  2. #2
    Join Date
    Oct 2011
    Posts
    1,917
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Re: Guide to create & run scripts (but not how to write scripts)

    b) Saving: it is better to create special folder for you scripts like “Scripts” at your home directory.
    Or create a ~/bin and/or ~/.local/bin, and as long as script has an executable bit and a valid shebang it can be invoked from any directory by its name for user owning that /home, similarly how system commands work.

    That works because bash is searching for command in locations specified in $PATH variable.

Similar Threads

  1. PHP Scripts saying No Write Permissions (Advanced)
    By drewdown in forum Using Fedora
    Replies: 4
    Last Post: 4th June 2012, 12:36 AM
  2. Is there are tutorial/guide for init scripts?
    By icy-flame in forum Using Fedora
    Replies: 3
    Last Post: 11th May 2009, 07:42 PM
  3. Replies: 12
    Last Post: 19th November 2008, 05:56 AM
  4. How do you create scripts or automation tasks?
    By electroconvulsi in forum Using Fedora
    Replies: 6
    Last Post: 28th June 2006, 03:36 PM
  5. Scripts
    By earobinson111 in forum Using Fedora
    Replies: 1
    Last Post: 1st August 2004, 10:55 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
[[template footer(Guest)]]