Making any script executable

Hi all,

I'm new to Unix so just wanted some help.
I've been self learning and came accross a question online that I was trying. It is to make any shell script executable, the name of the file is to be made executable.

I would use nano and type in something like

#! /bin/bash 
Chmod +x 

Is that correct? Or am I doing something wrong?

Thank you.

To make scripts executable, you need to set its directory entry's "executable permissions" for the intended users/persons, "owner", "group", or "others". Enter chmod (all lower case) into the shell for this. Consider well which target users should receive that permission; in above sample you give it to all three groups.
nano is one among other editors used to modify scripts. It has nothing to do with the execution of a script.
The so called "shebang" within the script text is not mandatory but helps the shell decide which program to use to run the script.

1 Like

I wanted to use nano as the editor, would I just write chmod in the editor and do it like that?

Thanks

Hello HelenaR,

You could use following steps for same.

To edit a file called filename, type nano filename.
Insert new text(which is I guess

#!bin/ksh 
chmod +x Input_file

) at the current cursor position just by typing the text in.

^O save contents without exiting (you will be prompted for a file to save to)
^X exit nano (you will be prompted to save your file if you haven't)
^T when saving a file, opens a browser that allows you to select a file name from a list of files and directories.

Hope this helps you.

Thanks,
R. Singh

You do not make the script you write executable by typing chmod +x filename in it.
Instead, you should write it in the terminal, so the file gets the permission.

As in:

cat myscript.sh

#!/bin/sh
echo "hello world"
./myscript.sh
bash: ./myscript.sh: Permission denied
chmod +x ./myscript.sh
./myscript.sh
hello world

Hope this helps

EDIT:
Or are you looking for something like:

#!/bin/bash
#
#
#       Description:    Prints a list of all files it toggled execution flag
#       Changed by:     sea
#       File created:   2012.09.28
#       File changed:   2013.08.26
        script_version=0.3
#
#       Vars
#
        ME=${0##*/}
        ME_DIR=${0##/*}
        help_text="
$ME ($script_version)
ChangeMod [+-]x for files available in \$PWD or \$PATH
Usage:  $ME [/path/to/]FILE
        $ME FILE1 FILE2 FILE3 ...
"
#
#       Action
#
        [ "-h" = "$1" ] && \
                printf "$help_text\n" && \
                exit 1
        for thisFile in "${@}";do
                td=${thisFile##/*}
                script="$thisFile"
                if [ "$td" = "$thisFile" ] && [ ! -f "$thisFile" ]
                then    for tmpPath in $(echo "$PATH"|sed s/':'/' '/g);do
                                [ -f "$tmpPath/$thisFile" ] && \
                                        script="$tmpPath/$thisFile" && \
                                        break
                        done
                else    script="$thisFile"
                fi
                [ -x "$script" ] && \
                        mode="-" || \
                        mode="+"
                [ -f "$script" ] && chmod ${mode}x "$script" && \
                        RET=0 || \
                        RET=1
                [ $RET -eq 0 ] && \
                        printf "Set ${mode}x to $script :)\n" || \
                        printf "Set ${mode}x to $script :(\n"
        done
        exit $RET

Have fun

1 Like