sh not working when attempting to run script.

AlphaAlien

New Member
Joined
Jan 10, 2024
Messages
6
Reaction score
1
Credits
75
Hi guys I am currently learning to write an execute scripts I just recently used vi to creat a script... Saved it as pact.

But when I use sh or ./pact to execute I get what you see in photos .. I'm wondering if maybe the book I'm learning from which is copyrighted 2002 might be the issue. I'm not sure. But I have added execute permission by
``
chmod +x pact
``
I have confirmed it's in the current directory of where I am trying to execute. But I'm not sure what I'm doing wrong. I will attach picture of my script and of the errors I get.
 

Attachments

  • 17078362485266885869339265454305.jpg
    17078362485266885869339265454305.jpg
    4.3 MB · Views: 166
  • 17078362948491949845486609919548.jpg
    17078362948491949845486609919548.jpg
    4.1 MB · Views: 117
  • 17078363523813410441366528004971.jpg
    17078363523813410441366528004971.jpg
    4 MB · Views: 125


I notice you have the line "Script Name: pact" uncommented.
You probably need that line commented.

It isn't required, but it is a common convention to name shell scripts with a .sh extension.
Like pact.sh

Also you don't have to type "sh pact". You can just type ./pact.sh
Typically Linux wants a path. The ./ just means run it from this directory.

Linux has a shell called "sh", but it also has a shell called "bash". I notice your script calls bash.
So when you type "sh pact", you're calling the sh shell to call the bash shell.
You could type "bash pact", but you're already in bash so you don't have to do that.
 
@AlphaAlien - Remove the direct call to bash from the top of the script.

The shebang line #!/bin/bash must always be the first line in any script. This tells the shell which interpreter should be used to execute the script.
You don’t need to add a .sh extension.

As long as you have included a shebang at the top of your script, that specifies the appropriate interpreter to use. It doesn’t matter which scripting language you use and you don’t have to use a file extension.
That way your scripts can be treated as if they are any other program. It doesn’t matter if it’s a shellscript, python, Ruby, Lua , php, lolcat etc etc. As long as you specify the correct interpreter to use in the shebang line, you can use any scripting language in your scripts.

Also, comment out the script name (change Script Name: pact to # Script Name: pact ) and your script will be syntactically correct and should run. Whether or not it shows any results will depend on you.

You’ve made the script executable with chmod, so that’s fine.

I can see from your second screenshot that you ran the script using sh ./pact, but it complained that you don’t have a file or directory called project.

That script appears to be using cut to extract text from a file called project, using : as a delimiter. And it’s only extracting field 4.
So the text in the file will be something like this:
Code:
Something:like:this:here
I:don’t:know:exactly:though
The cut command would extract the word here from line 1 and exactly from line 2, because they are the 4th field in each line.

The sort command would sort the results alphabetically:
E.g.
Code:
exactly
here
uniq counts how many times each of those words appears. So the output would be:
Code:
1 exactly
1 here
And then awk reformats the output to:
Code:
exactly:1
here:1
Which is redirected to a file called pnum.

Obviously, the data I’ve used above is completely unrelated to your script and therefore bogus. But it demonstrates what the script is doing, in a roundabout way.

From the blurb in your screenshot - the intention of the script has something to do with counting how many projects each programmer is working on - so I assume the data in the file is actually related to a bunch of programming projects.
So each line must relate to a particular project and the 4th field of each line must be the name of the programmer working on it.

That way cut extracts the name of the programmer who is working on each project from the 4th field of each line. Then sorts the collected list of programmer names alphabetically. Then uniq counts how many times each name appears, yielding a list of programmers and the number of projects they are working on. And awk produces the final report, which is redirected to a file called pnum.

So in order for your script to work properly, you need a file called project, with data formatted appropriately (with each field separated by colons : and a programmers name as the 4th field.).

If this script came from a book, I’d expect it to list some sample data to test the script on.

To execute your script, you should just need to use ./pact. And that should work.

Also, bear in mind that you’re using Kali. And I’m not sure, but I think Kali started using zsh as its default shell.
You can run bash scripts in zsh.
When zsh opens a script, it should read the shebang line to determine which interpreter to use (so for your script, it should load bash).
But zsh will only do that if the shebang line is the very first line in the file.
Again, according to your screenshot of your script - it isn’t. So that could have been another reason for your scripts failure to run.

One final thing - rather than including screenshots/photos, you could just copy/paste code into your posts inside bbcode [code][/code] blocks.

@dos2unix - on most Linux distros, sh is just a symbolic link to the default shell, which in most cases is bash anyway nowadays. So running sh will just run bash. Though sometimes it could be dash, or ash, or another shell.
 
Last edited:
@dos2unix - on most Linux distros, sh is just a symbolic link to the default shell, which in most cases is bash anyway nowadays. So running sh will just run bash. Though sometimes it could be dash, or ash, or another shell.

I'm pretty sure you're right. I do notice for a old redhat 6 system we have it is a separate binary.
My problem is I mess with Solaris, HPUx, AIX, and BSD, so I forget what's what. The default shell in our Solaris is ksh.
In AIX sh is separate from bash. ( In fact, bash isn't a default shell, you have to install it ).
In Fedora 39 bash is the default, but you can make ksh, tcsh or zsh the default if you want to.
Also I think you're right about Kali using zsh, so it would still be a shell calling another shell. That's why I never
trust "sh" :)
 
Last edited:
Hey guys thanks for the responses. Im not sure what ended up being the issue. I just wanted to say I appreciate yall responses.
 
Hi guys I am currently learning to write an execute scripts I just recently used vi to creat a script... Saved it as pact.

But when I use sh or ./pact to execute I get what you see in photos .. I'm wondering if maybe the book I'm learning from which is copyrighted 2002 might be the issue. I'm not sure. But I have added execute permission by
``
chmod +x pact
``
I have confirmed it's in the current directory of where I am trying to execute. But I'm not sure what I'm doing wrong. I will attach picture of my script and of the errors I get.
You need to be in a /bin/sh shelll
 


Top