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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
|
07-16-2012, 05:24 PM
|
#1
|
LQ Newbie
Registered: Jul 2012
Posts: 4
Rep:
|
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
|
|
|
07-16-2012, 05:28 PM
|
#2
|
LQ Guru
Registered: Apr 2005
Location: /dev/null
Posts: 5,818
|
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?
|
|
|
07-16-2012, 05:42 PM
|
#3
|
LQ Newbie
Registered: Jul 2012
Posts: 4
Original Poster
Rep:
|
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.
|
|
|
07-16-2012, 06:24 PM
|
#4
|
Member
Registered: Aug 2008
Location: INDIA
Distribution: Redhat,Debian,Suse,Windows
Posts: 179
Rep:
|
Quote:
Originally Posted by adrianadonis
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
Last edited by sharadchhetri; 07-16-2012 at 06:25 PM.
|
|
|
07-16-2012, 09:13 PM
|
#5
|
LQ Guru
Registered: Sep 2009
Location: Perth
Distribution: Arch
Posts: 10,028
|
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.
|
|
|
07-16-2012, 09:25 PM
|
#6
|
LQ Guru
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,414
|
|
|
|
07-17-2012, 02:10 PM
|
#7
|
LQ Newbie
Registered: Jul 2012
Posts: 4
Original Poster
Rep:
|
Quote:
Originally Posted by grail
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.
|
|
|
07-17-2012, 05:10 PM
|
#8
|
Member
Registered: Aug 2008
Location: INDIA
Distribution: Redhat,Debian,Suse,Windows
Posts: 179
Rep:
|
Quote:
Originally Posted by adrianadonis
the echo is just an example the actual command is a command executed by another program.
|
the eg. I gave is it useful
|
|
|
07-18-2012, 03:42 AM
|
#9
|
LQ Guru
Registered: Sep 2009
Location: Perth
Distribution: Arch
Posts: 10,028
|
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
|
|
|
07-20-2012, 05:12 PM
|
#10
|
LQ Newbie
Registered: Jul 2012
Posts: 4
Original Poster
Rep:
|
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
|
|
|
All times are GMT -5. The time now is 03:56 PM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|