LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 07-16-2012, 04:24 PM   #1
adrianadonis
LQ Newbie
 
Registered: Jul 2012
Posts: 4

Rep: Reputation: Disabled
Read and execute a text file in Bash


I need a bash script that uses a text file of parameters. It will use groups of five lines at a time. Line 1 is test name. Line 2 and Line 3 is a command that i need to execute. Line 4 is a model file. And line 5 is a redirect file. I need to execute line 2, then execute line 3 and redirect the output to the file that is line 5, finally it has to compare the output and the redirect file. Then go through all that again til it runs out of groups of five.

I'm not very good at scripting so any help you can give me would be great.
Thank you.

EDIT: Can a mod move this post to programming?

Last edited by adrianadonis; 07-16-2012 at 04:28 PM. Reason: posted in the wrong section
 
Old 07-16-2012, 04:28 PM   #2
corp769
LQ Guru
 
Registered: Apr 2005
Location: /dev/null
Posts: 5,818

Rep: Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007
Awk sounds like what you are looking for, first of all. On a side note though, could you post us an example text file? And what do you have so far of your script?
 
Old 07-16-2012, 04:42 PM   #3
adrianadonis
LQ Newbie
 
Registered: Jul 2012
Posts: 4

Original Poster
Rep: Reputation: Disabled
here is an example of a test file:

Code:
testname
echo "this is the first command"
echo "this is the second command"
ModelFileName
OutputFilename
testname2
...

Also I just started to learn bash so anything you can give me to help me start off would be appreciated.
 
Old 07-16-2012, 05:24 PM   #4
sharadchhetri
Member
 
Registered: Aug 2008
Location: INDIA
Distribution: Redhat,Debian,Suse,Windows
Posts: 179

Rep: Reputation: 23
Quote:
Originally Posted by adrianadonis View Post
here is an example of a test file:

Code:
testname
echo "this is the first command"
echo "this is the second command"
ModelFileName
OutputFilename
testname2
...

Also I just started to learn bash so anything you can give me to help me start off would be appreciated.
sed -n '2p' filename

this command will help you to get the line no. 2 from file. I will give you some basic idea.I hope it will help you.
for eg.


#!/bin/bash

x=`sed -n '2p' filename`
y=`sed -n '3p' filename`

echo "below is command-1,line no.2 output" >> OutputFilename
$x >> OutputFilename
echo "below is command-2,line no.3 output" >> OutputFilename
$y >> OutputFilename

And you can see the result by this
Quote:
Quote:
cat OutputFilename

Last edited by sharadchhetri; 07-16-2012 at 05:25 PM.
 
Old 07-16-2012, 08:13 PM   #5
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192
Is the data contained in the file something you are creating? If it is, my suggestion would be to remove the echo commands and leave the lines
on their own to be echoed within your script.
 
Old 07-16-2012, 08:25 PM   #6
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
Here are some good bash links
http://rute.2038bug.com/index.html.gz
http://tldp.org/LDP/Bash-Beginners-G...tml/index.html
http://www.tldp.org/LDP/abs/html/

awk & sed
http://www.grymoire.com/Unix/
 
Old 07-17-2012, 01:10 PM   #7
adrianadonis
LQ Newbie
 
Registered: Jul 2012
Posts: 4

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by grail View Post
Is the data contained in the file something you are creating? If it is, my suggestion would be to remove the echo commands and leave the lines
on their own to be echoed within your script.
the echo is just an example the actual command is a command executed by another program.
 
Old 07-17-2012, 04:10 PM   #8
sharadchhetri
Member
 
Registered: Aug 2008
Location: INDIA
Distribution: Redhat,Debian,Suse,Windows
Posts: 179

Rep: Reputation: 23
Quote:
Originally Posted by adrianadonis View Post
the echo is just an example the actual command is a command executed by another program.
the eg. I gave is it useful
 
Old 07-18-2012, 02:42 AM   #9
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192
Quote:
the echo is just an example the actual command is a command executed by another program.
The general rule of thumb with scripting is to place data in variables and use functions for execution.

If the file must maintain the current format I would probably extract the line and then check the first word(s) in a case or if clause (depending on number of options)
and then call the actual command with the remainder of the line.

So maybe something like:
Code:
#!/bin/bash

mapfile -t lines < test_file

(( n_lines =  ${#lines[*]} % 5 ))

for (( i = 0; i < n_lines; i++ ))
do
    commands=( ${lines[i*5+1]%% *} ${lines[i*5+2]%% *} )

    for (( j = 0; j < 2; j++))
    do
        if [[ ${commands[j]} == echo ]]
        then
            echo "${lines[i*5+j+1]#* }" >> ${lines[i*5+4]}
        fi
    done
done
 
Old 07-20-2012, 04:12 PM   #10
adrianadonis
LQ Newbie
 
Registered: Jul 2012
Posts: 4

Original Poster
Rep: Reputation: Disabled
I have a working bash script now

Thanks for everyone's help I finally have a working bash script. I'm going to post it here if anyone has any comments on it I would like to hear them.

Code:
#!/bin/bash

rm -rf results/*

rm -rf dif/*





# Load text file lines into a bash array.

OLD_IFS=$IFS

IFS=$'\n'

lines_ary=( $(cat "./real.txt") )

IFS=$OLD_IFS



output= $1 





idx=0

function check {
	if [ $char_1 = '#' ]
		let idx=idx+1

	fi
}


# Print each line in the array.

while [ $idx -lt $((${#lines_ary[@]} - 1)) ];

do   
	Char_1= ${lines_ary[@]:0:1}
	if [ $char_1 != '#' ]

 	then
		testname="${lines_ary[$idx]}"

		let idx=idx+1
		check

		testcmd="${lines_ary[$idx]}"

		let idx=idx+1
		check

		outputcmd="${lines_ary[$idx]}"

		let idx=idx+1
		check

		outputfilename=$testname".result"

		modelfilename=$testname".model"

		difffilename=$testname".dif"

		printf "executing testcmd" "${testname}\n"	

		$testcmd

		$outputcmd>>./results/$outputfilename

		diff -q ./results/$outputfilename ./models/$modelfilename>>./dif/$difffilename

		if [[ $? == "0" ]]

		then

			echo -e " \e[00;32mPASS\e[00m"

		else

			echo -e " \e[00;31mFAIL\e[00m"

		fi
	else
		let idx=idx+1
	fi

done
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Read a text file with bash huntaz556 Programming 5 10-27-2011 07:02 AM
How to read a text file using a bash script Jeroen1000 Programming 8 09-30-2009 06:53 AM
how to read text file using bash script kkpal Linux - Newbie 4 03-12-2008 01:57 AM
how to read text file using bash script kkpal Linux - Newbie 2 03-03-2008 11:40 AM
Read variables from a text file in Bash jakev383 Linux - General 5 12-20-2006 07:29 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 07:25 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration