Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/email/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在bash脚本中,如何创建一个新的文件名,在其中放置一个$variable?_Bash_Variables_Filenames - Fatal编程技术网

在bash脚本中,如何创建一个新的文件名,在其中放置一个$variable?

在bash脚本中,如何创建一个新的文件名,在其中放置一个$variable?,bash,variables,filenames,Bash,Variables,Filenames,在bash脚本的一个循环中,我正在“做工作”,然后将每次迭代的结果写入自己的文件。我想给这些文件命名 # User-defined Function (UDF) processLine(){ line="$@" # get all args index=$(echo $line | gawk -F::: '{ print $1 }') content=$(echo $line | gawk -F::: '{ print $2 }') # Let's tr it to remove doub

在bash脚本的一个循环中,我正在“做工作”,然后将每次迭代的结果写入自己的文件。我想给这些文件命名

# User-defined Function (UDF)
processLine(){

line="$@" # get all args 
index=$(echo $line | gawk -F::: '{ print $1 }')
content=$(echo $line | gawk -F::: '{ print $2 }')

# Let's tr it to remove double-quotes(42) and lfs(12)!
content=$(echo $content | tr -d \012)
content=$(echo $content | tr -d \042)
content=$(echo $content | tr -d \054)

# - THEN APPEND THE LINE to OUTPUT FILE
echo $index','$content>>OUTPUT

# - ALSO, save 'raw' individual student data as a backup
echo $content>STUDENTS/STUDENT.$index
}
是的,我知道,tr的东西写得很差!嘿,这几乎是我的第一个剧本了

我想写像STUDENT.7534这样的文件名,而不是STUDENT.“7534”-它甚至无法打开

嗯。。。怎么办?

评论:

# User-defined Function (UDF)
processLine(){

    index=$1  # First argument is the index
    shift     # Remove the index
    # Grab the content - "$*" is fine here,
    # and $* or $@ without quotes would probably work
    # And do the editing in a single tr operation
    # And document what we're doing completely
    # Let's tr it to remove double-quotes (42) and lfs (12) and comma (54)!
    content=$(echo "$*" | tr -d \012\042\054)

    # - THEN APPEND THE LINE to OUTPUT FILE
    # Single quotes around the comma are unnecessary
    echo $index,$content >>OUTPUT

    # - ALSO, save 'raw' individual student data as a backup
    echo $content >STUDENTS/STUDENT.$index
}
这段代码中没有任何东西可以在索引中添加(或删除)双引号,因此对这段代码的调用必须将引号放在那里

您说输出文件名是:

学生,“7534”-甚至不能打开

只要您记住shell删除了双引号,就可以打开它,因此您必须停止它这样做:

cat 'STUDENT."7534"'

你所要做的就是说服你的指导者,这是一个安全预防措施,以确保他们知道足够的shell来访问你的受保护文件。

如果调用函数时在第一个参数周围加引号,你可以设置
index=$(echo$index | tr-d')
在生成文件名之前去掉引号。@bta:同意,但通常更容易确保引号没有首先传递给函数。因此请求“how is called”,不仅仅是因为它需要一些操作。例如,
processLine“$studentid”$other\u info“
不会将任何双引号传递给
processLine
,除非它们已经存在于
$studentid
中存储的值中……这将问题推到脚本的该部分如何工作。如果使用虚假数据调用函数,修复调用者比修复函数要好,除非有令人信服的理由不更改调用者。它是从这个循环调用的,这个循环读取一个数字文本文件-指向记录的指针,本质上是:(哎呀!我如何巧妙地格式化此响应?)#使用文件描述符读取$FILE#将循环分隔符设置为行尾BAKIFS=$IFS IFS=$(echo-en“\n\b”)exec 3