LinuxQuestions.org
Visit Jeremy's Blog.
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


Closed Thread
  Search this Thread
Old 10-26-2017, 08:05 AM   #1
Linux_Fan9
LQ Newbie
 
Registered: Oct 2017
Location: Athens, MI
Posts: 8

Rep: Reputation: 0
Is It possible to convert a python script to a bash script?


I want to convert this script to bash but I don't know if I can.
This script is in python because I am on https://www.codecademy.com learning how to write in python. But I need this script for the game battleship in bash for a script I am writing in the program Geany.
I just started to learn how to write a script so please go easy on me.
I have tried
source /home/administrator/Desktop/Battleship.sh
bin/bash /home/administrator/Desktop/Battleship.sh
I did a Mad Lib and a script exactly like the way mentioned above.
If anyone is willing to help me (not just do it for me) that would be appreciated.

from random import randint

board = []

for x in range(0, 5):
board.append(["O"] * 5)

def print_board(board):
for row in board:
print " ".join(row)

print_board(board)

def random_row(board):
return randint(0, len(board) - 1)

def random_col(board):
return randint(0, len(board[0]) - 1)

ship_row = random_row(board)
ship_col = random_col(board)
print ship_row
print ship_col

for turn in range(4):
print "Turn", turn + 1
guess_row = int(raw_input("Guess Row: "))
guess_col = int(raw_input("Guess Col: "))

if guess_row == ship_row and guess_col == ship_col:
print "Congratulations! You sank my battleship!"
break
else:
if guess_row not in range(5) or \
guess_col not in range(5):
print "Oops, that's not even in the ocean."
elif board[guess_row][guess_col] == "X":
print( "You guessed that one already." )
else:
print "You missed my battleship!"
board[guess_row][guess_col] = "X"
if (turn == 3):
print "Game Over"
print_board(board)

Last edited by Linux_Fan9; 10-26-2017 at 08:06 AM.
 
Old 10-26-2017, 08:21 AM   #2
jlinkels
LQ Guru
 
Registered: Oct 2003
Location: Bonaire, Leeuwarden
Distribution: Debian /Jessie/Stretch/Sid, Linux Mint DE
Posts: 5,196

Rep: Reputation: 1044Reputation: 1044Reputation: 1044Reputation: 1044Reputation: 1044Reputation: 1044Reputation: 1044Reputation: 1044
You asked exactly this question yesterday as well. You received answers. So what is the purpose to asking the same question again? As far as I can see the answers provided were useful enough. But the script did not get translated magically by posting it here. Is that your problem maybe?

jlinkels
 
Old 10-26-2017, 09:58 AM   #3
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 11,546
Blog Entries: 4

Rep: Reputation: 4257Reputation: 4257Reputation: 4257Reputation: 4257Reputation: 4257Reputation: 4257Reputation: 4257Reputation: 4257Reputation: 4257Reputation: 4257Reputation: 4257
Also: "you can write 'a shell script'" in Python, or in any other language, just by prefixing your script with a #!shebang line. The user will neither know nor care that the script was written using something other than Bash's (extremely primitive ...) scripting tool.

Even though "Bash scripting" has been used to do some pretty-astonishing stuff, in my view the exercise is completely pointless: "shebang" lets you write a script using "whatever real Image programming language" you wish!
 
Old 10-26-2017, 10:33 AM   #4
KenJackson
Member
 
Registered: Jul 2006
Location: Maryland, USA
Distribution: Fedora and others
Posts: 757

Rep: Reputation: 145Reputation: 145
Quote:
Originally Posted by sundialsvcs View Post
... Bash's (extremely primitive ...) scripting tool.

Even though "Bash scripting" has been used to do some pretty-astonishing stuff, in my view the exercise is completely pointless ...
Interesting viewpoint.

I figure there's a higher probability that Bash, or at least a Borne shell, will be installed on any system than any other tool. That's a point. (This is not true for Windows, but nothing works on Windows unless it's written specifically for it.)

There are many times I've set out to write a script in Python or Perl and realized I could just do in in Bash. Since I do more of the latter and am more comfortable in it, Bash tends to be my choice.
 
Old 10-26-2017, 02:11 PM   #5
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=15, FreeBSD_12{.0|.1}
Posts: 6,437
Blog Entries: 24

Rep: Reputation: 4397Reputation: 4397Reputation: 4397Reputation: 4397Reputation: 4397Reputation: 4397Reputation: 4397Reputation: 4397Reputation: 4397Reputation: 4397Reputation: 4397
Linux_Fan9, please post your question only once in the most relevant forum. This thread is a duplicate of this one and as such is being closed.

When others respond to your question with useful answers as they did in the previous thread, please acknowledge the time they have taken to read and answer your question by interacting with them.

Participation on LQ is a two-way channel, where you are expected to participate, not simply to receive pre-made code or answers on demand.

Also, as asked in the other thread, please place your code snippets inside [CODE]...[/CODE] tags for better readability. You may type those yourself or click the "#" button in the edit controls.

Please review the Site FAQ for guidance in acceptable participation in these fora.

This thread is now closed, please continue the discussion in the original thread.
 
  


Closed Thread



Image 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



Image Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Convert Bash Script to Ash Script notchy Programming 12 07-20-2016 12:40 PM
convert bash script to sh script? parsbin Programming 4 07-21-2012 10:26 PM
[SOLVED] calling bash script with more than one variable from python script mfb Programming 4 10-12-2010 07:51 AM
Convert python script to shell script investmentbnker75 Programming 13 09-05-2010 02:38 AM

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

All times are GMT -5. The time now is 03:04 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
Open Source Consulting | Domain Registration