Scheduled Maintenance: We are aware of an issue with Google, AOL, and Yahoo services as email providers which are blocking new registrations. We are trying to fix the issue and we have several internal and external support tickets in process to resolve the issue. Please see: viewtopic.php?t=158230

 

 

 

how to execute shell script

New to Debian (Or Linux in general)? Ask your questions here!
Post Reply
Message
Author
arktos
Posts: 4
Joined: 2014-12-28 09:25

how to execute shell script

#1 Post by arktos »

Hi all,

I started using Debian system from a week ago, a lot of things are new to me. The text-based terminal seems a bit tedious at first (from a Windows user), but little-by-little I'm starting to get used to some basic commands that allow me to get simple things done. For the first time in my life, I feel like I'm in control of my computer.
Then I read about shell script and how powerful it could be. I was instantly attracted by its potentials and began learning it through http://www.freeos.com/guides/lsst/. However, the progress isn't very fast as I wanted to be.

Something is bugging me while I'm trying to follow a shell script tutorial.

e.g. with the following script (name: pos_neg)

Code: Select all

#!/bin/sh
#
# Script to see whether argument is positive of negatvie
#
if [ $# -eq 0 ]
then
echo "$0: You must supply one integer."
exit 1
fi

if [ $1 -gt 0 ]
then
echo "$1 is positive."
else
echo "$1 is negative."
fi
1. Even though the script doesn't have execute permission at all, I can still run the script with

Code: Select all

$sh pos_neg 3
why?

2. Why can't I execute the script with

Code: Select all

$./pos_neg 3
I've tried a)adding execute permission to the script; b)running it as root/sudo; c)changing the first line b/w #!/bin/bash and #!/bin/sh.

Code: Select all

sudo: unable to execute ./pos_neg: Permission denied
3. Regarding the actual script. when I changed the expression, e.g. at line5.

Code: Select all

if [ $# -eq 0 ]
to

Code: Select all

if [ $# == 0 ]
error occurs. Do they have the same meaning? If not, how do you write [ $# -eq 0 ] in mathematical notation?

Thank you for your precious time.
Arktos
Last edited by arktos on 2014-12-28 16:16, edited 1 time in total.

User avatar
Head_on_a_Stick
Posts: 14114
Joined: 2014-06-01 17:46
Location: London, England
Has thanked: 81 times
Been thanked: 132 times

Re: how to execute shell script

#2 Post by Head_on_a_Stick »

Please don't use white text -- it is almost invisible.
arktos wrote:1. Even though the script doesn't have execute permission at all, I can still run the script with

Code: Select all

$sh pos_neg 3
why?
man sh wrote: The sh utility is a command language interpreter that shall execute commands read from a
command line string, the standard input, or a specified file.
See man sh for more information.
2. Why can't I execute the script with

Code: Select all

$./pos_neg 3
Because it is not an executable file -- check by running `ls -l <name of script>` to see the file permissions.
I've tried a)adding execute permission to the script; b)running it as root/sudo; c)changing the first line b/w #!/bin/bash and #!/bin/sh.

Code: Select all

sudo: unable to execute ./pos_neg: Permission denied
You need to make the script executable:

Code: Select all

# chmod +x <name of script>
I don't know about your third question -- I couldn't script my way out of a wet paper bag...

I think you need to have a look at this guide:
http://mywiki.wooledge.org/BashGuide
deadbang

User avatar
edbarx
Posts: 5401
Joined: 2007-07-18 06:19
Location: 35° 50 N, 14 º 35 E
Been thanked: 2 times

Re: how to execute shell script

#3 Post by edbarx »

If you are attracted by shell scripts and their power, I suggest you to try learning a proper programming language that will offer far more flexibility. For instance, using C++, your shell script becomes:

Code: Select all

#include <cstdio>
#include <stdlib.h>
#include <iostream>
#include <string>

using namespace std;

int main(int argc, char **argv)
{
	if (argc == 1 || argc > 2)
	{
	  cout << "supply ONE numberical parameter\n";
	  return 0;
	}
	
	int num = atoi(argv[1]);
	if (num >= 0)
	  cout << "number is positive\n";
	  else cout << "number is negative\n"; 
	   
	return 0;
}
Debian == { > 30, 000 packages }; Debian != systemd
The worst infection of all, is a false sense of security!
It is hard to get away from CLI tools.

arktos
Posts: 4
Joined: 2014-12-28 09:25

Re: how to execute shell script

#4 Post by arktos »

So instead of executing my script, SH simply read the "script" as a sequence of commands. If that's the case why do most tutorials teach us to grant executive permission to a shell script like the one you provided.

I've tried the followings and getting the same result regardless to the executive status of the script.
Not working

Code: Select all

./pos_neg
Works only with mathematical operators, i.e. -eq, -ge....

Code: Select all

sh ./pos_neg
Works with both mathematical operators and statements, i.e. -eq, ==, -ge, >=.....

Code: Select all

bash ./pos_neg
BASH seems to have improved functionality compared to SH.
Thanks for linking the BashGuide. It seems to cover more basic topics with more detailed explanation, I will use it as a supplement to my ongoing tutorial. :D

edbarx, I eventually want to learn proper programming language. Thank you for your suggestion. Unfortunately I don't even know how to run the code that you translated for me :oops: But I do notice that C++ looks less repetitive than shell script. I will investigate more later on.

User avatar
edbarx
Posts: 5401
Joined: 2007-07-18 06:19
Location: 35° 50 N, 14 º 35 E
Been thanked: 2 times

Re: how to execute shell script

#5 Post by edbarx »

You have to install the compilers and libraries (as root):

Code: Select all

apt-get install build-essential
Then, copy the code and save it to a file, say, comp.cpp. To compile, the code into an executable file, open a terminal, cd to the directory where you installed comp.cpp, and issue this command which tells g++ to compile the code into an executable file named comp.

Code: Select all

g++ comp.cpp -o comp
To run the executable:

Code: Select all

./comp
Debian == { > 30, 000 packages }; Debian != systemd
The worst infection of all, is a false sense of security!
It is hard to get away from CLI tools.

User avatar
Head_on_a_Stick
Posts: 14114
Joined: 2014-06-01 17:46
Location: London, England
Has thanked: 81 times
Been thanked: 132 times

Re: how to execute shell script

#6 Post by Head_on_a_Stick »

arktos wrote:So instead of executing my script, SH simply read the "script" as a sequence of commands.
That is what "executing the script" means, right? Reading the sequence of commands and executing them in order.

Using `sh <script>` with a non-executable file is exactly equivalent to running `./<script>` with an executable file.

In Debian, /bin/sh is symlinked to /bin/dash -- this is a faster, less full-featured type of command shell (compared to BASH).

Most other distributions simply symlink /bin/sh to /bin/bash so the execution will be identical with either shebang.
deadbang

arktos
Posts: 4
Joined: 2014-12-28 09:25

Re: how to execute shell script

#7 Post by arktos »

Head_on_a_Stick wrote:Using `sh <script>` with a non-executable file is exactly equivalent to running `./<script>` with an executable file.
Thanks for making it clear for me.
But when I try `./<script>`, bash still says permission denied. `sh <script>` works fine though.
I checked the file with `ls -l` and the first column shows -rwxrwxrwx. I think that means the file is executable.
Am I missing something?

User avatar
Head_on_a_Stick
Posts: 14114
Joined: 2014-06-01 17:46
Location: London, England
Has thanked: 81 times
Been thanked: 132 times

Re: how to execute shell script

#8 Post by Head_on_a_Stick »

arktos wrote:Am I missing something?
Yes -- what is the full output of `ls -l`?
deadbang

arktos
Posts: 4
Joined: 2014-12-28 09:25

Re: how to execute shell script

#9 Post by arktos »

My apology for missing some details.
I still don't understand why I'm getting "Permission denied".

Code: Select all

arc@i3-330m:~/testing$ ls -l pos_neg.sh 
-rwxrwxrwx 1 arc arc 218 Dec 30 19:53 pos_neg.sh

Code: Select all

arc@i3-330m:~/testing$ bash pos_neg.sh 3
3 is positive.

Code: Select all

arc@i3-330m:~/testing# ./pos_neg.sh 3
bash: ./pos_neg.sh: Permission denied

User avatar
Head_on_a_Stick
Posts: 14114
Joined: 2014-06-01 17:46
Location: London, England
Has thanked: 81 times
Been thanked: 132 times

Re: how to execute shell script

#10 Post by Head_on_a_Stick »

I don't understand either.

You will have to wait for someone who knows better than I...
deadbang

User avatar
Issyer
Posts: 3032
Joined: 2007-05-23 02:59
Location: Khakassia

Re: how to execute shell script

#11 Post by Issyer »

Code: Select all

 sh ./pos_neg
You pass your script as an argument for the excecitable "sh".

Code: Select all

 ./pos_neg
You run your script as an executable itself. It won't start unless it has an x permission. Obviously you can't just run

Code: Select all

pos_neg
unless the directory with pos_neg is in the PATH variable. With the dot "." you indicate that directory is where you are currently. Or you can do

Code: Select all

export PATH=$PATH:.
pos_neg
You need a shebang for that

Code: Select all

#!/bin/bash

587jmj
Posts: 11
Joined: 2013-10-21 18:43

Re: how to execute shell script

#12 Post by 587jmj »

Hello,
If I read it correctly,
you did not make your script executeable,
that is why you can't execute it.
In the same directory your script is located,
type the following command,
you don't have to be root for this to work
in this case:
chmod +x pos_neg
then just type:
./pos-neg
and the script will run, it ran for me.

I would change one thing,
you should just type the script name in order
to run it, not the script name with a space then
a number. Linux does not like spaces in file names,
and nobody but you would know to add a number
after the script name.
I modified the script a
little that you just type the script name to run it(no number after the script name)
Then it tells you what the script is, and it asks you
it asks you to supply a number, then it tells you
what the number is and if it is negative, 0 or
positive.

here is the code

Code: Select all

   #!/bin/sh
    #
    # Script to see whether argument is positive of negatvie
    #
    echo "please supply a number"
    read num

    clear
    echo "you entered $num."




    if [ $num -eq 0 ]
    then
    echo "$num: Your number is 0."
    exit 1
    fi

    if [ $num -lt 0 ]
    then
    echo "$num: Your number is negative."

    fi

    if [ $num -gt 0 ]
    then
    echo "$num: Your number is positive."
    else
    echo "$num is 0 or is negative."

    fi


copy and paste the code above in a text editor like nano,
then save it. You can use any file name you wish, since
you already used pos_neg. I would choose a different name,
unless you want to use the same name. Then it will ask you
if you want to overwrite the file.

then type:
chmod +x <new file name>
WITHOUT THE < >


hope this helps.

reinob
Posts: 1189
Joined: 2014-06-30 11:42
Has thanked: 97 times
Been thanked: 47 times

Re: how to execute shell script

#13 Post by reinob »

arktos wrote:My apology for missing some details.
I still don't understand why I'm getting "Permission denied".

Code: Select all

arc@i3-330m:~/testing$ ls -l pos_neg.sh 
-rwxrwxrwx 1 arc arc 218 Dec 30 19:53 pos_neg.sh

Code: Select all

arc@i3-330m:~/testing$ bash pos_neg.sh 3
3 is positive.

Code: Select all

arc@i3-330m:~/testing# ./pos_neg.sh 3
bash: ./pos_neg.sh: Permission denied
Your /home partition is probably mounted with the noexec option, which prevents executing anything (by the kernel, not by the shell, as you've already figured out).

Kindly post the contents of /etc/fstab. If you see the "noexec" option and you are confident enough to remove it, do so (use "sudo nano /etc/fstab", don't use a graphical editor to avoid a sub-thread). Otherwise ask for help. (If sudo -- with your own password -- doesn't work, use "su" (with root's password) and then "nano /etc/fstab", then "exit").

Post Reply