It is currently Thu May 24, 2018 7:24 pm

Passing user input to functions?

All times are UTC - 6 hours

Post new topic Reply to topic  [ 2 posts ] 
Author Message
 PostPosted: Thu Apr 12, 2012 4:51 am   

Joined: Thu Apr 12, 2012 4:14 am
Posts: 1
Location: Pompey UK
Hi there! I just started learning and writing shell scripts and have decided to practice the basics of file handling, script control and user input.

I use a command line music player called xmms2 (now updated to nyxmms2)
and decided to shorten the commands.

as you can see I have written a function (I intend to have all of the case statement options as functions)

Is there a better way to pass the value of $2 to the function?

any other criticism welcome :)

thanks in advance.


# *******smegy********* copy of smeg for testing new things

# smeg - small media entertainment gizzmo
# this shell script controls the nyxmms2 music player with simpler
# commands

function jumpTo {
   nyxmms2 jump "$patern"

# if the varible $1 contains input then procede through the case statement
# checking for commands
if [ -n "$1" ]
   case "$1" in
   jumpit) patern="$2"; jumpTo ;;
   on) nyxmms2 next;;   
   # play next tune
   back) nyxmms2 prev;;
   # go back one tune
   list) nyxmms2 list;;
   # list the current playlist
   off) nyxmms2 server shutdown;;
   # shut down nyxmms2 removing it from processes
   add) nyxmms2 add -t artist:"$2";;
   # append an artist to the current playlist
   showlists) nyxmms2 collection list;;
   # show all playlists avialable
   mplist) xmms2 collection create "$2" artist:"$3";;
   # make and name a new playlist
   search) xmms2 search artist:"$2";;
   # search for artist list,  useful to do before adding and
   # naming a new playlist
   *) nyxmms2 stop; nyxmms2 playlist clear; nyxmms2 add in:"$1"; nyxmms2 play;;
   # if not a command add artist playlist to the playlist
# if $1 does not contain input then simply toggle play/pause
# when paused xymms2 is running in the background,  to terminate it
# use smeg off
else nyxmms2 toggle

Last edited by ionsbrew on Thu Apr 12, 2012 6:42 am, edited 1 time in total.

 PostPosted: Thu Apr 12, 2012 5:31 am   

Joined: Mon Mar 02, 2009 3:03 am
Posts: 643

$ myFunction() {
>   echo "$1"
> }
$ myFunction foo bar baz
$ myFunction foo\ bar baz
foo bar

inside a script, directly pass positionnal parameter to the function

myFunction() { echo "$1";}

myFunction "$2"

./myScript foo bar baz
./myScript foo bar\ baz
bar baz

Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 2 posts ] 

All times are UTC - 6 hours

Who is online

Users browsing this forum: Bing [Bot] and 5 guests

You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  

BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group