0

i'm currently running a r program where i use already 9 argument with "commandArgs()" for my bash script, but it seems i can't use a 10th argument, the program doesn't take it, is there a way i could do it and won't have to make an other program just for that ? thanks in advance for your help ! After some question here is the script:

library(tidyverse)
library(readr)
library(DT)Args 
library(data.table)

args <- commandArgs()

table1 <- read.csv(args[6])
table2 <- read.csv(args[7])

idem <-as.character(intersect(table1$x, table2$x))
same <- data.frame( nom_colonne = idem)

pasidem <-as.character(setdiff(table1$x, table2$x))
passame <- data.frame( nom_colonne = pasidem)

pasidem2 <-as.character(setdiff(table2$x, table1$x))
notsame <- data.frame( nom_colonne = pasidem2)

write.csv(same, args[8], row.names = F,  quote=F)
write.csv(passame, args[9], row.names = F,  quote=F)
write.csv(passame, args[10], row.names = F,  quote=F)

As you can see i ask args 6 to 10 (since 1 to 5 already taken by the system) but it seems i can't use a 10th argument

guillaume
  • 1
  • 1
  • Use `"${10}"` or `"$@"` ? but the question is to vague though – Jetchisel Jun 02 '23 at 10:26
  • sorry new here and don't really know how to put it better, my script already use 9 argument so it work, but when i had a 10th argument i have an error (on my terminal) meaning it can't detect the 10th one (i tried the same command with 9th argument and it work) so the question is more how can i use a 10th argument on my script ? it seems commandArgs only let me use 9 argument max. – guillaume Jun 02 '23 at 10:47
  • 2
    Show some code, and paste your script at https://shellcheck.net – Jetchisel Jun 02 '23 at 10:59
  • 4
    Welcome to SO, guillaume! I've heard of 9 being a (soft) limit for windows cli utils, but there are ways around it (https://stackoverflow.com/a/38390433/3358272). I don't know of any limit imposed by `commandArgs`. It would be very helpful if you make your question more _reproducible_, namely a minimal script attempt, how you are calling it (including the shell type, i.e., `cmd` or `bash`), and all errors/warnings. Please see https://stackoverflow.com/q/5963269 , [mcve], and https://stackoverflow.com/tags/r/info. Thank you! – r2evans Jun 02 '23 at 11:39
  • thanks you ll do it and try make a little model to show what i'm talking about. – guillaume Jun 02 '23 at 12:42

2 Answers2

2

You can use littler and its r front-end. It provides a vector argv (just like C does) which is also easier to use programmatically than commandArgs(). (We most often use this with additional command-line parsing packages; I liked docopt a lot and have many examples.)

Code

#!/usr/bin/r

for (i in seq_along(argv)) {
    cat("Argument", i, "is", argv[i], "\n")
}

Demo

$ ./argcount.r a b c d e f g h i j k l
Argument 1 is a 
Argument 2 is b 
Argument 3 is c 
Argument 4 is d 
Argument 5 is e 
Argument 6 is f 
Argument 7 is g 
Argument 8 is h 
Argument 9 is i 
Argument 10 is j 
Argument 11 is k 
Argument 12 is l 
$ 

(This worked all the way up to 26 when I just tested with all letters.)

Littler ships with a Makevars for Linux and macOS and could build on Windows but I have not a need. If someone wants to "port" this, the very similar package RInside is structured similarly and can provide a model.

Dirk Eddelbuettel
  • 360,940
  • 56
  • 644
  • 725
0

BASH has a builtin named shift which might help you.

#!/usr/bin/bash

echo ${1} ${9}
shift 9
echo ${1}

If you save the above script as shift_example.bash and make it executable then you can call it as below to see what I mean.

./shift_example.bash A B C D E F G H I J K L

The second echo should print J which is the tenth argument.

See https://www.gnu.org/software/bash/manual/bash.html for details.

chandra
  • 63
  • 6