LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Converting Batch Script to Shell Script (https://www.linuxquestions.org/questions/linux-newbie-8/converting-batch-script-to-shell-script-4175481161/)

sandeep.gcet@gmail.com 10-17-2013 08:31 AM

Converting Batch Script to Shell Script
 
Hi

I have a batch script :
FOR /f "eol=; tokens=1,2,3,4 delims=, " %%i in (var.txt) do ... command

This reads the data file var.txt

How do i covert it into shell script.

Thanks
Sandeep

TenTenths 10-17-2013 08:32 AM

Put it in a file, make the first line:

Code:

#!/bin/bash
and then chmod it 755

sandeep.gcet@gmail.com 10-17-2013 08:35 AM

Hi

i need to know how to convert FOR /f "eol=; tokens=1,2,3,4 delims=, " %%i in (var.txt) in a shell script.

TenTenths 10-17-2013 08:38 AM

So what have you done so far?

sandeep.gcet@gmail.com 10-17-2013 08:48 AM

Hi

for VARIABLE in var.txt
do
.. Commands..
done

But i need to read the file var.taxt

i.e

nextyear 2012
thisyear 2011

i need to read each line of this file and use "nextyear" and 2012 as $1 and $2 in first loop. In second loop $1 and $2 must take thisyear and 2011. I am confused if the read function can do this.

sandeep.gcet@gmail.com 10-17-2013 09:18 AM

Hi

i tried using awk, but this doesn't seems to work ..

#!/bin/bash

awk '
BEGIN { FS = "|"

}
{
essmsh set variable $1 $2
#my commands to update the variable for nextyra as 2013 here

}
' var.txt

grail 10-17-2013 10:35 AM

Well you firstly need to decide on what you would like to use, ie either bash or awk can easily do what you want, but to use both for something so trivial is a waste.

awk by default will read a file line by line and divide the contents based on FS which by default is any contiguous white space

bash on the other hand you would need to use a loop (suggest while) and then the read command to extract data a line at a time. Then it will depend on how well you know the data
as to whether or not you read the line into a single variable to be dissected later or split based on the IFS value into multiple variables or lastly into an array

Below are some links that may help:

http://tldp.org/LDP/abs/html/
http://mywiki.wooledge.org/TitleIndex
http://www.gnu.org/software/gawk/man...ode/index.html

TB0ne 10-17-2013 10:35 AM

Quote:

Originally Posted by sandeep.gcet@gmail.com (Post 5047488)
Hi

i tried using awk, but this doesn't seems to work ..

Ok...and have you checked ANY of the easily-found bash scripting tutorials?? There's even one in my posting signature, and the Advanced Bash Scripting Guide has MANY examples which you can use.

sandeep.gcet@gmail.com 10-17-2013 10:51 AM

Hi

I tried to assign the variable

#!/bin/bash

awk '
BEGIN { FS = "|"

}
{
var1=$1
value1=$2
var2=$3
Value2=$4
}
' var.txt
where var.txt looks like

thisyear 2013
nextyear 2014

but i want to use only $1 and $2 and loop in for every line in the file. Any suggestions. can for loop be used with awk

TB0ne 10-17-2013 11:02 AM

Quote:

Originally Posted by sandeep.gcet@gmail.com (Post 5047535)
Hi
I tried to assign the variable

but i want to use only $1 and $2 and loop in for every line in the file. Any suggestions. can for loop be used with awk

Right..you tried awk before, and it didn't work...why would it work the second time you tried it?? And we did suggest that you read the bash scripting tutorials, and use the EXAMPLES IN THEM. Did you?

grail 10-17-2013 11:36 AM

You may wish to read my first post again ... there is a key part about awk that you are missing.


All times are GMT -5. The time now is 09:59 PM.