awk: fatal: cannot open file 'file' for reading (Permission denied)

Viewed 844

A piece code below is part of a larger program which I am running on a remote server via a batch script with #!/bin/bash -l as its first line.

On my local machine it runs normally but on a remote server permission issues arises. What may be wrong?

The description of the code may not important to the problem, but basically the code uses awk in processing the contents of the files based on the names of the files.

Why is awk denied permission to operate on the files? When I run awk directly on a shell prompt of the remote server it works normally.

#!/usr/bin/env python

list_of_files = ["file1", "file2", "file3"]

for file in list_of_files:
       awk_cmd = '''awk '/^>/{print ">" substr(FILENAME,1,length(FILENAME)) ++i; next} 1' ''' + file +  " > tmp && mv tmp " + file + \
                 " | cat files > 'pooled_file' "
       exitcode = subprocess.call(awk_cmd, shell=True)

Any help would be appreciated.

1 Answers

I am pretty sure it is a permissions issue since when you are landing into remote machine it is NOT landing on directory where your Input_file(s) are present, off course it will land in HOME directory of logged in user at remote server. So it is a good practice to mention file names with complete paths(Make sure file names with path you are giving are present in target location too else you could write a wrapper over it to check either files are present or not too). Could you please try following.

#!/usr/bin/env python

list_of_files = ["/full/path/file1", "/full/path/file2", "/full/path/file3"]

for file in list_of_files:
       awk_cmd = '''awk '/^>/{num=split(FILENAME,array,"/");print ">" substr(array[num],1,length(array[num])) ++i; next} 1' ''' + file +  " > tmp$$ && mv tmp$$ " + file + \
                 " | cat files > 'pooled_file' "
       exitcode = subprocess.call(awk_cmd, shell=True)

I haven't tested it but I have changed it as per full path, since awk will print complete path with filename so I have changed FILENAME in your code to as per array's place, I also changed tmp temporary file to tmp$$ for safer side.

Related