Making any script executable


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Making any script executable
# 1  
Old 09-07-2015
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

Code:
#! /bin/bash 
Chmod +x

Is that correct? Or am I doing something wrong?

Thank you.

Last edited by rbatte1; 09-07-2015 at 10:14 AM..
# 2  
Old 09-07-2015
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.
This User Gave Thanks to RudiC For This Post:
# 3  
Old 09-07-2015
Quote:
Originally Posted by RudiC
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.

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

Thanks
# 4  
Old 09-07-2015
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
Code:
#!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
# 5  
Old 09-07-2015
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:
Code:
cat myscript.sh

#!/bin/sh
echo "hello world"

Code:
./myscript.sh
bash: ./myscript.sh: Permission denied

Code:
chmod +x ./myscript.sh
./myscript.sh
hello world

Hope this helps

EDIT:
Or are you looking for something like:
Code:
#!/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

Last edited by sea; 09-07-2015 at 09:02 AM..
This User Gave Thanks to sea For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Making bash script allways executable when transfer ?

Does it possible to make some bash script automatic to be a executable when transfered to another pc...? (5 Replies)
Discussion started by: tomislav91
5 Replies

2. UNIX for Advanced & Expert Users

What choice when "making" an executable on FreeBSD?

I should make an executable on our server, and are having some problem (I changed this question cause I found out that anser). I'm getting this error when trying to do make: In file included from... /usr/include/sys/file.h:161: error: expected specifier-qualifier-list before 'u_int' *** Error... (1 Reply)
Discussion started by: 244an
1 Replies

3. Shell Programming and Scripting

Making a perl script executable

Hello, I have a perl program called snp_hwe.pl I have another program called hwe_test.run which contains the following: for file in *.inp do cp $file genotype_counts_c.txt ./snp_hwe.exe > $file'.res' done I want to change my perl program to an executable program while changing... (3 Replies)
Discussion started by: Homa
3 Replies

4. UNIX for Dummies Questions & Answers

Making file executable

Hi guys, i'm trying to make a file called 'run-all-tests' executable but it is not letting me for some reason. I am presented with the following error: chmod: cannot access `./run-tests': No such file or directory Basically i have a folder called ex3 and within that there are task folders:... (11 Replies)
Discussion started by: Shyamz1
11 Replies

5. Shell Programming and Scripting

problem in making Awk executable script

Guys I placed #!path/awk -f placed awk script and used $1 to call 1st inputfile inside the script. But some where I did mistake. Could you please help me run this script as executable I forgot to mention I also used BEGIN before placing awk script. But nothing worked out. Script ... (2 Replies)
Discussion started by: repinementer
2 Replies

6. UNIX for Dummies Questions & Answers

need help making a script executable

making a script in vi to create a shell script called wherearethey by entering the following script: echo -n "Who are you looking for: "read userif then list=`w | grep $user | cut -c19-30` if then echo "The user $user is logged in from $list" else echo "The user $user is not logged in... (3 Replies)
Discussion started by: curtner
3 Replies

7. UNIX for Dummies Questions & Answers

Making UNIX script executable

Hello, I am very new to UNIX and I have been learning about writing scripts and making them executable. I created a script called myscript. It has three lines: #! /bin/sh # This is my first shell script echo friendsjustfriends Now I try to run it using the sh command and it works Next I... (4 Replies)
Discussion started by: rohitx
4 Replies

8. AIX

Making Executable File

Hi All: I am a newbie. I have shell script and bunch of java jar files and I want to give one single executable file (may be .bin). Ex: I have test.sh, jar1.jar, jar2.jar. I have to make process.xxx When we run "process.xxx" it will run the "test.sh" script which inturn uses jar1.jar and... (0 Replies)
Discussion started by: laxman123
0 Replies

9. Filesystems, Disks and Memory

pls hlp: making .sh files executable

I've got a file named jdictd.sh containing the following: --- begin file contents --- #! /bin/csh echo I\'m running! java -cp jdictd.jar org.dict.server.JDictd data/dict.ini # Use the following line instead if JRE 1.1 is used # jre -cp jdictd.jar org.dict.server.JDictd data/dict.ini ---end... (2 Replies)
Discussion started by: ropers
2 Replies
Login or Register to Ask a Question