Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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?

share|improve this question

9 Answers 9

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.

share|improve this answer
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

you may have forgotten to put at the first ligne 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 says ? what does : ls -l test.sh says ?

my best advice is try with a very simple script first so you can check every environnement and context behaviors.

hope this helps

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

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
share|improve this answer

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...

share|improve this answer

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
share|improve this answer

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.

share|improve this answer

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.

share|improve this answer

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.

share|improve this answer

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

share|improve this answer
    
please see the webwesen comment for my answer.. –  Charly May 29 '09 at 9:15

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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