I have a variable that am declaring like below in my shell script:
Variable = awk 'Some code' filename.txt << this is assigning one word from the text file to this variable
And then am trying to display this in an HTML heading like below, but its not working.
<h2><font color="navy"> Network-element : ${variable} </font></h2>
any help is appreciated.
I think there could be 2 points here.
1- Cover the variable value like:
Variable=$(awk 'Some code' filename.txt)
2- Then in shell script html code doesn't work like simple print you have to use echo for it (NOTE this is only an example)eg->
echo "<html>" > $OUTPUT_FILE
echo "<title>" >> $OUTPUT_FILE
echo "A Test script." >> $OUTPUT_FILE
echo "</title>" >> $OUTPUT_FILE
echo "</body>" >> $OUTPUT_FILE
echo "</html>" >> $OUTPUT_FILE
This above code is only an example where I am putting echo statements into output file. You could use it as per your use case. Also use "$Variable" in echo command.
Related
while read row do
echo "<tr>" >> $file
for valore in $row
do
echo "<td>$valore</td>" >> $file
done
echo "</tr>" >> $file
done < alunni.txt
echo "</table>" >> $file
When I execute this fraction of code it gives me this error:
./table_html.csv: line 36: syntax error near unexpected token `echo'
./table_html.csv: line 36: ` echo "<td>$valore</td>" >> $file'
How do I fix it?
I'm not very good with shell scripts, that's why I can not think of anything else.
(Ah I was working on this when the alarm for the oven went off) what I wanted to say was it's just the "do" in the wrong place after the while. So here's your code restated (with a file="filename" added to get it working):
file="test2.html"
while read row
do
echo "<tr>" >> $file
for valore in $row
do
echo "<td>$valore</td>" >> $file
done
echo "</tr>" >> $file
done < alunni.txt
echo "</table>" >> $file
So it was just a line break before the do is all you needed :-)
Recently github has announced change that echo "::set-output name=x::y" command is deprecated and should be replaced by echo "x=y" >> $GITHUB_OUTPUT
The previous command was able to process multilined value of b while the new approach fails with the folllowing errors
Error: Unable to process file command 'output' successfully.
Error: Invalid format
In my script, I populate a variable message with a message text that should be sent to slack. I need output variables to pass that text to the next job step which performs the send operation.
message="Coverage: $(cat coverage.txt). Covered: $(cat covered.txt). Uncovered: $(cat uncovered.txt). Coverage required: $(cat coverageRequires.csv)"
The last part of message includes context of a csv file which has multiple lines
While the set-output command was able to process such multilined parameters
echo "::set-output name=text::$message"
the new version fails
echo "text=$message" >> $GITHUB_OUTPUT
What can be done to fix or avoid this error?
The documentation describes syntax for multiline strings in a different section but it seems to work even for output parameters.
Syntax:
{name}<<{delimiter}
{value}
{delimiter}
This could be interpreted as:
Set output with the defined name, and a delimiter (typically EOF) that would mark the end of data.
Keep reading each line and concatenating it into one input.
Once reaching the line consisting of the defined delimiter, stop processing. This means that another output could start being added.
Therefore, in your case the following should work and step's text output would consist of a multiline string that $message contains:
echo "text<<EOF" >> $GITHUB_OUTPUT
echo "$message" >> $GITHUB_OUTPUT
echo "EOF" >> $GITHUB_OUTPUT
...and unless you need $message for something else, you could actually avoid setting it and get a more readable set of instructions to construct the output:
echo "text<<EOF" >> $GITHUB_OUTPUT
echo "Coverage: $(cat coverage.txt)." >> $GITHUB_OUTPUT
echo "Covered: $(cat covered.txt)." >> $GITHUB_OUTPUT
echo "Uncovered: $(cat uncovered.txt)." >> $GITHUB_OUTPUT
echo "Coverage required: $(cat coverageRequires.csv)" >> $GITHUB_OUTPUT
echo "EOF" >> $GITHUB_OUTPUT
Note: The last example is not 100% same as yours because it would contain new lines between the sections. You could use echo -n to avoid that.
I ended up having replaced all breaklines in the message variables by the command
message=$(echo $message | tr '\n' ' ')
echo "text=$message" >> $GITHUB_OUTPUT
This eliminated the error.
The previous command was able to process multilined value of b while the new approach fails with the folllowing errors
Actually it has not been, but lately they changed the behaviour:
https://github.com/orgs/community/discussions/26288
What can be done to fix or avoid this error?
The same way as was for the GITHUB_ENV variable:
https://docs.github.com/en/actions/using-workflows/workflow-commands-for-github-actions#multiline-strings
echo 'var<<EOF' >> $GITHUB_OUTPUT
echo "<multi-line-output>" >> $GITHUB_OUTPUT
echo 'EOF' >> $GITHUB_OUTPUT
Or more fancy way:
https://github.com/orgs/community/discussions/26288#discussioncomment-3876281
delimiter="$(openssl rand -hex 8)"
echo "output-name<<${delimiter}" >> "${GITHUB_OUTPUT}"
echo "Some\nMultiline\nOutput" >> "${GITHUB_OUTPUT}"
echo "${delimiter}" >> "${GITHUB_OUTPUT}"
Another option to set multilines in outputs could be using this implementation (same as for ENV variables in the $GITHUB_ENV):
- name: Setup output var
id: test1
run: |
MESSAGE=$(cat << EOF
first line
second line
third line
...
EOF
)
echo TEST=$MESSAGE >> $GITHUB_OUTPUT
- name: Check output var
run: |
echo ${{steps.test1.outputs.TEST}}
I made a test here with the same behavior that for environment variables (detailed in this other thread)
EDIT 1:
This syntax also works (and looks easier to use):
run: |
echo "TEST1=first line \
second line \
third line" >> $GITHUB_OUTPUT
EDIT 2:
It's also possible to display the output as multilines (and not on a single line as the other examples above). However, the syntax would be different and you would need yo use echo -e together with \n inside the variable.
Example:
- name: Setup output var
id: test
run: echo "TEST=first line\n second line\n third line" >> $GITHUB_OUTPUT
- name: Check output var
run: |
echo ${{steps.test.outputs.TEST}} #Will keep the n from the \n
echo -e "${{steps.test.outputs.TEST}}" #Will break the line from the \n
steps:
- run: |
some_response=$(curl -i -H "Content-Type: application/json" \
-d "${body}" -X POST "${url}")
echo response_output=$some_response >> $GITHUB_OUTPUT
id: some-request
- run: |
echo "Response is: ${{ steps.some-request.outputs.response_output }}"
Worked for me well. Quotes (and curly brackets) are not needed in case of just setting output var
I have written a script myscript which does an action on particular type of files. However there is an option for displaying the filename with -d. If I've not mentioned
$ myscript -d
also it is saying, invalid file path (because that file should be file_evaluated AFTER the action of my script on it) but -d is triggered even if it's not mentioned. How do I solve this? Do help. Thanks.
myscript.sh:
#!/bin/zsh
echo "Enter source directory: "
read directory
echo
echo "Add a file (with path): "
read file
echo
while getopts ":rd" cal; do
case "$cal" in
r) echo "Renaming..."
echo
;;
d) echo "Displaying the Renamed files.."
echo
;;
*) echo "Invalid option: $OPTARG"
;;
esac
done
shift $((OPTIND -1))
echo
find . "*.txt" -print0 | while read -d $'\0' file
do
anothescript -r "$file" -d
done
echo "done..."
I am setting up a bash/html CGI that will allow me to generate graphs with GnuPlot.
For this, I have two CGI pages in bash/html:
The first page contains a simple "generate" button that opens a new page and executes my gnuplot script, and a listbox that will have been previously filled with the names of different clusters.
The second page that is opened via the "generate" button displays my graph.
My question is: how do I put gnuplot commands in my CGI page in bash/html ?
Here is the code of my first page that allows me to choose the cluster that interests me :
#!/bin/bash
echo "Content-type: text/html"
echo ""
echo '
<html>
<head>
<meta http-equiv="Content-Type" content="test/html"; charset=UTF-8">
<title> CLUSTER GRAPH </title>
<h1> Cluster Graph <font size=3> [ Index ] </font> </h1>
<hr size="4" color="blue">
</head>
<body>
<p> Choose a Cluster and press the button to generate the graph ! </p>'
Cluster_Name=$(cat ClusterFullList.csv | awk '{print $3}' | sort |uniq)
echo "<form action="Script_test.sh" method="post">"
echo "<select name="CLUSTER">"
echo "$Cluster_Name" | while read CLUSTER; do
echo " <option value="$CLUSTER">$CLUSTER</option>"
done
echo "</select>"
echo"<br><br>"
echo "<input type="submit" value="Generate">"
echo "</form>"
echo '
</body>
</html>
'
Here is the code of my second page on which my graph should appear:
#!/bin/bash
echo "Content-type: text/html"
echo ""
echo "
<html>
<head>
<title> CLUSTER GRAPH </title>
<h1> Cluster Graph <font size=3> [ Index ]</font></h1>
<hr size="4" color="blue">
</head>
<body>
<img src="$test.png">
<PRE>"
read a
test=`echo $a | cut -d'=' -f2`
echo "$test"
echo " f(w) = (strlen(w) > 10 ? word(w, 1) . "\n" . word(w, 2) : w) " >> gnuplot_script_test.txt
echo " set title $test " >> gnuplot_script_test.txt
echo " set terminal png truecolor size 960, 720 " >> gnuplot_script_test.txt
echo ' set output "$test.png" ' >> gnuplot_script_test.txt
echo " set bmargin at screen 0.1 " >> gnuplot_script_test.txt
echo " set key top center " >> gnuplot_script_test.txt
echo " set grid " >> gnuplot_script_test.txt
echo " set style data histograms " >> gnuplot_script_test.txt
echo " set style fill solid 1.00 border -1 " >> gnuplot_script_test.txt
echo " set boxwidth 0.7 relative " >> gnuplot_script_test.txt
echo " set yrange [0:] " >> gnuplot_script_test.txt
echo " set format y "%g%%" " >> gnuplot_script_test.txt
echo " set datafile separator "," " >> gnuplot_script_test.txt
echo ' plot "/var/www/cgi-bin/CLUSTER_1.txt" using 2:xtic(f(stringcolumn(1))) title " CPU consumption (%) ", '' using 3 title " RAM consumption (%)", '' using 0:($2+1):(sprintf("%g%%",$2)) with labels notitle, '' using 0:($3+1):(sprintf(" %g%%",$3)) with labels notitle ' >> gnuplot_script_test.txt
gnuplot gnuplot_script_test.txt
rm gnuoplot_script_test.txt
echo "
</PRE>
</body>
</html>
"
The idea is :
The commands:
read a
test=`echo $a | cut -d'=' -f2`
echo "$test"
Allow me to retrieve my query string which is the name of the cluster I am interested in (cluster name which is in the list box of my first page)
I execute the gnuplots commands and send them each time in the file "gnuplot_script_test.txt", then I execute it via the command :
gnuplot gnuplot_script_test.txt
Then the txt file deletes itself via the command :
rm gnuplot_script_test.txt
I would like to make sure that the name of the cluster I retrieve via my query string, I can put it in my gnuplot commands. To do this, I put my variable "$test" (which contains the name of my cluster) in the lines :
echo" set title "$test""
To put the name of my cluster in my title
echo ' set output "$test.png" '
To give my png the name of my cluster
But, when I choose my cluster on my first page, that I click on generate, the name of my cluster appears well on my second page, but the image is not generated...
Can you tell me why ?
Thank you !
You are using $test variable before you set it up.
You can find helpful heredoc http://tldp.org/LDP/abs/html/here-docs.html
Then you can do e.g.:
gnuplot <<EOF_GNUPLOT
# your gnuplot code here - e.g.:
reset
set term png truecolor enhanced size 1050,1050
set xtics out
# ...
EOF_GNUPLOT
I want to write a script which will colour lines of the input green if they represent the current date (i.e. on 2017-07-13, the second line should be green), red otherwise.
Input format (CSV):
Feed1,2017-07-01
Feed2,2017-07-13
Feed3,2017-07-03
Here is what I have so far:
while IFS="," read dtts feed; do
if [ "$dtts" == "$DATEVALID" ]; then
echo "<tr>"
echo "<td><font color=green>$dtts</font></td>"
echo "<td bgcolor=green>$feed</td>"
echo "</tr>"
else
echo "<tr>"
echo "<td><font color=red>$dtts</font></td>"
echo "<td bgcolor=red>$feed</td>"
echo "</tr>"
fi
done < $INPUTFILE.csv
But it is showing incorrect output (all lines have the same colour).
I see a couple of possible problems with your code. Some have been fixed when editing your original question, please consider formatting your code properly when asking a question on SO.
You are reading fields from the CSV in the incorrect order.
read dtts feed # This means the first field is $dtts, the second is $feed
So the while loop should probably look like
while IFS="," read feed dtts; do
And finally, you did not provide the code responsible for $DATEVALID or $INPUTFILE. An important question is whether $DATEVALID is in the same format as the one used in your input file. To obtain such date within a shell script, you could try:
DATEVALID=`date "+%Y-%m-%d"`
This should give you today's date in the YYYY-MM-DD format you need. To summarise:
DATEVALID=`date "+%Y-%m-%d"`
INPUTFILE="input.csv"
while IFS="," read feed dtts; do
echo "<tr>"
if [ "$dtts" == "$DATEVALID" ]; then
echo "<td><font color=green>$dtts</font></td>"
echo "<td bgcolor=green>$feed</td>"
else
echo "<td><font color=red>$dtts</font></td>"
echo "<td bgcolor=red>$feed</td>"
fi
echo "</tr>"
done < $INPUTFILE
First problem -- your data does not match the variables you are specifying in your read command.
First question -- what is the value of $DATEVALID ?
echoing your variables will go a long way toward troubleshooting these types of problems.