Welcome to Linux Forums! With a comprehensive Linux Forum, information on various types of Linux software and many Linux Reviews articles, we have all the knowledge you need a click away, or accessible via our knowledgeable members.
Find the answer to your Linux question:
New to Linux Forums? Register here for free!
    Linux Forums > GNU Linux Zone > Linux Programming & Scripting > Need help converting .bat to an Bash script

Forgot Password?
 Linux Programming & Scripting   C, Perl, PHP, Bash Scripts, anything programming or script related post in here!

Site Navigation
Linux Articles
Linux Forums
Linux Downloads
Linux Hosting
Free Magazines
Job Board
IRC Chat
RSS Feeds


Linux Forum Topics
Linux Forums
Your Distro
Linux Resources
GNU Linux Zone
The Community
Reply
 
Thread Tools Display Modes
Old 12-02-2008   #1 (permalink)
Just Joined!
 
Join Date: Dec 2008
Posts: 3
Need help converting .bat to an Bash script

Hi all

Im new here,

so to my problem i have create a functional bat file
but i need to make it into a .sh file so it can run yum and other programs as it is in installer script for a full server setup

can some one tell me how i create a function and use vars
as its not as simple %var%

my code that i need to convert
Code:
@echo off
:mainmenu
CLS
echo  ##############################################################################
echo  ##############################################################################
echo  ##                        Welcome to PhdPanel Installer                     ##
echo  ##                          1. Install Recomended                           ##
echo  ##                          2. Install No MySQL                             ##
echo  ##                          3. Install No DNS                               ##
echo  ##                          4. Custom Install                               ##
echo  ##                          5. Read Me File                                 ##
echo  ##                          6. Terms                                        ##
echo  ##############################################################################
echo  ############################################################################## 
GOTO inputloop  

:inputloop
echo  Please use Options above
SET /p choice=
IF %choice% == 1 GOTO Option1
IF %choice% == 2 GOTO Option2
IF %choice% == 3 GOTO Option3
IF %choice% == 4 GOTO Option4
IF %choice% == 5 GOTO Option5
IF %choice% == 6 GOTO Option6
IF %choice% == EXIT EXIT
GOTO mainmenu
@pause

:Option1 
echo Sorry Installer Not Created yet
echo Installer Begining 
echo yum install -y httpd php apache mysql php-mysql mysql-admin mysql-server vsftp name caching-nameservers sendmail
@pause
EXIT

:Option2 
echo Sorry Installer Not Created yet
echo Installer Begining 
echo yum install -y httpd php apache php vsftp name caching-nameservers sendmail
@pause
EXIT

:Option3
echo Sorry Installer Not Created yet
echo Installer Begining 
echo yum install -y httpd php apache php-mysql vsftp sendmail mysql php-mysql mysql-admin mysql-server
@pause
EXIT

:Option4
GOTO startcustom
EXIT

:Option5
CLS
echo  ##############################################################################
echo  ##############################################################################
echo  ##           This is the Readmore Section Copyright of martin barker        ##
echo  ##                           and Richarch Dickinson                         ##
echo  ##############################################################################
echo  ##############################################################################
@pause
GOTO mainmenu

:Option6
CLS
echo  ##############################################################################
echo  ##############################################################################
echo  ##                         The Terms and conditions                         ##
echo  ##############################################################################
echo  ##############################################################################
@pause
GOTO mainmenu

:startcustom
SET co1=on
SET co2=on
SET co3=on
SET co4=on
SET co5=on
SET co6=on
SET co7=on
GOTO customdraw

:switch1
IF %co1% == on GOTO sw1n
IF %co1% == off GOTO sw1f

:sw1n
SET co1=off
GOTO customdraw

:sw1f
SET co1=on
GOTO customdraw

:switch2
IF %co2% == on GOTO sw2n
IF %co2% == off GOTO sw2f

:sw2n
SET co2=off
GOTO customdraw

:sw2f
SET co2=on
GOTO customdraw

:switch3
IF %co3% == on GOTO sw3n
IF %co3% == off GOTO sw3f

:sw3n
SET co3=off
GOTO customdraw

:sw3f
SET co3=on
GOTO customdraw

:switch4
IF %co4% == on GOTO sw4n
IF %co4% == off GOTO sw4f

:sw4n
SET co4=off
GOTO customdraw

:sw4f
SET co4=on
GOTO customdraw

:switch5
IF %co5% == on GOTO sw5n
IF %co5% == off GOTO sw5f

:sw5n
SET co5=off
GOTO customdraw

:sw5f
SET co5=on
GOTO customdraw

:switch6
IF %co6% == on GOTO sw6n
IF %co6% == off GOTO sw6f

:sw6n
SET co6=off
GOTO customdraw

:sw6f
SET co6=on
GOTO customdraw

:switch7
IF %co7% == on GOTO sw7n
IF %co7% == off GOTO sw7f

:sw7n
SET co7=off
GOTO customdraw

:sw7f
SET co7=on
GOTO customdraw

:custominstall
echo Sorry Installer Not Created yet
echo Installer Begining 
echo yum install -y 
@pause
EXIT

:customdraw
CLS
set Cchoice=Install
echo  ##############################################################################
echo  ##############################################################################
echo  ##                   Welcome to PhdPanel Custom Installer                   ##
IF %co1% == on echo  ##                     1. Apache                   [%co1%]                     ##
IF %co1% == off echo  ##                     1. Apache                   [%co1%]                    ##
IF %co2% == on echo  ##                     2. Httpd                    [%co2%]                     ##
IF %co2% == off echo  ##                     2. Httpd                    [%co2%]                    ##
IF %co3% == on echo  ##                     3. php-mysql                [%co3%]                     ##
IF %co3% == off echo  ##                     3. php-mysql                [%co3%]                    ##
IF %co4% == on echo  ##                     4. php(no SQL)              [%co4%]                     ##
IF %co4% == off echo  ##                     4. php(no SQL)              [%co4%]                    ##
IF %co5% == on echo  ##                     5. ftp                      [%co5%]                     ##
IF %co5% == off echo  ##                     5. ftp                      [%co5%]                    ##
IF %co6% == on echo  ##                     6. DNS                      [%co6%]                     ##
IF %co6% == off echo  ##                     6. DNS                      [%co6%]                    ##
echo  ##                                                                          ##
echo  ##                       Press Return to Install                            ##
echo  ##############################################################################
echo  ############################################################################## 
echo Please use Options above
SET /p Cchoice=
IF %Cchoice% == 1 GOTO switch1
IF %Cchoice% == 2 GOTO switch2
IF %Cchoice% == 3 GOTO switch3
IF %Cchoice% == 4 GOTO switch4
IF %Cchoice% == 5 GOTO switch5
IF %Cchoice% == 6 GOTO switch6
IF %Cchoice% == 7 GOTO switch7
IF %Cchoice% == Install GOTO custominstall
IF %Cchoice% == EXIT EXIT
GOTO customdraw
barkermn01 is offline  


Reply With Quote
Old 12-02-2008   #2 (permalink)
/etc/init.d/moderator
 
bigtomrodney's Avatar
 
Join Date: Nov 2004
Location: Sunny South-East of Ireland
Posts: 6,031
Well here are a few to get you started...

Variables are preceded with a dollar ,e.g. $choice

You can read in a variable with the read command
Code:
read choice
. Labels aren't generally used, rather functions. Others like if tests are phrased slightly differently.

Basically, bash is very like dos/batch scripts but with a little more fluency. That is you can make your script as simple as you wish but with the wealth of options available you can often find that an entire section of a batch script is unnecessary as there is one command or function that will cover it all off. With the fluency you have shown above I doubt it will be a difficult switch.

I recommend checking out The Advanced Bash Scripting Guide. It's all free and online and a will take you from the ground up very quickly...it certainly did for me.

Do come back and ask any questions as you go with your conversion. I'll give you one clue though:rather than labelling and using if tests, consider the case command
__________________
Registered Linux user #378740
New members read here / Forum Rules
#linuxforums on irc.freenode.net
bigtomrodney is offline   Reply With Quote
Old 12-02-2008   #3 (permalink)
Just Joined!
 
Join Date: Dec 2008
Posts: 3
I code in php and do it quite well by the looks of things bash is very C oriented just like php is this correct?

Also how do i do if statements? in bash
one las thing whats the bash CSL
barkermn01 is offline   Reply With Quote
Old 12-02-2008   #4 (permalink)
/etc/init.d/moderator
 
bigtomrodney's Avatar
 
Join Date: Nov 2004
Location: Sunny South-East of Ireland
Posts: 6,031
That guide goes into it in more detail, but essentially you finish every if test with fi. Similarly with case, you use esac. There are also more tests available with if.

I haven't done a lot of C so I can't really say. There is also the C shell (csh) but I hear that csh scripts are a bad idea. You know I've never had to use the clear screen command I'm not sure to be honest. Interactively I use control L but I'm not sure how you would script it.
__________________
Registered Linux user #378740
New members read here / Forum Rules
#linuxforums on irc.freenode.net
bigtomrodney is offline   Reply With Quote
Old 12-02-2008   #5 (permalink)
Just Joined!
 
Join Date: Dec 2008
Posts: 3
Quote:
Originally Posted by bigtomrodney View Post
That guide goes into it in more detail, but essentially you finish every if test with fi. Similarly with case, you use esac. There are also more tests available with if.

I haven't done a lot of C so I can't really say. There is also the C shell (csh) but I hear that csh scripts are a bad idea. You know I've never had to use the clear screen command I'm not sure to be honest. Interactively I use control L but I'm not sure how you would script it.
I think i got the if statments wrong but i think the rest of it is correct can you have a look please?

If that dose not work i might cheat and just get a .sh to install php and run a php script lol

Code:
#!/bin/bash
 
function mainmenu{
Clear
echo  "##############################################################################"
echo  "##############################################################################"
echo  "##                        Welcome to PhdPanel Installer                     ##"
echo  "##                          1. Install Recomended                           ##"
echo  "##                          2. Install No MySQL                             ##"
echo  "##                          3. Install No DNS                               ##"
echo  "##                          4. Custom Install                               ##"
echo  "##                          5. Read Me File                                 ##"
echo  "##                          6. Terms                                        ##"
echo  "##############################################################################"
echo  "##############################################################################"
inputloop()
}

function inputloop{
echo  Please use Options above
export /p choice=
IF $choice == 1  Option1()
IF $choice == 2  Option2()
IF $choice == 3  Option3()
IF $choice == 4  Option4()
IF $choice == 5  Option5()
IF $choice == 6  Option6()
mainmenu()
}

function Option1{
echo "Sorry Installer Not Created yet"
echo "Installer Begining "
echo "yum install -y httpd php apache mysql php-mysql mysql-admin mysql-server vsftp name caching-nameservers sendmail"
}

function Option2{ 
echo "Sorry Installer Not Created yet"
echo "Installer Begining "
echo "yum install -y httpd php apache php vsftp name caching-nameservers sendmail"
}

function Option3{
echo "Sorry Installer Not Created yet"
echo "Installer Begining "
echo "yum install -y httpd php apache php-mysql vsftp sendmail mysql php-mysql mysql-admin mysql-server"
}

function Option4{
 startcustom()
}

function Option5{
clear
echo  "##############################################################################"
echo  "##############################################################################"
echo  "##           This is the Readmore Section Copyright of martin barker        ##"
echo  "##                           and Richarch Dickinson                         ##"
echo  "##############################################################################"
echo  "##############################################################################"
mainmenu()
}

function Option6{
clear
echo  "##############################################################################"
echo  "##############################################################################"
echo  "##                         The Terms and conditions                         ##"
echo  "##############################################################################"
echo  "##############################################################################"
mainmenu()
}

function startcustom{
export $co1=on
export $co2=on
export $co3=on
export $co4=on
export $co5=on
export $co6=on
export $co7=on
customdraw()
}

function switch1{
IF [$co1 = on]  sw1n()
IF [$co1 = off]  sw1f()
}

function sw1n{
export co1=off
customdraw()
}

function sw1f{
export co1=on
customdraw()
}

function switch2{
IF [$co2 == on]  sw2n()
IF [$co2 == off]  sw2f()
}

function sw2n{
export co2=off
customdraw()
}

function sw2f{
export co2=on
customdraw()
}

function switch3{
IF [$co3 == on]  sw3n
IF [$co3 == off]  sw3f
}

function sw3n{
export co3=off
customdraw()
}

function sw3f{
export co3=on
customdraw()
}

function switch4{
IF [$co4 == on]  sw4n()
IF [$co4 == off]  sw4f()
}

function sw4n{
export co4=off
customdraw()
}

function sw4f{
export co4=on
customdraw()
}

function switch5{
IF [$co5 == on]  sw5n()
IF [$co5 == off]  sw5f()
}

function sw5n{
export co5=off
customdraw()
}

function sw5f{
export co5=on
customdraw()
}

function switch6{
IF [$co6 == on]  sw6n()
IF [$co6 == off]  sw6f()
}

function sw6n{
export co6=off
customdraw()
}

function sw6f{
export co6=on
customdraw()
}

function switch7{
IF [$co7 == on]  sw7n()
IF [$co7 == off]  sw7f()
}

function sw7n{
export co7=off
customdraw()
}

function sw7f{
export co7=on
customdraw()
}

function custominstall{
echo "Sorry Installer Not Created yet"
echo "Installer Begining" 
echo "yum install -y" 
}

function customdraw(){
clear
export Cchoice=Install
echo  "##############################################################################"
echo  "##############################################################################"
echo  "##                   Welcome to PhdPanel Custom Installer                   ##"
IF [$co1 == on] echo  "##                     1. Apache                   [$co1]                     ##"
IF [$co1 == off] echo  "##                     1. Apache                   [$co1]                    ##"
IF [$co2 == on] echo  "##                     2. Httpd                    [$co2]                     ##"
IF [$co2 == off] echo  "##                     2. Httpd                    [$co2]                    ##"
IF [$co3 == on] echo  "##                     3. php-mysql                [$co3]                     ##"
IF [$co3 == off] echo  "##                     3. php-mysql                [$co3]                    ##"
IF [$co4 == on] echo  "##                     4. php(no SQL)              [$co4]                     ##"
IF [$co4 == off] echo  "##                     4. php(no SQL)              [$co4]                    ##"
IF [$co5 == on] echo  "##                     5. ftp                      [$co5]                     ##"
IF [$co5 == off] echo  "##                     5. ftp                      [$co5]                    ##"
IF [$co6 == on] echo  "##                     6. DNS                      [$co6]                     ##"
IF [$co6 == off] echo  "##                     6. DNS                      [$co6]                    ##"
echo  "##                                                                          ##"
echo  "##                       Press Return to Install                            ##"
echo  "##############################################################################"
echo  "############################################################################## "
echo "Please use Options above"
export /p Cchoice=
IF [$Cchoice == 1]  switch1()
IF [$Cchoice == 2]  switch2()
IF [$Cchoice == 3]  switch3()
IF [$Cchoice == 4]  switch4()
IF [$Cchoice == 5]  switch5()
IF [$Cchoice == 6]  switch6()
IF [$Cchoice == 7]  switch7()
IF [$Cchoice == Install]  custominstall()
customdraw()
}
mainmenu()
barkermn01 is offline   Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

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 On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off

Free Magazines
Run Your Own Web Server Using Linux & Apache - Free 191 Page Preview
Learn about everything you'll need to build and maintain your Linux servers, and to deploy Web applications to them.
subscribe
Open Source Security Myths Dispelled
Dispel the five major myths surrounding Open Source Security and gain the tools necessary to make a truly informed decision for your IT organization
subscribe
InformationWeek
InformationWeek is the only newsweekly you'll need to stay on top of the latest developments in information technology.
subscribe



All times are GMT. The time now is 07:16 AM.






© 2000 - 2009 - All Rights Reserved - Property of  MAS Media

Content Relevant URLs by vBSEO 3.3.0 RC2