ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
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.
Hi, trying to read data from a file with several blocks in it. Each block corresponds to a particular day and blocks are separated by empty lines. I managed to write a small and very simple bash script that selects the relevant lines and outputs them to separate files for each day.
Here is the script:
Code:
#! /bin/bash
DESCRIPTION='Date Time Ref avgU10 gstU10 dirU10 tC10 RH10 avgU4 avgU2 avgU1 tC0_5 avgU0_5 gstU0_5'
while read LINE
do
FILEID=${LINE:0:6}
if [ ${LINE:0:1} == 9 ]; then
if [ ! -f ${FILEID}.txt ]; then # if file does not exist, add description header
echo ${DESCRIPTION} >> ${FILEID}.txt
echo ${LINE} >> ${FILEID}.txt
else
echo ${LINE} >> ${FILEID}.txt
fi
fi
done
It works simply by redirecting the data file into the script:
./script < datafile
I would like to modify it so I can specify the name of the data file to be processed within the script. Tried a bunch of different things I found on the net but nothing worked. Anybody knows a good way of doing it?
You might check the Bash Guide for Beginners and then the Advanced Bash Scripting Guide, here. A very nice guide about linux command line and shell scripting is here.
Whilst the current example works the input file would always need to be of the same name and in the current directory.
In the sites suggested I would look for command line arguments / parameters so you could then use a complete path to any file.
Whilst the current example works the input file would always need to be of the same name and in the current directory.
In the sites suggested I would look for command line arguments / parameters so you could then use a complete path to any file.
Here is a further evolution of the script with the input file passed by the -i flag. It also checks for illegal flags and runs anyway as long as the input file is set and exists (although it still does not differentiate between these cases).
Still does not have error checking within the data being read but it's coming...
Code:
#! /bin/bash
# This script reads a NordlysStasjonen data acquisition file with data for several days
# and splits it into a series of daily measurement files 22/11/11
display_usage() {
echo '####'
echo 'USAGE: extractDays -i inputfile'
echo 'where inputfile is the global NordlysStasjonen data acquisition file'
echo '####'
echo ''
}
while getopts :i: option # flag construct with leading : for illegal flag detection
do
case "${option}"
in
i) INFILE=${OPTARG};;
?) HUH=${OPTARG};;
esac
done
if [[ -n "${INFILE:+x}" && -f ${INFILE} ]]; then # Fails if INFILE is unset or empty or does not exist
echo -e '\n\t\tVariable INFILE has been set, and input file exists.'
else
echo -e '\n\t\tVariable INFILE is unset or empty, or input file does not exist.'
echo -e '\t\tExiting!!!'
display_usage
exit 1
fi
if [ -n "${HUH+x}" ]; then # Fails if HUH is unset
echo -e '\tIllegal use of additional flag'
echo -e '\t\tWill ignore illegal flag and run script since INFILE has been correctly set!\n'
display_usage
fi
# Initialize output file description header
DESCRIPTION='Date Time Ref avgU10 gstU10 dirU10 tC10 RH10 avgU4 avgU2 avgU1 tC0_5 avgU0_5 gstU0_5'
while read LINE
do
FILEID=${LINE:0:6}
if [ ${LINE:0:1} == 9 ]; then
if [ ! -f ${FILEID}.txt ]; then # if file does not exist, add description header
echo ${DESCRIPTION} >> ${FILEID}.txt
echo ${LINE} >> ${FILEID}.txt
else
echo ${LINE} >> ${FILEID}.txt
fi
fi
done < ${INFILE}
echo 'Script execution complete. Exiting...'
exit 0
if [[ -n "${INFILE:+x}" && -f ${INFILE} ]]; then # Fails if INFILE is unset or empty or does not exist
Not sure I understand your comment here? Both -n and -f seem to work fine for me whether INFILE is set or not?
Code:
if [ -n "${HUH+x}" ]; then # Fails if HUH is unset
echo -e '\tIllegal use of additional flag'
echo -e '\t\tWill ignore illegal flag and run script since INFILE has been correctly set!\n'
display_usage
fi
The red highlighted section would seem to be a guess? Just because 'HUH' has been set does not mean 'INFILE' has been set
if [[ -n "${INFILE:+x}" && -f ${INFILE} ]]; then # Fails if INFILE is unset or empty or does not exist
Not sure I understand your comment here? Both -n and -f seem to work fine for me whether INFILE is set or not?
This one checks whether INFILE is set, non-empty and the corresponding file exists. All conditions have to be present and it does fail if any one or more conditions is not met.
Quote:
Originally Posted by grail
Code:
if [ -n "${HUH+x}" ]; then # Fails if HUH is unset
echo -e '\tIllegal use of additional flag'
echo -e '\t\tWill ignore illegal flag and run script since INFILE has been correctly set!\n'
display_usage
fi
The red highlighted section would seem to be a guess? Just because 'HUH' has been set does not mean 'INFILE' has been set
True, but if we made it so far into the script then according to the previous check INFILE has been indeed properly set.
I tested the script for all possible situations and it works as intended. Or maybe I misunderstood your comments?
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.