LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
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


Reply
  Search this Thread
Old 10-17-2013, 08:31 AM   #1
sandeep.gcet@gmail.com
LQ Newbie
 
Registered: Oct 2013
Posts: 5

Rep: Reputation: Disabled
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
 
Old 10-17-2013, 08:32 AM   #2
TenTenths
Senior Member
 
Registered: Aug 2011
Location: Dublin
Distribution: Centos 5 / 6 / 7
Posts: 2,380

Rep: Reputation: 849Reputation: 849Reputation: 849Reputation: 849Reputation: 849Reputation: 849Reputation: 849
Put it in a file, make the first line:

Code:
#!/bin/bash
and then chmod it 755
 
Old 10-17-2013, 08:35 AM   #3
sandeep.gcet@gmail.com
LQ Newbie
 
Registered: Oct 2013
Posts: 5

Original Poster
Rep: Reputation: Disabled
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.
 
Old 10-17-2013, 08:38 AM   #4
TenTenths
Senior Member
 
Registered: Aug 2011
Location: Dublin
Distribution: Centos 5 / 6 / 7
Posts: 2,380

Rep: Reputation: 849Reputation: 849Reputation: 849Reputation: 849Reputation: 849Reputation: 849Reputation: 849
So what have you done so far?
 
Old 10-17-2013, 08:48 AM   #5
sandeep.gcet@gmail.com
LQ Newbie
 
Registered: Oct 2013
Posts: 5

Original Poster
Rep: Reputation: Disabled
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.
 
Old 10-17-2013, 09:18 AM   #6
sandeep.gcet@gmail.com
LQ Newbie
 
Registered: Oct 2013
Posts: 5

Original Poster
Rep: Reputation: Disabled
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
 
Old 10-17-2013, 10:35 AM   #7
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,494

Rep: Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867
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
 
1 members found this post helpful.
Old 10-17-2013, 10:35 AM   #8
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 18,791

Rep: Reputation: 4162Reputation: 4162Reputation: 4162Reputation: 4162Reputation: 4162Reputation: 4162Reputation: 4162Reputation: 4162Reputation: 4162Reputation: 4162Reputation: 4162
Quote:
Originally Posted by sandeep.gcet@gmail.com View Post
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.
 
Old 10-17-2013, 10:51 AM   #9
sandeep.gcet@gmail.com
LQ Newbie
 
Registered: Oct 2013
Posts: 5

Original Poster
Rep: Reputation: Disabled
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
 
Old 10-17-2013, 11:02 AM   #10
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 18,791

Rep: Reputation: 4162Reputation: 4162Reputation: 4162Reputation: 4162Reputation: 4162Reputation: 4162Reputation: 4162Reputation: 4162Reputation: 4162Reputation: 4162Reputation: 4162
Quote:
Originally Posted by sandeep.gcet@gmail.com View Post
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?
 
Old 10-17-2013, 11:36 AM   #11
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,494

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


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Converting a Shell script to a Python script TheIndependentAquarius Programming 4 01-29-2010 12:27 AM
Change batch script to shell script alan.belizario Programming 5 03-31-2005 12:41 AM
Change batch script to shell script alan.belizario Linux - Software 1 03-30-2005 01:49 AM
Help w/ batch/shell script mikehlinuxquest Linux - General 5 09-13-2004 04:41 PM
converting shell script to php script ? ibro Linux - General 6 05-24-2004 05:19 AM


All times are GMT -5. The time now is 10:10 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration