ifs

shell - temp IFS as newline only. Why doesn't this work: IFS=$(echo -e '\\n')

﹥>﹥吖頭↗ 提交于 2019-12-04 06:54:02
I'm trying to use for in the shell to iterate over filenames with spaces. I read in a stackoverflow question that IFS=$'\n' could be used and that seems to work fine. I then read in a comment to one of the answers that to make it posix compatible for shells other than bash one could use IFS=$(echo -e '\n') . The former works but the latter doesn't. The comment is upvoted several times. I checked the output and the variable doesn't seem to contain the newline. #!/bin/sh IFS=$(echo -e '\n') echo -n "$IFS" | od -t x1 for file in `printf 'one\ntwo two\nthree'`; do echo "Found $file" done echo IFS=

点云数据的读取(las、txt、xyz格式)

二次信任 提交于 2019-12-04 03:13:19
主要实现las格式、txt格式、xyz格式点云文件的读取及显示 (1)las点云数据直接使用编译好的LibLAs库进行读取 std::ifstream ifs; std::ifstream ifs; ifs.open(path, std::ios::in | std::ios::binary); if (!ifs) { cout << "打开失败!" << endl; } liblas::ReaderFactory f; liblas::Reader reader = f.CreateWithStream(ifs); liblas::Header const& header = reader.GetHeader(); double maxX = header.GetMaxX(); double minX = header.GetMinX(); double maxY = header.GetMaxY(); double minY = header.GetMinY(); double maxZ = header.GetMaxZ(); double minZ = header.GetMinZ(); int n=header.GetPointRecordsCount();//获取总的点数 double x = 0, y = 0, z = 0; while (reader

When do I set IFS to a newline in Bash?

旧时模样 提交于 2019-12-03 21:04:15
问题 I thought setting IFS to $'\n' would help me in reading an entire file into an array, as in: IFS=$'\n' read -r -a array < file However, the above command only reads the first line of the file into the first element of the array, and nothing else. Even this reads only the first line into the array: string=$'one\ntwo\nthree' IFS=$'\n' read -r -a array <<< "$string" I came across other posts on this site that talk about either using mapfile -t or a read loop to read a file into an array. Now my

Why does command substitution change how quoted arguments work?

匿名 (未验证) 提交于 2019-12-03 08:36:05
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I have the following snippet: printf "%s\n%s\n%s" $(echo "'hello world' world") I would expect it to produce: hello world world But it actually produces: 'hello world' world Why is the above command not the same as the following? printf "%s\n%s\n%s" 'hello world' world 回答1: After command substitution, only word splitting and wildcard expansion are done, but not quote processing. From tbe Bash Reference Manual The order of expansions is: brace expansion, tilde expansion, parameter, variable, and arithmetic expansion and command substitution

How to parse $QUERY_STRING from a bash CGI script

匿名 (未验证) 提交于 2019-12-03 07:50:05
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I have a bash script that is being used in a CGI. The CGI sets the $QUERY_STRING environment variable by reading everything after the ? in the URL. For example, http://example.com?a=123&b=456&c=ok sets QUERY_STRING=a=123&b=456&c=ok . Somewhere I found the following ugliness: b=$(echo "$QUERY_STRING" | sed -n 's/^.*b=\([^&]*\).*$/\1/p' | sed "s/%20/ /g") which will set $b to whatever was found in $QUERY_STRING for b . However, my script has grown to have over ten input parameters. Is there an easier way to automatically convert the parameters

Bash string to array with IFS

匿名 (未验证) 提交于 2019-12-03 03:10:03
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I'm having trouble using the IFS to convert my string into an array. Here is what I have as my string: "Jun01 Jun02 Jun03 Jun04 Jun05 ..." #in that format, separated by spaces And here is the code I have tried: IFS=" " #set it to space character DATES_ARRAY=($DATES_STRING) #from above echo ${DATES_ARRAY[0]} #output is empty However when I remove the IFS line it works. But I used a few lines to print out its default ASCII value and I got '32' which means 'Space' character. Being an OCD programmer I'd like to set it myself just to be safe... I

How do I split a string on a delimiter in Bash?

匿名 (未验证) 提交于 2019-12-03 02:11:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I have this string stored in a variable: IN="bla@some.com;john@home.com" Now I would like to split the strings by ; delimiter so that I have: ADDR1="bla@some.com" ADDR2="john@home.com" I don't necessarily need the ADDR1 and ADDR2 variables. If they are elements of an array that's even better. After suggestions from the answers below, I ended up with the following which is what I was after: #!/usr/bin/env bash IN="bla@some.com;john@home.com" mails=$(echo $IN | tr ";" "\n") for addr in $mails do echo "> [$addr]" done Output: > [bla@some.com] >

How to use bash return code in conditional?

匿名 (未验证) 提交于 2019-12-03 01:10:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 选择语言 中文(简体) 日语 英语 中文(繁体) 由 翻译 强力驱动 问题: I have a small piece of code which checks IP address validity : function valid_ip () { local ip = $1 local stat = 1 if [[ $ip =~ ^[ 0 - 9 ]{ 1 , 3 } \. [ 0 - 9 ]{ 1 , 3 } \. [ 0 - 9 ]{ 1 , 3 } \. [ 0 - 9 ]{ 1 , 3 } $ ]]; then OIFS = $IFS IFS = '.' ip =( $ip ) IFS = $OIFS if [[ $ { ip [ 0 ]} - le 255 && $ { ip [ 1 ]} - le 255 \ && $ { ip [ 2 ]} - le 255 && $ { ip [ 3 ]} - le 255 ]]; then stat = 1 else stat = 0 fi fi return $stat } But I am having problems with its usage in bash conditionals. I have tried

IFS separate a string like “Hello”,“World”,“this”,“is, a boring”, “line”

自古美人都是妖i 提交于 2019-12-02 12:55:55
I'm trying to parse a .csv file and I have some problems with IFS. The file contains lines like this: "Hello","World","this","is, a boring","line" The columns are separated with a comma, so I tried to explode the line with this code: IFS=, read -r -a tempArr <<< "$line" But I get this output: "Hello" "World" "this" "is a boring" "line" I understand why, so I tried some other commands but I don't get my expected output. IFS=\",\" IFS=\", IFS=',\"' IFS=,\" Every time the third element is seperated in 2 parts. How can I use IFS to seperate the string to 5 parts like this? "Hello" "World" "this"

SHELL学习笔记三

这一生的挚爱 提交于 2019-12-02 06:05:38
SHELL学习笔记三 SHELL学习笔记一 SHELL学习笔记二 SHELL学习笔记三 for 命令 for var in list do commands done $ cat test1 #!/bin/bash # basic for command for test in Alabama Alaska Arizona Arkansas California Colorado do echo The next state is $test done $ ./test1 The next state is Alabama The next state is Alaska The next state is Arizona The next state is Arkansas The next state is California The next state is Colorado $ 读取列表中的复杂值 使用转义字符(反斜线)来将单引号转义; 使用双引号来定义用到单引号的值。 $ cat test2 #!/bin/bash # another example of how not to use the for command for test in I don\'t know if "this'll" work do echo "word:$test" done $ .