I am trying to run the following script, lets call it foo.sh
from a different location other than its working directory:
var1="$1"
var2="$2"
for SNR_NR in $var1; do
for parts in $var2; do
# echo *-$SNR_NR-$parts
for file in *-$SNR_NR-$parts; do
cat "$file" | sed -n '3p' \
| sed 's/$/,/' >> tmp
cat "$file" | sed -n '4p' \
| tr ' ' ',' \
| sed 's/^.\(.*\).$/\1/' >> tmp
done
done
# Concatenate the above onto one line
paste -d " " - - < tmp
rm tmp
done
It is simple enough, it runs perfectly well from the working directory as so sh foo.sh 5 500
where it takes two numerical integer arguments.
Now the error I am getting if I try to run it from somewhere other than the working directory is:
/data/RESULTS/foo.sh: 18: /data/RESULTS/foo.sh: cannot create tmp: Is a directory
cat: *-0-500: No such file or directory
/data/RESULTS/foo.sh: 22: /data/RESULTS/foo.sh: cannot create tmp: Is a directory
cat: *-0-500: No such file or directory
paste: -: Is a directory
paste: -: Is a directory
rm: cannot remove ‘tmp’: Is a directory
I have tried adding my working directory to $PATH
but it still doesn't work. Running ls -la on foo.sh
gives -rw-------
but doing chmod +x foo.sh
still doesn't work.
The strangest part is that it trying to create a directory called tmp
which I cannot quite understand, since it is just a temporary file to store some intermediate results, which it then prints to the shell.
EDIT:
After taking on board suggestions from the comments, the following now produces this error instead, which is an improvement:
var1="$1"
var2="$2"
for SNR_NR in $var1; do
for parts in $var2; do
# echo *-$SNR_NR-$parts
for file in *-$SNR_NR-$parts; do
cat "$file" | sed -n '3p' \
| sed 's/$/,/' >> brian
cat "$file" | sed -n '4p' \
| tr ' ' ',' \
| sed 's/^.\(.*\).$/\1/' >> brian
done
done
# Concatenate the above onto one line
paste -d " " - - < brian
rm brian
done
Run with sh foo.sh 5 500
results in:
cat: *-0-500: No such file or directory
cat: *-0-500: No such file or directory
SOLUTION:
A simple rewrite produced
var1="$1"
var2="$2"
# Supply results/script directory as argument
var3 = "$3"
# cd into script directory before running it
cd $var3
for SNR_NR in $var1; do
for parts in $var2; do
# echo *-$SNR_NR-$parts
for file in *-$SNR_NR-$parts; do
cat "$file" | sed -n '3p' \
| sed 's/$/,/' >> brian
cat "$file" | sed -n '4p' \
| tr ' ' ',' \
| sed 's/^.\(.*\).$/\1/' >> brian
done
done
# Concatenate the above onto one line
paste -d " " - - < brian
rm brian
done