LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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, 05: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 05:28 PM. Reason: posted in the wrong section
 
Old 07-16-2012, 05:28 PM   #2
corp769
LQ Guru
 
Registered: Apr 2005
Posts: 5,817

Rep: Reputation: 1002Reputation: 1002Reputation: 1002Reputation: 1002Reputation: 1002Reputation: 1002Reputation: 1002Reputation: 1002
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, 05: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, 06: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 06:25 PM.
 
Old 07-16-2012, 09:13 PM   #5
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,256

Rep: Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686
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, 09:25 PM   #6
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.8, Centos 5.10
Posts: 17,241

Rep: Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325
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, 02: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, 05: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, 03:42 AM   #9
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,256

Rep: Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686
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, 05: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


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 08:02 AM
How to read a text file using a bash script Jeroen1000 Programming 8 09-30-2009 07:53 AM
how to read text file using bash script kkpal Linux - Newbie 4 03-12-2008 02:57 AM
how to read text file using bash script kkpal Linux - Newbie 2 03-03-2008 12:40 PM
Read variables from a text file in Bash jakev383 Linux - General 5 12-20-2006 08:29 AM


All times are GMT -5. The time now is 04:57 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration