Bash Scripting FOR loop with 2 variables

N

Neuls

Guest
Hi all,

I'm not pretty sure if this post is placed in the right forum..
I'd like to write a script which does the following idea:

#!/bin/bash

CODESEC=`cut -c 1-5 Data.txt`
CODEORD=`cut -c 7-21 Data.txt`

for i in ${CODESEC} AND j in ${CODEORD}
do

echo $i $j

done

I have googled this concept several times but i cant find a way which solves this problem, mainly because both variables are extracted from the same database .txt file and must be utilized at the same time

Thank you for yr help
 


Try this:
Save this as whatever you want to call it. I've just called it cutfile!
Code:
#!/bin/bash

if [ $# -eq 1 ] && [ -f $1 ]; then
  while read line
  do
    CODESEC=`echo ${line} | cut -c 1-5`
    CODEORD=`echo ${line} | cut -c 7-21`
    echo "${CODESEC} ${CODEORD}"
  done < ${1}
else
  echo "Usage: ${0} {filename}"
fi

Run it using:
./cutfile /path/to/data.txt

What it does:
- Checks that a single parameter was passed to the script and the parameter is a valid file
- If no parameter is passed, or the parameter is not a valid file - some usage information is displayed.
- Assuming we have a valid file, we keep reading lines from the file, extracting the information we require into variables which are then echoed to the screen.

Is that close to what you were trying to do?
 
Last edited:
Try this:
Save this as whatever you want to call it. I've just called it cutfile!
Code:
#!/bin/bash

if [ $# -eq 1 ] && [ -f $1 ]; then
  while read line
  do
    CODESEC=`echo ${line} | cut -c 1-5`
    CODEORD=`echo ${line} | cut -c 7-21`
    echo "${CODESEC} ${CODEORD}"
  done < ${1}
else
  echo "Usage: ${0} {filename}"
fi

Run it using:
./cutfile /path/to/data.txt

What it does:
- Checks that a single parameter was passed to the script and the parameter is a valid file
- If no parameter is passed, or the parameter is not a valid file - some usage information is displayed.
- Assuming we have a valid file, we keep reading lines from the file, extracting the information we require into variables which are then echoed to the screen.

Is that close to what you were trying to do?
Thank you for the fast answer Jas.
I'm reminding old knowledge about bash and I didnt remember about while read line loops, thank you! it solved my problem! :)
Although I dont get completly the if usage in this case.. why shouldnt each single parameter be pased to the script? It worked without. What does the variable $# mean?

Thank you,

Regards
 
I put the initial 'if' statement in there so you could run the script and pass it the file to extract data from.
You don't have to use it if you don't want to! I figured rather than hard-coding a file-name in my example script, you could just call the script and pass it any of your data files, so you could see if it worked or not! :)

$# is the number of arguments passed to the script.
So the line:
Code:
if [ $# -eq 1 ] && [ -f $1 ]; then
Means:
if [ the number of parameters passed to the script is exactly 1 ] AND [ the parameter is a path to a valid file ] ;
then we attempt to read lines from the file and extract data until we reach the end of the file.

And best of all - No need for multiple/nested loops. Once we have read a line of text from the file into $line, we can use as many calls to "echo $line | cut ... " as we need to in order to extract the data we require from the line. In this case we only need two. The result of the first echo/cut is stored in $CODESEC, the second is stored in $CODEORD. Simple!

Going back to the parameters:
If you want to be able to pass the script a list of files to operate on, you could always check the number of parameters ($#), and then use a loop to check that each parameter is a valid file. - If it is a file - process it using your "while read" loop. If it isn't - well, you could ignore that parameter, or display an error message, or whatever! :)

Again, I just tried to offer a simple, complete script as an example. Feel free to modify it to suit your needs.
 

Members online


Top