Shell script will not run, permission denied

  • (2 Pages)
  • +
  • 1
  • 2

17 Replies - 6611 Views - Last Post: 27 December 2012 - 12:32 PM

#1 gregwhitworth   User is offline

  • Tired.
  • member icon

Reputation: 221
  • View blog
  • Posts: 1,613
  • Joined: 20-January 09

Shell script will not run, permission denied

Posted 27 December 2012 - 11:13 AM

Hey guys I have the following shell script that will generate a report:

#!/bin/sh

now=$(date +"%m_%d_%Y")

#I removed username, database and query for security
psql -U username -d database -t -A -F"," -c "SELECT ... REST OF QUERY HERE ..." > /home/username/connect_users_$now.csv


When I run it I get the following error
-bash: /usr/local/bin/get_users.sh: /bin/sh^M: bad interpreter: No such file or directory


My $PATH value is:
/usr/lib64/qt-3.3/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin:/usr/pgsql-9.0/bin/


The file has the following permissions:
-rwxrwxrwx


The current working directory where the file reside is:
/usr/local/bin


Not sure what else to provide. Thanks for your help.

Is This A Good Question/Topic? 0
  • +

Replies To: Shell script will not run, permission denied

#2 jon.kiparsky   User is online

  • Beginner
  • member icon


Reputation: 11736
  • View blog
  • Posts: 19,929
  • Joined: 19-March 11

Re: Shell script will not run, permission denied

Posted 27 December 2012 - 11:20 AM

We'll deal with the obvious first.
>> which sh



What do you get?
Was This Post Helpful? 1
  • +
  • -

#3 sepp2k   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2756
  • View blog
  • Posts: 4,416
  • Joined: 21-June 11

Re: Shell script will not run, permission denied

Posted 27 December 2012 - 11:27 AM

You have a carriage return (that's what ^M stands for) at the end of your shebang line. Remove it.

This post has been edited by sepp2k: 27 December 2012 - 11:28 AM

Was This Post Helpful? 1
  • +
  • -

#4 gregwhitworth   User is offline

  • Tired.
  • member icon

Reputation: 221
  • View blog
  • Posts: 1,613
  • Joined: 20-January 09

Re: Shell script will not run, permission denied

Posted 27 December 2012 - 11:33 AM

Unfortunately that didn't work. I pulled out all carriage returns so now it's:
#!/bin/sh
now=$(date +"%m_%d_%Y")

As far as the which, here is the result:
/bin/sh


Was This Post Helpful? 0
  • +
  • -

#5 no2pencil   User is offline

  • Professor Snuggly Pants
  • member icon

Reputation: 6832
  • View blog
  • Posts: 31,493
  • Joined: 10-May 07

Re: Shell script will not run, permission denied

Posted 27 December 2012 - 11:34 AM

I would recommend checking for the file before executing it.
file=/home/username/connect_users_$now.csv
if [ -f ${file} ]; then
  psql -U username -d database -t -A -F"," -c "SELECT ... REST OF QUERY HERE ..." > /home/username/connect_users_$now.csv
else
  echo ${file} does not exist
fi



If it is being found, it could possibly be open by another process, which could trigger a permission issue even though it isn't permission based.
Was This Post Helpful? 0
  • +
  • -

#6 gregwhitworth   User is offline

  • Tired.
  • member icon

Reputation: 221
  • View blog
  • Posts: 1,613
  • Joined: 20-January 09

Re: Shell script will not run, permission denied

Posted 27 December 2012 - 11:36 AM

no2pencil

I like the idea but I am not executing the CSV I am merely creating one, so it can't exist until the script is run.
Was This Post Helpful? 0
  • +
  • -

#7 no2pencil   User is offline

  • Professor Snuggly Pants
  • member icon

Reputation: 6832
  • View blog
  • Posts: 31,493
  • Joined: 10-May 07

Re: Shell script will not run, permission denied

Posted 27 December 2012 - 11:37 AM

If it exists, would that throw an error?

Perhaps it isn't you that has the permissions issue, it is the user level under which your calling function is running, compared to the file it's trying to write to, that may already exist with a higher required security level for writing.

In which case we reverse the check process for proper output :

file=/home/username/connect_users_$now.csv
if [ -f ${file} ]; then
   echo ${file} already exist
   rm -f ${file}
fi
psql -U username -d database -t -A -F"," -c "SELECT ... REST OF QUERY HERE ..." > /home/username/connect_users_$now.csv


Was This Post Helpful? 0
  • +
  • -

#8 sepp2k   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2756
  • View blog
  • Posts: 4,416
  • Joined: 21-June 11

Re: Shell script will not run, permission denied

Posted 27 December 2012 - 11:38 AM

View Postgregwhitworth, on 27 December 2012 - 07:33 PM, said:

Unfortunately that didn't work. I pulled out all carriage returns so now it's:


If you're still getting the same error message, you most definitely did not remove all carriage returns. Can you explain how you attempted to remove them?
Was This Post Helpful? 0
  • +
  • -

#9 gregwhitworth   User is offline

  • Tired.
  • member icon

Reputation: 221
  • View blog
  • Posts: 1,613
  • Joined: 20-January 09

Re: Shell script will not run, permission denied

Posted 27 December 2012 - 11:46 AM

Through VIM. I have no blank spaces between the lines and it's only 3 lines long, and at the end there are the ~ so I know there are none at the end of the file.

UPDATE

Also I just copied the file down to my local machine, opened it up in Sublime and looked at the hidden characters and no carriage return is in there. The only hidden characters that are in there are the dots for spaces.

This post has been edited by gregwhitworth: 27 December 2012 - 11:50 AM

Was This Post Helpful? 0
  • +
  • -

#10 no2pencil   User is offline

  • Professor Snuggly Pants
  • member icon

Reputation: 6832
  • View blog
  • Posts: 31,493
  • Joined: 10-May 07

Re: Shell script will not run, permission denied

Posted 27 December 2012 - 11:50 AM

View Postjon.kiparsky, on 27 December 2012 - 01:20 PM, said:

We'll deal with the obvious first.
>> which sh



What do you get?

After checking wich, run echo $SHELL

Also, how are you calling this script?
Was This Post Helpful? 1
  • +
  • -

#11 sepp2k   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2756
  • View blog
  • Posts: 4,416
  • Joined: 21-June 11

Re: Shell script will not run, permission denied

Posted 27 December 2012 - 11:51 AM

Okay, just to avoid any misunderstandings: newline != carriage return. If you're still getting the same error message, you definitely, without a doubt still have carriage returns in your file.

Note that if vim detects your file as being in "dos format" (which it will if all the lines have a carriage return before the newline), it will not display any carriage returns (only a [dos] indicator in the status line). To remove the carriage returns, you first have to set the file format to unix to make them visible.

Also if you're absolutely convinced that you don't have any carriage returns in there, please post the output of cat -v get_users.sh.

This post has been edited by sepp2k: 27 December 2012 - 11:52 AM

Was This Post Helpful? 2
  • +
  • -

#12 gregwhitworth   User is offline

  • Tired.
  • member icon

Reputation: 221
  • View blog
  • Posts: 1,613
  • Joined: 20-January 09

Re: Shell script will not run, permission denied

Posted 27 December 2012 - 11:56 AM

echo $SHELL returns:
/bin/bash


How am I calling a the script:

right now I'm just in the /usr/local/bin directory and typing in the file name to make sure that it works right before I place it in the crontab.

@sepp2k

As far as the returns go I understand what you're getting at I just don't know any other way (especially inside of Linux) to see where the carriage returns are. You mention changing the file to a unix format, how do I go about this?

View Postsepp2k, on 27 December 2012 - 11:51 AM, said:

Also if you're absolutely convinced that you don't have any carriage returns in there, please post the output of cat -v get_users.sh.



Ok, I did that and I can see the ^M symbols, but I have even deleted it so that the #!/bin/sh is right next to the date command and pressed enter in hopes to remove it, to no avail. Any tips?
Was This Post Helpful? 0
  • +
  • -

#13 gregwhitworth   User is offline

  • Tired.
  • member icon

Reputation: 221
  • View blog
  • Posts: 1,613
  • Joined: 20-January 09

Re: Shell script will not run, permission denied

Posted 27 December 2012 - 12:01 PM

So I created a new file in VIM and pasted in my code and the carriage returns are gone. Thanks for your help guys!
Was This Post Helpful? 0
  • +
  • -

#14 no2pencil   User is offline

  • Professor Snuggly Pants
  • member icon

Reputation: 6832
  • View blog
  • Posts: 31,493
  • Joined: 10-May 07

Re: Shell script will not run, permission denied

Posted 27 December 2012 - 12:03 PM

Glad you got it resolved!
Was This Post Helpful? 0
  • +
  • -

#15 jon.kiparsky   User is online

  • Beginner
  • member icon


Reputation: 11736
  • View blog
  • Posts: 19,929
  • Joined: 19-March 11

Re: Shell script will not run, permission denied

Posted 27 December 2012 - 12:03 PM

A bit of searching turns up a plausible-looking bit of perl:

perl -p -i -e 's/\r\n$/\n/g' file1.txt file2.txt ... filen.txt



EDIT: Problem sorted, never mind.

This post has been edited by jon.kiparsky: 27 December 2012 - 12:05 PM

Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2