Question

I need a sample bash script to compare a first line of a file(Result.txt) to first row and column of another file(table.csv), then send the result to an html file.

I am very basic in coding, this is what I found so far:

#!/bin/sh
Result.txt="$(head -n 1 < $1|tail -n 1)"
table.csv="$(head -n 1 < $2|tail -n 1)"
test "$R.txt" = "$sheet.csv" && (echo The same; exit 0)

Appreciate your help

Was it helpful?

Solution

Slightly tweaking your script.

#!/bin/bash
Res=$(head -n 1 "$1")
tab=$(head -n 1 "$2")
[[ $Res == $tab ]] && echo The same

Notes

  • "dot" is not a valid identifier (i.e. variable name) character: valid is letters, numbers and underscore, and the first character cannot be a number.
  • if you're doing head -1, there's no need to pipe that into tail -1
  • I think [[ is more readable than test, primarily because [[ forces you to have ]]
  • parentheses launch a subshell which is overkill for an echo statement.
    • the exit will only exit the subshell not your program
    • if you have multiple statements, use if ...; then ...; fi -- it's more readable.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top