0

Im having issues with my shell script for school. There is a syntax error but I don't understand whats the issue. its on line 25, but it seems fine to me. I tried going through lecture notes but they don't seem to help. The script is supposed to pull a directory of XML files that are the elements of the periodic table, Write a shell script that will read all the .xml files in data-shell/data/elements directory of the data archive data.zip Download data.zipand create a file "Summary.txt" containing a summary table of the element data.

The summary table should contain 5 columns: "Name" = , "Abbreviation" = , "Weight" = , "Number" = , "Liquid Range" = - . it doesn't want to print the result, so here I am. If anyone can solve this issue, it would be greatly appreciated! enter image description here

the script:

#!/usr/bin/bash

yourfilenames=`ls ./*.xml`

echo -e "Name\tAbbreviation\tWeight\tNumber\tLiquid Range"

for eachfile in $yourfilenames
do

        # here fetching out the values of mentioned variables
        name=($(grep -oP '(?<=element-name>)[^<]+' "$eachfile"))
        symbol=($(grep -oP '(?<=symbol>)[^<]+' "$eachfile"))
        at_weight=($(grep -oP '(?<=atomic-weight>)[^<]+' "$eachfile"))
        at_number=($(grep -oP '(?<=atomic-number>)[^<]+' "$eachfile"))
        boiling_pt=($(grep -oP '(?<=boiling-point>)[^<]+' "$eachfile"))
        melting_pt=($(grep -oP '(?<=melting-point>)[^<]+' "$eachfile"))

        # doing a little bit of mathematical operation for finding out the liquid range of the element
        liquid_range=$((boiling_pt - melting_pt))

        # printing out the values of the variables in a tabular format
        echo -e "$name\t$symbol\t\t$at_weight\t$at_number\t$liquid_range"
done
chepner
  • 446,329
  • 63
  • 468
  • 610
  • You can format code correctly by putting four spaces at the start of each line. Make sure you have a semicolon here: `for eachfile in $yourfilenames; do`. You should also use `for eachfile in ./*.xml; do`, instead of `ls`. – dan Nov 23 '21 at 14:12
  • 2
    Don't try to parse XML files with `grep`. Find a proper XML parser instead. – chepner Nov 23 '21 at 14:14
  • 1
    The error message in the image refers to line 25. This code does not have 25 lines. Paste your code into https://www.shellcheck.net and fix the errors it tells you about. – glenn jackman Nov 23 '21 at 14:25
  • Even if your logic to process one file would be correct, it would break on filenames which contain spaces. If you are going to use _bash_, as your #! line suggests, consider storing the file names into an array instead of using `ls`. – user1934428 Nov 23 '21 at 14:50
  • [Don't parse `ls`](https://mywiki.wooledge.org/ParsingLs). A newline instead of a semicolon is fine before the `do` of a loop, though. That's just a matter of style. – Paul Hodges Nov 23 '21 at 15:10
  • Might I suggest using Python instead of bash for this script? That way you get a real XML parser (instead of regexes that don't really know XML -- can't ignore content in comments, can't recognize elements that are supposed to be ignored because of namespace restrictions, etc), you get real filesystem calls (instead of buggy, hacky attempts to read the output of `ls`), _and_ you get floating-point math built-in instead of needing to use external tools like `bc`. – Charles Duffy Nov 27 '21 at 16:57
  • (mind, you _can_ get real filesystem calls in bash -- `for file in *.xml` does that, if you get rid of the `stringVariable=$(ls *.xml)` trash; wrt. "trash" -- keep in mind that output of `ls` is newline-delimited, _and filenames can contain literal newlines_. Thus, it's impossible for `ls` to accurately represent all possible filenames; see the link Paul gave you above for more). – Charles Duffy Nov 27 '21 at 16:58
  • (as-written, the code also has trouble with much more prosaic filenames, including ones with spaces). – Charles Duffy Nov 27 '21 at 17:00

1 Answers1

0

where is the syntax issue?

It's in the little bit of mathematical operation - the shell is not capable of computation with decimal fractions, and you got 1235.1. You can use bc instead:

        liquid_range=`bc <<<"$boiling_pt - $melting_pt"`
Armali
  • 16,557
  • 13
  • 53
  • 152
  • 1
    Questions that have been "asked and answered many times before" should be closed as duplicates rather than answered -- see the _Answer Well-Asked Questions_ section of [How to Answer](https://stackoverflow.com/help/how-to-answer). – Charles Duffy Nov 27 '21 at 16:56
  • "Where is the syntax issue?" is too broad to be on-topic; Stack Overflow questions need to be narrow and specific. I'm doing the OP the courtesy of pretending they'd done the work to isolate their issue and ask a narrow-enough-to-be-permissible question. For questions that _don't_ isolate a specific problem, see the very first bullet point in the _Answer Well-Asked Questions_ section linked above. – Charles Duffy Nov 27 '21 at 17:20
  • (...one could _also_ legitimately close the question for not including a [mre] -- a properly _minimal_ reproducer would rip out everything that already works; so looking for `*.xml` files, extracting fields from them, etc. would all have been removed before the question was asked as part of building the shortest possible code that produces the OP's problem when run without changes). – Charles Duffy Nov 27 '21 at 17:25
  • Do you have any basis for the claim _"Where is the syntax issue?" is too broad to be on-topic_? How can a question with a precisely defined answer be _too broad_? And if it were _too broad_, I'm not convinced you did the OP a _courtesy_ _pretending_ a different close reason. – Armali Nov 27 '21 at 17:29
  • How could it _not_ be too broad? Nothing in the question itself other than its title (images don't count as "included in the question itself" for the reasons described in [Why not upload images of code/errors when asking a question?](https://meta.stackoverflow.com/a/285557/14122)) even says that there _is_ a syntax error, much less shows what that error is! And if we correct for that, then we just have a question with a broader title asking people to find the bug instead of building a proper [mre] that isolates the smallest possible reproducer. And, again, we already have the question answered – Charles Duffy Nov 27 '21 at 17:41
  • ...if one accepted your interpretation, any duplicate question could suddenly become on-topic again by changing the title to be more vague about what's wrong. Clearly that's absurd: When something is off-topic for multiple reasons, it can be closed for _any_ of those reasons. – Charles Duffy Nov 27 '21 at 17:43
  • Anyhow -- if you want an official second opinion, open a question on [meta]; that's what it's for. – Charles Duffy Nov 27 '21 at 17:45