What does $IFS$() mean? [closed] - bash

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 years ago.
Improve this question
Just as the title states, I'm confused about $IFS$(), I saw it in a website which said that $IFS$() can replace the blank space, but I don't know how. Can anyone help me?

By $IFS$() they probably mean they change IFS from default white space, to end of string.
From bash manpages:
IFS The Internal Field Separator that is used for word splitting after expansion and to split lines into words with the read builtin command. The default value is ``<space><tab><newline>''.
They mean they do IFS=$() which acts the same as doing IFS=$'\0', declaring that field separator is null character, which marks end of a string.
$() means return the output of command inside parenthesizes, which the is the same as you just pressing Enter key in terminal.
Example:
$ cat test
1 2 3 4 5 6 7
8 9
It will take every number as new variable, because every whitespace (be it single space, tab or new line is considered field separator)
$ for i in $(cat test); do echo $i; done
1
2
3
4
5
6
7
8
9
If we change IFS to $(), output is the same as is in the file:
$ IFS=$();for i in $(cat test); do echo $i; done
1 2 3 4 5 6 7
8 9
Unset IFS and it goes back to looking whitespace as IFS
$ unset IFS
$ for i in $(cat test); do echo $i; done
1
2
3
4
5
6
7
8
9
you can similarly make IFS change to null character with $'\0'
$ IFS=$'\0';for i in $(cat test); do echo $i; done
1 2 3 4 5 6 7
8 9
IFS=$() is basically the same as IFS= or IFS="", you are declaring it equal to empty string so bash looks for end of strings as separators.

Related

Bash arithmetic awk calculation fails [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed last month.
The community is reviewing whether to reopen this question as of yesterday.
Improve this question
The code snippet below extracts values from an xml file and store totals for days 1 to 7 and a grand total for the last 7 days - $lsd. The day figures are 6 digit numbers with 3 decimal places. I cannot make the 'awk' function work. Everything I have tried produces either "0.000" or "". Changing the INC operator to simple addition gives similar results
Needless to say, I'm a complete newbie to Bash Arithmetic. Please can somebody tell me where I'm going wrong.
for i in {1..7} ;
do
day=$(grep -oPm1 "(?<=<d00$i>)[^<]+" < data.xml)
printf "%.3f" $day > day$i.txt ;
awk 'BEGIN{lsd += $day}'
done ;
printf "%.3f" $lsd > lsd1.txt
Please can somebody tell me where I'm going wrong.
(...)
awk 'BEGIN{lsd += $day}'
(...)
printf "%.3f" $lsd > lsd1.txt
does suggests that you are promptly ignoring that awk's variables and bash's variables are separate from each other, consider following example
x=10
awk 'BEGIN{x+=1;print x}' emptyfile
echo $x
does output
1
10
Observe that inside awk command x is unset, thus when increment by 1 is request it is assumed to be 0 thus print x does give 1 and awk did not anything to bash's x as shown by line with echo.
(tested in GNU Awk 5.0.1)

How can I append the same byte to a file a specified number of times? [duplicate]

This question already has answers here:
Bash: repeat character a variable number of times
(3 answers)
Closed 4 months ago.
I have the following code:
yes "$(echo -e "\xff")" | head -n 10 > SomeFile.bin
Which writes 10 times 0xFF and 0x0A (newline) to SomeFile.bin. But my objective is to fill the file with FF.
Is there a way to print only consecutive 0xFF values instead? Without adding a newline every time?
Here is one way:
printf '\xFF%.s' {1..10} >SomeFile.bin

How to evaluate mathematics in Ubuntu console? [duplicate]

This question already has answers here:
Bash: evaluate a mathematical term?
(9 answers)
Closed 6 years ago.
When I try to calculate a maths problem such as 1 + 1 using the Ubuntu console like this:
user#servername:~$ 1 + 1
Ubuntu thinks the first 1 is a command and I get this error:
1: command not found
I then tried to use echo to evaluate the string (with and without quotes) like this:
user#servername:~$ echo 1 + 1
user#servername:~$ echo "1 + 1"
Unfortunatly both output 1 + 1 and not 2.
It would also be greatly appreciated to include a explanation as to why echo does not evaluate the specified string before outputting it?
Also is there is a built-in command that evaluates a string before outputting it (maybe something that behaves like eval in Python)?
Thank you for the help in advance.
The one I usually use is
bc<<<2+2
You can make that easier to type with an alias:
$ alias x='bc<<<'
$ x 53*(27-23)
212
If you put whitespace in the expression you'll need to quote it. Also if the expression starts with an open parenthesis (as opposed to having one in the middle).
$ echo $((1+1))
2
$ echo "1+1" | bc
2
$ awk 'BEGIN{print 1+1}'
2

bash syntax for string and number comparision [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 7 years ago.
Improve this question
Why does bash use -lt, -eq
etc for number comparison, and ==, != etc for string comparison ?
Bash syntax has its quirks which make it difficult to write and remember, but this seems completely non-intuitive for a simple use case.
Source : [bash syntax][1]http://www.ibm.com/developerworks/library/l-bash-test/index.html
bash only has one type: strings. More richly typed languages can overload (e.g.) the > operator to perform string comparison or numerical comparison based on the types of the arguments. Lacking anything other than a string type, bash must have separate operators for the different operations. Compare
[[ 9 > 10 ]] # exit status 0; 9 is lexicographically greater than 10
[[ 9 -gt 10 ]] # exit status 1; 9 is not numerically greater than 10
You can, however, use the normal operators for numbers if you use them inside an arithmetic expression, where bash assumes all values are either numbers or variables with numerical values.
(( 9 > 10 )) # exit status 1, 9 is not numerically greater than 10
(( 9 > foo )) # same as [[ 9 -gt $foo ]]

Compare 2 arrays and sort out different elements into an array [duplicate]

This question already has answers here:
Compare/Difference of two arrays in Bash
(9 answers)
Closed 9 years ago.
it would be appreciated if you can help me out this case.
I have 2 arrays
array1=(1 2 3)
array2=(5 2 6)
Is there anyway to filter out the different elements from comparing those 2 arrays with bash script.
The expected result is
array3=(1 3 5 6)
Thank you so much,
To get the unique elements from an array in bash, you can use this approach:
$ a=(aa ac aa ad)
$ declare -A b
$ for i in ${a[#]}; do b[$i]=1; done
$ echo ${!b[#]}
ac aa ad
The rest is left as an exercice...

Resources