LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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 06-02-2009, 02:22 PM   #1
dudeman41465
Member
 
Registered: Jun 2005
Location: Kentucky
Distribution: Debian
Posts: 794

Rep: Reputation: 56
Working with User Input in Bash


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()
 
Old 06-02-2009, 02:38 PM   #2
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
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
 
Old 06-03-2009, 05:02 PM   #3
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
why all those print statements? it's awful.
don't do it.
I know python has better ways than that to do multi lines.

you can use select for input menus

Code:
echo who is your favourite beatle?
select choice in john paul ringo george
do

    echo you chose $choice
    break
done
 
Old 06-03-2009, 07:42 PM   #4
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Quote:
Originally Posted by dudeman41465 View Post
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.
 
  


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
User input into Bash scripts and checking validity of user input?? helptonewbie Programming 8 07-07-2008 06:40 PM
Getting asynchronous user input in BASH jakeo25 Linux - Software 1 05-08-2008 06:22 PM
Bash Y/N user input zcrxsir88 Programming 11 04-16-2008 11:35 AM
Bash scripting and user input Woodsman Slackware 13 11-02-2005 02:20 PM
User input using a BASH script... causticmtl Programming 5 07-13-2003 09:59 PM

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

All times are GMT -5. The time now is 03:25 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