Reading a file passed as an argurement and assessing the files syntax

I am trying to write a shell script which takes an input file as an arguement in the terminal e.g. bash shellscriptname.sh input.txt. I would like for the file to be read line by line each time checking if the .txt file contains certain words or letters(validating the syntax). If the line being read follows the syntax then it should be sent to another file called correct.txt if the syntax is incorrect of the line then it should be sent to incorrect.txt. I have been struggling to get the shell script to read the file and then assess the syntax. Please could someone help

Without seeing your script it will be difficult...
What kind of syntax is it trying to check? your own rules?

They are rules I have been given as requirements for example, line 1 in the txt file is add $s0 $s1 $s2. The shell script is then supposed to view the line of text and run a check to see if there is an add or sub in the line of text, if there is does it have 3 arguments after it.

---------- Post updated at 10:25 AM ---------- Previous update was at 10:19 AM ----------

Is it possible for you to email me, so I can send you some more information? gurdza_pol@hotmail.com

Hello!

In case you forgot to read the forum rules, do not send a private message with a technical question. The forums are for the benefit of all, so all Q&A should take place in the forums.

Users who break the rules are generally banned from the site, so please follow the rules.

Thanks.

The UNIX and Linux Forums

Sorry I am new to the site and was not aware of the rules

Please become accustomed to provide decent context info of your problem.
It is always helpful to support a request with system info like OS and shell, related environment (variables, options), preferred tools, and adequate (representative) sample input and desired output data and the logics connecting the two, to avoid ambiguities and keep people from guessing.

Open questions: input file format, how do you convey the rules, ans preferred tools?

And, is that homework or classwork?

I am writing the shell on notepad called valsplit.sh, when running the script in the ubuntu terminal I am required to supply 3 arguments a .txt file to read information from and 2 more .txt files to send information to. The file to be read is called input.txt it contains lines of text similar to an example I provided above. They are to be read and validated one line at a time. First we are to check if at the start of the line there is the word "add", "sub", "addi", "lw" or "sw". If that passes the test next is to check if the "add" or "sub" has 3 arguments after it containing either a t or an s, they must also have a $ before it. If the command addi is used one of the arguements must be within a certain number range. If they pass the checks they should be sent to the correct.txt file which is the second argument when running the script in the terminal. If they do not pass the checks the line must be to the third argument in the terminal which is the incorrect.txt file and a message should be output to the terminal stating the error.

This is a practice task for revision I am having trouble with setting up the shell script in notepad, creating a while loop to read each line of the input.txt file and the if statements to check the syntax also I am having trouble using the terminal.

First off: you shouldn't. UNIX (any UNIX, including Linux) has a different way of separating text lines in a file , compared to Windows (any Windows-version). If you use (Windows-) notepad.exe to write a file chances are you can't run it on Unix wiht some processing so you will be on the safer side writing the file with a UNIX-based editor on the system itself instead.

OK, but: show us the script! how are we supposed to tell you what is wrong with your script when we haven't seen it yet? Post the content of said valsplit.sh and we will perhaps be able to tell you why it doesn't do what it should. Then you can correct it until it is working. But without having seen it we are like doctors who - instead of examining a patient - are just called by one with the information "it hurts". The reason for this can be everything and then some.

OK. Lets for now assume the thread is legitimate here. We still haven't seen your code and as long as that has not been changed we can't help you. So please post your code (put it in CODE-tags!!) and on we go.

I hope this helps.

bakunin

This might be a start:

#!/bin/bash
# cycle through the arguments
for input in "$@"
do
  err=""
  linenr=0
  # cycle through each line
  while read line
  do
    ((linenr++))
    case $line in
    (*a*)
      : do nothing
    ;;
    (*)
      err="No 'a' in line $linenr"
    ;;
    esac
  done < "$input"
  if [ -n "$err" ]
  then
    echo "${err}, file $input not copied"
  else
    : copy $input
  fi
done

Please try to understand it, and modify it according your needs.