Save text block to file with bash [duplicate] - bash

This question already has answers here:
How to cat <<EOF >> a file containing code?
(5 answers)
Using variables inside a bash heredoc
(3 answers)
Closed 16 days ago.
Below code try to save a text block to file with bash but report error:
#!/bin/sh
cat > FILE.txt <<EOF
x=1024
x=$(($x/1024))
echo $x
EOF
Error message:
$ ./a.sh
./a.sh: 3: ./a.sh: arithmetic expression: expecting primary: "/1024"
What's proper way to fix such error?

Related

echo and read with pipe issue [duplicate]

This question already has answers here:
Read values into a shell variable from a pipe
(17 answers)
Why variable values are lost after terminating the loop in bash? [duplicate]
(1 answer)
Closed 17 days ago.
First of all, I am sorry, I am learning the bash and I am a newbie.
Please find the below script.
grep "error" /var/log/syslog | while read line
do
echo $line
done
If I am not wrong,The above script will grep the keyword "error" in /var/log/syslog and will send it inside the while loop as STDIN and output will be displayed.
Also please loop the below script.
echo "hello" | read hi
echo $hi
So when I run this script I am not getting any output, why is that?
should I use any loop? only then I will get output?

In Shell script process variables in text [duplicate]

This question already has answers here:
bash replace variable name in string with variable value
(2 answers)
Closed 2 years ago.
I have a file 'test.txt', the contents is "SomeText_$(date '+%Y%m%d')".
When reading this into a variable with:
txt=`cat test.txt`
Then I try to print with
echo $txt
This prints: "SomeText_$(date '+%Y%m%d')"
How do I print this so I receive "SomeText_20200904"
The posted echo will display the content from $txt but not execute anything else.
The second line here with eval will read and process what follows then execute the result as a shell command
txt=`cat test.txt`
eval echo $txt

evaluating expression with bash [duplicate]

This question already has answers here:
What's the point of eval/bash -c as opposed to just evaluating a variable?
(3 answers)
The 'eval' command in Bash and its typical uses
(11 answers)
Closed 2 years ago.
There is something i do not understand with strings in bash:
Look at this script:
#!/bin/bash
tmp="ls"
"$tmp"
This script executes ls command and display result in the console.
Now look at this script:
#!/bin/bash
tmp="ls > out.txt"
"$tmp"
This second script does not execute ls and displays this error:
line 3: ls > out.txt: command not found
I just want to understand. I do not want to understand how to run ls command. I want to understand why the first script works and not the second.
Thanks

Why is this 4-line bash script not working? [duplicate]

This question already has answers here:
When do we need curly braces around shell variables?
(7 answers)
Closed 3 years ago.
Based on this answer: https://stackoverflow.com/a/4141042/226473
I came up with this script:
ALT02884% for i in {0..4}
do
printf "Set for $1" > "$i_directories"
done
But it results in the following:
zsh: no such file or directory:
zsh: no such file or directory:
zsh: no such file or directory:
zsh: no such file or directory:
zsh: no such file or directory:
The shell thinks the underscore is part of the variable name. Use curly braces to tell it to look for a variable named i rather than i_directories.
for i in {0..4}
do
printf "Set for $1" > "${i}_directories"
done

Nested substitution in shell script ($ within a $ field variable) [duplicate]

This question already has answers here:
Bash - variable variables [duplicate]
(4 answers)
Dynamic variable names in Bash
(19 answers)
Closed 5 years ago.
I have a CSV file which contains the field names and values. The field names have given with a '#'
Eg.
test.csv
#field_1,field_2,field_3
1,axt,3
2,bss,3
I need to display the output like this:
heading_1=field_1
heading_2=field_2
heading_3=field_3
The script I have written so far:
headings=$(grep '^#' $arg_file)
echo "Fields name= $headings"
###### put a for loop to take in all headings as separate variables
for ((i=1; i<=$(echo "$headings"|tr ',' ' '|wc -w); i++))
do
echo "Trial $i field"
heading_$i=$(echo "$headings"|cut -d "," -f $i)
var="$(heading_$i)"
echo ${!var}
done
But this is giving me an error:
./script.sh: line 28: heading_1=#field_1: command not found
./script.sh: line 29: heading_1: command not found
What should I do to get the output the way I want? I am new to shell script and I am not really sure if that is even possible.

Resources