ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I've been using python/bash to help myself automate things for a long time, however I am curious. One reason I use python more is because I don't quite know how to work with user input in Bash, such as getting it to ask for input and wait, and then placing that user input into a command. Here's an example of a script I use from time to time for converting OGG video files into AVI. How could I write this same script in Bash? Keep in mind I am mostly self taught, so I am by no means an expert programmer.
Code:
#This script automates the process of converting an OGG video file
#to an AVI format file. This script requires that you have mencoder
#installed.
import os
print ""
print "Welcome to your OGG Video Converter!"
print ""
print "This will convert OGG videos into AVI format."
print ""
def main():
print "Please enter the filename of the video you wish"
print "to convert, remember to include quotes around"
print "the filename if it contains spaces."
print ""
infile=raw_input("Enter filename: ")
print ""
print "The converted video will be placed on your desktop."
print ""
newname=raw_input("What would you like to name the converted video? ")
print ""
print "The file %s is about to be converted to %s.avi."%(infile, newname)
print ""
raw_input("Press Enter to begin the conversion...")
print ""
os.system(('mencoder %s -o $HOME/Desktop/"%s".avi -oac mp3lame -ovc lavc')%(infile, newname))
print ""
raw_input("Conversion complete, press Enter to exit...")
def firstrun():
if os.name!='posix':
print "This program will only run in a Unix-based operating system"
print "with the program 'mencoder' installed."
raw_input("Press Enter to exit...")
else:
main()
firstrun()
User input is retrieved from the read built-in. Use read -p if you want to prompt a message to the user, for example:
Code:
read -p "Please enter the filename of the video you wish
to convert, remember to include quotes around
the filename if it contains spaces.
Enter filename: " infile
I've been using python/bash to help myself automate things for a long time, however I am curious. One reason I use python more is because I don't quite know how to work with user input in Bash,
that should not even be one of the reason why you choose Python over bash.
by the way, to print multiline
Code:
thestring = """
this is a
multi line statement
the end
"""
also, try to use subprocess module for calling external commands like mencoder
Last edited by ghostdog74; 06-03-2009 at 07:44 PM.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.