Linux - NewbieThis 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.
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.
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
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?
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.
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.
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
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
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.