IFS 不仅仅是 R 脚本和 bash 的字符用法

发布于 2024-12-01 16:32:50 字数 1129 浏览 0 评论 0原文

我正在使用 bash 脚本来编写和运行 R 脚本:因为我不精通 RI,所以我常常在 bash 脚本中编写循环和条件,然后将其翻译到 R 脚本或“此处文档”中...正如您可以想象的那样以这种方式生成的 R_scripts 变得非常长并且难以阅读......所以我学习如何用 R 编写循环和条件,但我发现命令 system() 存在一些困难,所以我意识到 shell 脚本在某种程度上是必要的,如果我不想疯狂引用和转义...;-) 我遇到的第一个问题是: 我想声明一个像这样的变量 Rarr="file_1", "file_2", "file_3" ecc 因为我想将它插入到 R_script 中,

cat>my_R_script.R<<EOF
my_arr<-c(${Rarr})
do something with my_arr
EOF

所以需要引号,因为如果 file_names 没有被引用,R 会提示您找不到名为 file_names 的对象

我尝试遵循 数组的逗号分隔元素 定义 IFS="" ,"" 但似乎当 Rarr="${arr[*]}";echo "${Rarr}" 时,arr 的元素仅由 ${IFS} 的第一个字符分隔...在我的例子中,它们将被 " 分隔,有办法避免这种情况吗?

所以基本上我的问题是:如何强制 shell 考虑 ${IFS} 中的所有字符>

无论如何,我找到了解决问题的两个方法..第一个

arr=($(ls -1 | tail))
new_IFS="\" ,\""
Rarr=${arr[0]}
for ((i=1;i<${#arr[@]};i++))
do 
    Rarr="${Rarr}${new_IFS}${arr[$i]}"
    #echo "${Rarr}"
done;
Rarr=\""${Rarr}"\"
#echo ${Rarr}

和另一个参数替换...但我想知道是否有直接解决我的问题的方法,

谢谢您的帮助

I am using bash scripting to write and run R scripts: since I was not proficient in R I used to write loops and conditions in the bash script that then were translated in the R scripts or "here documents"... as you can imagine the R_scripts produced in such a way becomes extremely long and difficult to be read... so I learn how to write loops and conditions with R but I found several difficulties with the command system(), so I realized that shell scripting was somehow necessary if I didn't want to get crazy with quoting and escaping... ;-)
One of the first problems I faced was this:
I wanted to declare a variable like this Rarr="file_1", "file_2", "file_3" ecc
because I wanted to insert it in the R_script

cat>my_R_script.R<<EOF
my_arr<-c(${Rarr})
do something with my_arr
EOF

quotes are needed since if file_names were not quoted R would prompt you that it cannot find the objects named file_names

I tryed to follow the first solution in comma separated elements of array
defining IFS="" ,""
but it seems that when Rarr="${arr[*]}";echo "${Rarr}" the elements of arr are separated just by the first character of ${IFS}... in my case they will be separated by " is there a way to avoid this?

So basically my question is: how to force shell to consider all the characters in ${IFS} ?

anyway I found two workarounds to my problem.. the first

arr=($(ls -1 | tail))
new_IFS="\" ,\""
Rarr=${arr[0]}
for ((i=1;i<${#arr[@]};i++))
do 
    Rarr="${Rarr}${new_IFS}${arr[$i]}"
    #echo "${Rarr}"
done;
Rarr=\""${Rarr}"\"
#echo ${Rarr}

and another with parameter substitution... but I would like to know if there exist a direct solution to my problem

thank you in advance for your help

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

烧了回忆取暖 2024-12-08 16:32:50

使用标准 IFS 将 Rarr 简单地定义为 Rarr="\"file_1\" \"file_2\" \"file_3\"" 怎么样?

How about simply defining Rarr as Rarr="\"file_1\" \"file_2\" \"file_3\"" using the standard IFS ?

暮色兮凉城 2024-12-08 16:32:50

如果我们不使用空格(我们不需要):(

#!/bin/bash
arr=(${arr[*]/#/\"})            # prepend quotes
arr=(${arr[*]/%/\"})            # append quotes
IFS=, eval 'Rarr="${arr[*]}"'   # join by commas

注意:arr 已修改)。

If we do without the spaces (which we don't need):

#!/bin/bash
arr=(${arr[*]/#/\"})            # prepend quotes
arr=(${arr[*]/%/\"})            # append quotes
IFS=, eval 'Rarr="${arr[*]}"'   # join by commas

(note: arr is modified).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文