LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-21-2011, 12:44 AM   #1
newtounix
LQ Newbie
 
Registered: Jul 2011
Posts: 1

Rep: Reputation: Disabled
Converting a .bat script to sh script to work in linux server


Hi everyone,

I am new to scripting world and i have been assigned the task of converting some bat scripts to sh scripts because we migrate from Windows to Linux server.I would be really really thankful if you could convert this bat script to sh script.

Please find below the .bat script.

rem *************************************************************************************
rem This script is used to get r2devices files.
rem
*************************************************************************************
ECHO OFF
E:
dir >>r2_test.txt
cd \Informatica\infa_shared\SrcFiles\IT_REPORTING\RISH
date/T>r2devices.log.
time/T>>r2devices.log.
del r2devices_delta.csv
SET CURRDATE=%DATE%

SET PARSEARG="eol=; tokens=1,2,3,4* delims=/,. "
FOR /F %PARSEARG% %%h IN ("%CURRDATE%") DO (
SET GYear=%%k
SET GDay=%%j
SET GMonth=%%i
SET GWeekDay=%%h
)
CALL :JDate %GYear% %GMonth% %GDay%
SET /A NewJDate = %JDate% - 1
CALL :GDate %NewJDate%
echo r2devices.%GYear%.%GMonth%.%GDay%.csv to download >>r2devices.log.
echo open edm01 >get_r2devices.ftp
echo user rca Pu2 >>get_r2devices.ftp
echo ascii >>get_r2devices.ftp
echo lcd >>get_r2devices.ftp
echo prompt >>get_r2devices.ftp
echo pwd >>get_r2devices.ftp
echo ls -l >>get_r2devices.ftp
echo get r2devices.%GYear%.%GMonth%.%GDay%.csv >>get_r2devices.ftp
echo bye >>get_r2devices.ftp
ftp -n -s:get_r2devices.ftp >> r2devices.log
ren r2devices.%GYear%.%GMonth%.%GDay%.csv r2devices_delta.csv >> r2devices.log
del *.ftp
exit 0

::===================================::
:: ::
:: - S u b r o u t i n e s - ::
:: ::
::===================================::


:GDate
:: Convert Julian date back to "normal" Gregorian date
:: Argument : Julian date
:: Returns : YYYY MM DD

::
SET /A P = %1 + 68569
SET /A Q = 4 * %P% / 146097
SET /A R = %P% - ( 146097 * %Q% +3 ) / 4
SET /A S = 4000 * ( %R% + 1 ) / 1461001
SET /A T = %R% - 1461 * %S% / 4 + 31
SET /A U = 80 * %T% / 2447
SET /A V = %U% / 11
SET /A GYear = 100 * ( %Q% - 49 ) + %S% + %V%
SET /A GMonth = %U% + 2 - 12 * %V%
SET /A GDay = %T% - 2447 * %U% / 80
:: Clean up the mess
FOR %%A IN (P Q R S T U V) DO SET %%A=
:: Add leading zeroes
IF 1%GMonth% LSS 20 SET GMonth=0%GMonth%
IF 1%GDay% LSS 20 SET GDay=0%GDay%
:: Return value
SET GDate=%GYear% %GMonth% %GDay%
GOTO:EOF

:JDate
:: Convert date to Julian
:: Arguments : YYYY MM DD
:: Returns : Julian date
::
:: First strip leading zeroes
SET MM=%2
SET DD=%3
IF %MM:~0,1% EQU 0 SET MM=%MM:~1%
IF %DD:~0,1% EQU 0 SET DD=%DD:~1%
::

SET /A Month1 = ( %MM% - 14 ) / 12
SET /A Year1 = %1 + 4800
SET /A JDate = 1461 * ( %Year1% + %Month1% ) / 4 + 367 * ( %MM% - 2 -12 * %Month1% ) / 12 - ( 3 * ( ( %Year1% + %Month1% + 100 ) / 100 ) ) / 4 + %DD% - 32075
FOR %%A IN (Month1 Year1) DO SET %%A=
GOTO:EOF
 
Old 07-21-2011, 01:24 AM   #2
EricTRA
LQ Guru
 
Registered: May 2009
Location: Gibraltar, Gibraltar
Distribution: Fedora 20 with Awesome WM
Posts: 6,805
Blog Entries: 1

Rep: Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297
Hello and Welcome to LinuxQuestions,

The task was assigned to YOU, right? So why do you expect us to do the work for you? There's no problem whatsoever if you ask for help when you cannot find a solution on your own and when you ask we'll point you in the right direction. But I don't believe that just giving you the solution helps you in learning Bash scripting. Here are some pointers to good documentation you can read to get you started with scripting in Linux.

Bash Guide for Beginners
Advanced Bash Scripting Guide

One thing: have a look at each line of your bat file and look for the equivalent command in Linux, then go through the man page for that command and try it out in a console before putting everything together in a script. Furthermore, drive letters like C: and E: don't exist in Linux so you'll need to get another approach. And to declare a variable you just declare it with the name, no use for the set command, like this:
Code:
VARIABLE="VALUE"
Looking forward to your participation in the forums. Have fun with Linux.

Kind regards,

Eric
 
Old 07-21-2011, 07:23 AM   #3
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
http://linuxcommand.org/learning_the_shell.php

http://mywiki.wooledge.org/BashPitfalls
 
Old 07-21-2011, 07:30 AM   #4
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
The Advanced Bash Scripting Guide even has a page specifically on converting batch files to scripts:

http://tldp.org/LDP/abs/html/dosbatch.html

Also, please use [code][/code] tags around your code, to preserve formatting and to improve readability.

Last edited by David the H.; 07-21-2011 at 07:31 AM.
 
  


Reply



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 Aquarius_Girl Programming 4 01-29-2010 12:27 AM
How to execute a ssh script on Linux server from Windows through a bat script? wanna13e Programming 13 10-23-2009 02:41 AM
Converting a Windows Perl script to a Linux Perl script. rubbercash Programming 2 07-19-2004 10:22 AM
Remote Win Bat File execute Shell Script on AIX Server DriveMeCrazy AIX 5 05-26-2004 06:24 PM
converting shell script to php script ? ibro Linux - General 6 05-24-2004 05:19 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:42 PM.

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
Open Source Consulting | Domain Registration