How to run the script in terminal linux

Hi all,
I have a problem to run the script in bash from terminal, linux. Basically, the script calculates the monthly sum for 4th column and the monthly average for 5th column, and exports the result.

I saved the script as samp.sh and used the code: ./samp.sh, but got the error that: No such file or directory. I think the input and output file directories are correct, so how to solve it? Thanks for your help.

#!/usr/bin/env Rscript

########### INPUTS

## input file directory
indir <- "/input directory/"

## output file directory
outdir <- "/output directory/"

## name of the input file
infile <- "samp.txt"

## name of the monthly
outfile <- "samp.monthly.txt"

coln <- 4

# set working directory to the specified folder
setwd(indir)

# read in the aggregated file
print(paste("reading input file: ",indir,infile,sep=""))

# agg <- read.table(infile,head=F,col.names=c('YEAR','MONTH','DAY','value1','value2'))

# infile is a text file and the format is like this
agg = data.frame(YEAR=rep(1985,45),MONTH=c(rep(2,28),rep(3,17)),DAY=c(1:28,1:17),
                 value1=rnorm(45),value2=rnorm(45)*11-5.6)

##############
library(dplyr)
dfavg <- agg %>% group_by(YEAR,MONTH) %>% summarise_each(funs(mean))

dfsum <- agg %>% group_by(YEAR,MONTH) %>% summarise_each(funs(sum))

dffinal <- dfavg

dffinal[,coln] <- dfsum[,coln]

write.table(dffinal, file = outfile,col.names =F,row.names=F,sep="\t")

Because infile is a text file that has 5 columns and 45 rows for daily data, so I manually created the file to match samp.txt.

Try commenting out

# setwd(indir)

and then work on

Warning message:
funs() is soft deprecated as of dplyr 0.8.0
Please use a list of either functions or lambdas:

It still does not work. The error message is:
: No such file or directory

It seems there is something off with the path in you script. Be sure the script is ran from a folder where other relative path are accessible.

Does it run when executing from the same workdir than you are launching bash script ?

I think you should check you input directory and output directory, and there access right also (we never know). You also could use absolute path for infile and outfile instead of relative path and setwd. Here,I am not sure you are writing your output inside the correct directory.

It is difficult to reproduce the error on our end because we don't have at hand all that is required. You may need to try debug further yourself. You can use logs to try debug your code. (message function for example)
You can also use checks to try for file existence before reading for example.

Some :package: that may help :

About the last point, creating command line script with argument, you could find this series useful: Learn to Write Command Line Utilities in R | sellorm

Also another tutorial with advice on creating commandline programs
https://swcarpentry.github.io/r-novice-inflammation/05-cmdline/

the ability to pass the input and output directory from command line may help here

1 Like

Your directory paths begin with "/" which indicates they're paths starting at the filesystem root. Did you want to use relative paths to the directories? If so, drop the leading slash.

For diagnosing problems with finding files, see if the script works using the full absolute path to the file first. If the full path to the file works. If that works, it indicates that the issue is with relative paths. Usually, because there's an assumption that the working directory of process isn't where you think it is.

A sanity check here might be to throw in

# Print the working directory
getwd()

# Check if the working directory exists
dir.exists(getwd())

# List files in the working directory
list.files(getwd())
2 Likes

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.