2

When I tried to run my .sh file in Redhat using ./test.sh

I am getting a error like

[test@tester unix_scripts]$ ./test.sh
: No such file or directory
[test@tester unix_scripts]$ 

I have also set the permission of the file with chmod 777 test.sh

still the same error

Can anybody help me?

10 Answers 10

6

Actually the issue was the script that i used was created in dos, so i converted my script to unix by using dos2unix command. Thanks to all for your valuable help.

  • 2
    dos2unix only converts DOS line breaks to UNIX style line breaks. It won't bless the script with the magic to make it compatible with the shell. – Dave Cheney May 28 '09 at 14:14
  • 3
    dos2unix fixes that often for me as well. if the Win line break added at the 1st line - unix cannot find the /bin/sh anymore as it is /bin/sh^M now – webwesen May 28 '09 at 20:02
  • I've had this issue numerous times too. Always fixed with dos2unix – user4767 May 29 '09 at 19:22
4

You may have forgotten to put at the first line of your script:

#!/bin/bash

for example:

#!/bin/bash          
echo Hello World  

If you try a simple shell script like the hello world example above? does it work or not?

By the way: are you sure about SELinux context?
What does: getenforce say?
What does: ls -l test.sh say?

My best advice is try with a very simple script first so you can check every environment and context behavior.

  • the header that i have used is #!/bin/sh in this case how can run the script? – Charly May 28 '09 at 13:38
2

Double check that you are in the right directory (you can view what is in the directory by typing "ls") and the file's name is "test.sh". You may also want to try:

[test@tester unix_scripts]$ sh test.sh
0

Is test.sh in the unix_scripts directory?

Is there a file or directory that is being referenced in test.sh that doesn't exist? Because the error could be coming from within the script, not from the execution of the script...

0

Yes, it should be #! (crash bang!) :)

Can you post the results of the following commands, executed in the directory your script is in?

ls -l
cat test.sh
0

Are you using sh or bash? (ie. what is the first line of your script?)

If you are using #!/bin/sh, then you need to ensure /bin/sh exists (type which sh)

You may need to try #!/bin/bash or another shell if /bin/sh is not installed.

0

Try getenforce. If is returns enforcing then selinux is on and your script may need to be in a different context to run.

If getenforce does return enforcing then check dmesg, /var/log/message /var/log/audit/audit.log or wherever selinux logs to on your host to find the exact problem.

Or just try chcon'ing the script to the same context as another script that does run.

To see a files context use ls -lZ.

Or try

chcon --reference=/script/that/will/work /script/that/wont/work

Maybe check the chcon man page, I'm not 100% sure on the --reference syntax.

0

Putting a hashbang on your script won't change what it takes to run it. You have to either set it to be executable (chmod +x test.sh) and then run it (./test.sh), or you can call it as an argument of the shell (sh test.sh).

You can also put the script in any location from your $PATH variable (e.g. ~/bin/, /usr/local/bin/) and, if it's executable, you can run it from anywhere without referencing its location (test.sh).

If a script is run without being an argument of the shell, it will by default run in whatever shell you're currently using. That is where the hashbang gets important... if you want to make sure it's run as a bash script, even if the user is using tcsh or ksh or something else, you put #!/bin/bash as the first line of the script.

-1

step 1 - check for a shebang on first line of file step 2 - does the shebang point to a real binary? (try which [path after shebang]) step 3 - if step 2 checks out, ./test.sh should work, else.. step 4 - try /full/path/to/sh test.sh


what you posted, charly, makes no sense whatsoever

  • please see the webwesen comment for my answer.. – Charly May 29 '09 at 9:15
-4

you need to change the permissions:

chmod +x test.sh
  • 1
    You need to read the other answers before posting a "new" answer. This exact answer was given over seven years ago. If you agree with an answer, up vote the answer - DO NOT just post a duplicate! That aside, the question actually states that the permissions on the file are 777 already. – Colt Jul 2 '16 at 14:16
  • man, you have a hobby for Saturdays. – MarianP Jul 2 '16 at 16:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.