25

I'm stumped. I have a script in my /home directory which is executable:

[user@server ~]$ ll
total 4
-rwx------ 1 user user 2608 Jul 15 18:23 qa.sh

However, when I attempt to run it with sudo it says it can't find it:

[user@server ~]$ sudo ./qa.sh 
[sudo] password for user: 
sudo: unable to execute ./qa.sh: No such file or directory

This is on a fresh build. No changes have been made which would cause problems. In fact, the point of the script is to ensure that it is actually built according to our policies. Perhaps maybe it isn't and sudo is actually being broken during the build?

I should also note that I can run sudo with other commands in other directories.

EDIT: The script ( I didn't write it so don't /bin/bash me over it, please ;) )

#! /bin/bash

. /root/.bash_profile

customer=$1

if [ -z "$customer" ]; then

        echo "Customer not provided. Exiting..."
        exit 1

fi

space ()
{
echo
echo '###########################################################################'
echo '###########################################################################'
echo '###########################################################################'
echo
}

g=/bin/egrep

$g ^Listen /etc/ssh/sshd_config
$g ^PermitR /etc/ssh/sshd_config
$g ^LogL /etc/ssh/sshd_config
$g ^PubkeyA /etc/ssh/sshd_config
$g ^HostbasedA /etc/ssh/sshd_config
$g ^IgnoreR /etc/ssh/sshd_config
$g ^PermitE /etc/ssh/sshd_config
$g ^ClientA /etc/ssh/sshd_config

space

$g 'snyder|rsch|bream|shud|mweb|dam|kng|cdu|dpr|aro|pvya' /etc/passwd ; echo ; echo ; $g 'snyder|rsch|bream|shud|mweb|dam|kng|cdu|dpr|aro|pvya' /etc/shadow

space

$g 'dsu|scan' /etc/passwd ; echo ; echo ; $g 'dsu|scan' /etc/shadow

space

$g ${customer}admin /etc/passwd

space

chage -l ${customer}admin

space

$g 'urs|cust|dsu' /etc/sudoers

space

$g dsu /etc/security/access.conf

space

$g account /etc/pam.d/login

space

/sbin/ifconfig -a | $g addr | $g -v inet6

space

echo "10.153.156.0|10.153.174.160|10.120.80.0|10.152.80.0|10.153.193.0|172.18.1.0|10.153.173.0"
echo
$g '10.153.156.0|10.153.174.160|10.120.80.0|10.152.80.0|10.153.193.0|172.18.1.0|10.153.173.0' /etc/sysconfig/network-scripts/route-eth1

space

cat /etc/sysconfig/network-scripts/route-eth2

space

netstat -rn | tail -1

space

cat /etc/sysconfig/iptables

space

cat /etc/hosts

space

##file /usr/local/groundwork ; echo ; echo ; /sbin/service gdma status

##space

cat /etc/resolv.conf

space

HOSTNAME=`echo $HOSTNAME | awk -F. '{ print $1 }'`

nslookup ${HOSTNAME}

echo
echo

nslookup ${HOSTNAME}-mgt

echo
echo

nslookup ${HOSTNAME}-bkp

space

/sbin/service rhnsd status ; echo ; echo ; /sbin/chkconfig --list rhnsd ; echo ; echo ; yum update --security

space

/sbin/service osad status ; echo ; echo ; /sbin/chkconfig --list osad

space

/sbin/service sshd status ; echo ; echo ; /sbin/chkconfig --list sshd

space

/sbin/service snmpd status ; echo ; echo ; /sbin/chkconfig --list snmpd ; echo ; echo ; echo ; cat /etc/snmp/snmpd.conf

space

df -h

space

cat /proc/cpuinfo | $g ^processor

space

free -g

space

if [ -f /etc/rsyslog.conf ]; then

        tail -3 /etc/rsyslog.conf

else

        echo "This system is not running rsyslog."

fi

rm -f $0
  • 3
    try to use sh qa.sh instead of ./qa.sh – Networker Jul 15 '14 at 22:44
  • What is in your script? – ctrl-alt-delor Jul 15 '14 at 22:49
  • @richard Script added – theillien Jul 15 '14 at 22:58
  • @Networker No change in behavior when using this format. – theillien Jul 15 '14 at 22:59
25

This usually happens when the shebang (#!) line in your script is broken.

The shebang is what tells the kernel the file needs to be executed using an interpreter. When run without sudo, the message is a little more meaningful. But with sudo you get the message you got.

For example:

$ cat test.sh
#!/bin/foo
echo bar

$ ./test.sh
bash: ./test.sh: /bin/foo: bad interpreter: No such file or directory

$ bash test.sh
bar

$ sudo ./test.sh
sudo: unable to execute ./test.sh: No such file or directory

$ sudo bash ./test.sh
bar

The bad interpreter message clearly indicates that it's the shebang which is faulty.

  • 9
    This was the issue. There were hidden ^M characters and the interpreter was reading it as part of the shebang line. I ran it through dos2unix and it fixed it right up. Thanks~ – theillien Jul 15 '14 at 23:05
  • 1
    What editor are you using? – ctrl-alt-delor Jul 15 '14 at 23:06
  • 4
    In my case, line ending was set wrong, to CR-LF for Windows, should be LF for Linux. Can take a while before you find that out. – RolfBly Dec 5 '14 at 14:46
9

I just had this exact problem, it turned out to be a text file encoding problem. For me to fix it while running Xubuntu 14.04.3 LTS, I installed dos2unix and converted the script's encoding and then ran the script again using sudo and it worked fine. You can find an example below:

sudo apt-get install dos2unix -y
dos2unix test.sh
sudo chmod u+x test.sh && sudo ./test.sh
  • While this does work, realize that the interpretation of line ending characters is shaped by the shebang on the file. So this can fix the problem, but so can changing the shebang. – ErikE Jun 13 '18 at 17:20
  • I had this problem with the Atom editor on Windows. The default line ending was CRLF. But if you go to Settings > Packages > Line Ending Selector you can change the default line ending to LF. Then when you WinSCP the bash script to Linux it should execute without that misleading No such file or directory error. – snark Feb 11 at 15:00

protected by Community Sep 7 '17 at 13:46

Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).

Would you like to answer one of these unanswered questions instead?

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