Basic Bash Scripting Question

Discuss Programming
User avatar
Calum
guru
guru
Posts: 1349
Joined: Fri Jan 10, 2003 11:32 am
Location: Bonny Scotland
Contact:

Post by Calum » Tue Dec 20, 2005 2:12 pm

Calum wrote:okay and "$1" will do what i ask, right? thank you both.
Void Main wrote:Are you saying it would be helpfull to provide documentation with the code? :)
i was just reading over this and realised (shock! horror) i think the above interchange was a complete misunderstanding.
void main, i didn't mean "i ask you, what will that do?" i meant "will that do the thing that i am asking it to?".

Glad i got to clear that up!
:-D

User avatar
Void Main
Site Admin
Site Admin
Posts: 5716
Joined: Wed Jan 08, 2003 5:24 am
Location: Tuxville, USA
Contact:

Post by Void Main » Tue Dec 20, 2005 2:24 pm

I think my response was actually to X11. He was using "%1" which is what you would use in DOS/Windows batch files but in UNIX/Linux shell (base,bourne,korn,etc) scripts it would be "$1". Those are special variables that replace the first parameter passed to your script. The second parameter is "$2", third "$3", all params "$*", number of params "$#", etc.

User avatar
Calum
guru
guru
Posts: 1349
Joined: Fri Jan 10, 2003 11:32 am
Location: Bonny Scotland
Contact:

Post by Calum » Sun Jul 02, 2006 12:14 pm

ok, now here's another really dumb question.

i know what i want to do with the script, but am not sure what syntax to use. here goes:

ok, the function within wma2mp3 and the other scripts is quite simple, it simply converts the file to wav, usually using mplayer, and then converts to the output format, usually using lame or oggenc. This is fine, but i want to include a set of if/then tests to check whether certain software is installed. for instance, i want it to check for gogo first, if not installed, check for blade, if not installed, use lame. same with mplayer. not everybody has mplayer installed, and i suppose they might not have all the plugins, which is another thing, because the function might check for mplayer then try to use it but mplayer cannot decode the input file, not sure if that would be so easy to test for, might have to figure out the exit codes for mplayer when it can't decode a file.

anyway, that's what i would like to know, how to check in a bash script for whether things are installed, probably based on the exit code of "which" or possibly based on the exit code of the program itself when called. In a similar vein i suppose i want to make the function aware of when it has bombed out, and why, adding in different error messages like "no space left in output directory" and "codec not installed".

i am probably asking you to do all the work for me, but really i do know what shape the scripts should be in, just not sure of what words and characters to use to do it! i did try the bash scripting guide from tldp but i have never been able to engage with those tldp howtos at all, sadly. maybe i should dig out Unix Unleashed again and see what it says about bash.

User avatar
Void Main
Site Admin
Site Admin
Posts: 5716
Joined: Wed Jan 08, 2003 5:24 am
Location: Tuxville, USA
Contact:

Post by Void Main » Sun Jul 02, 2006 1:21 pm

There are a log of built in file tests in bash (-f, -e, -x etc) but you could also use which as you suggest:

Code: Select all

TOOL=lame
if which $TOOL > /dev/null 2>&1; then
   echo "$TOOL is installed"
else
   echo "$TOOL was not found"
fi
Of course you can change the logic to however you want. If you want to use the bash built-in to check if /usr/bin/lame is executable:

Code: Select all

TOOL=/usr/bin/lame
if [ -x $TOOL ]; then
   echo "$TOOL is executable"
else
   echo "$TOOL is not executable"
fi
which might be better for you though because it will search your PATH where you would have to assume where the file is installed using the file tests. If you did want to use the built in bash tests there are a log of them:

$ man bash

snippet:

Code: Select all

       -a file
              True if file exists.
       -b file
              True if file exists and is a block special file.
       -c file
              True if file exists and is a character special file.
       -d file
              True if file exists and is a directory.
       -e file
              True if file exists.
       -f file
              True if file exists and is a regular file.
       -g file
              True if file exists and is set-group-id.
       -h file
              True if file exists and is a symbolic link.
       -k file
              True if file exists and its ''sticky'' bit is set.
       -p file
              True if file exists and is a named pipe (FIFO).
       -r file
              True if file exists and is readable.
       -s file
              True if file exists and has a size greater than zero.
       -t fd  True if file descriptor fd is open and refers to a terminal.
       -u file
              True if file exists and its set-user-id bit is set.
       -w file
              True if file exists and is writable.
       -x file
              True if file exists and is executable.
       -O file
              True if file exists and is owned by the effective user id.
       -G file
              True if file exists and is owned by the effective group id.
       -L file
              True if file exists and is a symbolic link.
       -S file
              True if file exists and is a socket.
       -N file
              True if file exists and has been modified since it was last read.
       file1 -nt file2
              True  if  file1  is newer (according to modification date) than file2, or if file1 exists and file2 does
              not.
       file1 -ot file2
              True if file1 is older than file2, or if file2 exists and file1 does not.
       file1 -ef file2
              True if file1 and file2 refer to the same device and inode numbers.

JoeDude
administrator
administrator
Posts: 355
Joined: Sun Feb 08, 2004 1:41 pm
Location: Sutton Coldfield, UK
Contact:

Post by JoeDude » Sun Jul 02, 2006 4:46 pm

I have to say...

I didn't get any of this :O

User avatar
Calum
guru
guru
Posts: 1349
Joined: Fri Jan 10, 2003 11:32 am
Location: Bonny Scotland
Contact:

Post by Calum » Mon Jul 03, 2006 3:05 pm

well, it's new to me, but it looks logical - to be honest i didn't even try man bash, since i have tried in the past to read it and been swamped with info!

thanks for the help void main, as always.

User avatar
Calum
guru
guru
Posts: 1349
Joined: Fri Jan 10, 2003 11:32 am
Location: Bonny Scotland
Contact:

Post by Calum » Fri Sep 22, 2006 8:53 am

ok, new collection of scripts up at http://wma2mp3.501megs.com - all i have done is make it check for installed output encoders and corrected a few typing errors in the comments (!) but if anybody fancies just trying out these scripts and letting me know if you get any unpredictable behaviour or issues then that'd be fab. I hope to soon update the website to include better documentation and then after that to get the scripts to check for installed input decoders and then maybe take some input switches (like bitrate etc), also i will want to maybe make the scripts output only 44.1k stereo files (which would ensure audio CD burning compatibility with XCDRoast), then after that i might add some script that automate recording audio off the internet. Another thing i want to do now the scripts are getting bigger and more numerous is to have fewer scripts but have symlinks to activate them to do different conversions based on the name they were called by......

i haven't done any of that yet though. :-)

PS: by the way, what's the most sensible place for the scripts to be installed to in a system? I think /usr/local/bin is the best place since i think it tends to be in the $PATH, and also it tends to be empty of things that were installed when the system was installed. On the other hand there's something in the slackpacks howto that says /usr/local is off limits because it says so in the FHS (something about a networked environment, which makes some kind of sense i suppose). so should it go into /opt then? the problem is i'd need to put in a thing in the installer (thinking about making rpm and slackpaks) to add the installation directory to the $PATH, or should i just stick them into /usr/bin instead? i am not so sure about that since i am worried they might get "lost", on the other hand if they were installed with a slackpack or rpm then they should be easy to upgrade or remove. What do you think?

User avatar
Void Main
Site Admin
Site Admin
Posts: 5716
Joined: Wed Jan 08, 2003 5:24 am
Location: Tuxville, USA
Contact:

Post by Void Main » Fri Sep 22, 2006 9:51 am

If you want them installed as part of the system for all users to use there is no reason you can't put them right in /usr/bin. I find the problem with doing that is you can lose track of them and forget they are not part of the OS installation so they might get lost on a new install or upgrade. Because of this I usuually put these custom scripts in my ~/bin directory which is where I put all my personal scripts. I know they are backed up that way and I can easily copy my personal scripts to other systems if I need them. There is also nothing wrong with /usr/local/bin. I would say that is a good "in-between" spot if /usr/bin and ~/bin doesn't fit. That's a good place for custom non-OS included software that you want all users to have access to as long and it's easy to include /usr/local in your backup scripts (if you only keep backups of critical (non-OS installed) stuff).

P.S. I think your script has been quite popular based on the number of views on this thread, and I see talk of it in several places when I search for wma2mp3 on a google search.

P.S.S. It would be cool if you could also come up with an ogg2wma and an mp32wma even though I know it is against our religion. I just don't believe it's possible at the moment because WMA is proprietary MS as far as I know.

worker201
guru
guru
Posts: 668
Joined: Sun Jun 13, 2004 6:38 pm
Location: Hawaii

Post by worker201 » Fri Sep 22, 2006 11:37 am

[offtopic]Void, I believe PPS is the official abbreviation for a second postscript, not PSS :P [/offtopic]

Any chance of an m4a to mp3 script?

User avatar
Void Main
Site Admin
Site Admin
Posts: 5716
Joined: Wed Jan 08, 2003 5:24 am
Location: Tuxville, USA
Contact:

Post by Void Main » Fri Sep 22, 2006 11:56 am

worker201 wrote:[offtopic]Void, I believe PPS is the official abbreviation for a second postscript, not PSS :P [/offtopic]
Wow! Good to know, thanks!
Any chance of an m4a to mp3 script?
My guess is that a slight mod to the wma2mp3 script would get a w4a2mp3 pronto. In fact try this out:

http://voidmain.is-a-geek.net/files/scripts/m4a2mp3

I don't have any m4a files to try it on. Maybe you can point me to one for testing?

worker201
guru
guru
Posts: 668
Joined: Sun Jun 13, 2004 6:38 pm
Location: Hawaii

Post by worker201 » Fri Sep 22, 2006 12:46 pm

I put one of my own up temporarily:

User avatar
Void Main
Site Admin
Site Admin
Posts: 5716
Joined: Wed Jan 08, 2003 5:24 am
Location: Tuxville, USA
Contact:

Post by Void Main » Fri Sep 22, 2006 1:36 pm

Thanks Worker, yep the script I created worked just fine so you can use that. It did not id3 tag it though (of course the wma2mp3 doesn't either). It probably wouldn't take much to add id3 tagging from information pulled from the m4a file.

You might also look at this:

http://badcomputer.org/unix/code/sneetchalizer/

It appears to be able to do all the conversions plus handles the id3 tags. Haven't tried it yet though.

EDIT: I tried the sneetchalizer script and it converted to ogg nicely (including the id3 tags) but I got an error trying to convert to MP3:

To ogg:

Code: Select all

$ ./sneetchalizer -v --in=m4a --out=ogg beyond.m4a
sneetchalizer version 0.4.3 (Horton heard a what?). Use at your own risk
Written by Darren Kirby :: d@badcomputer.org :: http://badcomputer.org/unix/code/sneetchalizer/
Released under the Artistic License

Working on: beyond.m4a
     Title:     Beyond the Void
     Artist:    Void Main
     Album:     Void Main's Black Album
     Genre:     Void Rock
     Year:      2006
     Comment:   Starbellied using sneetchalizer
     Tracknum:  13 of 16
     converting to wav...

     converting to ogg...
Wrote: beyond.ogg

That is all.
Trying to convert to MP3:

Code: Select all

$ ./sneetchalizer -v --in=m4a --out=mp3 beyond.m4a
sneetchalizer version 0.4.3 (Horton heard a what?). Use at your own risk
Written by Darren Kirby :: d@badcomputer.org :: http://badcomputer.org/unix/code/sneetchalizer/
Released under the Artistic License

Working on: beyond.m4a
     Title:     Beyond the Void
     Artist:    Void Main
     Album:     Void Main's Black Album
     Genre:     Void Rock
     Year:      2006
     Comment:   Starbellied using sneetchalizer
     Tracknum:  13 of 16
     converting to wav...

     converting to mp3...
*** lame error: 256 ***
Failed to write: beyond.mp3

That is all.
Maybe I just didn't pass the appropriate options.

User avatar
Calum
guru
guru
Posts: 1349
Joined: Fri Jan 10, 2003 11:32 am
Location: Bonny Scotland
Contact:

Post by Calum » Fri Sep 22, 2006 2:32 pm

P.S. I think your script has been quite popular based on the number of views on this thread, and I see talk of it in several places when I search for wma2mp3 on a google search.
really? that's pretty cool, i certainly didn't know that. there must be other things called wma2mp3 though i would have thought, i can't be the only one to want to write this script surely.
P.S.S. It would be cool if you could also come up with an ogg2wma and an mp32wma even though I know it is against our religion. I just don't believe it's possible at the moment because WMA is proprietary M$ as far as I know.
well i can't think of any wma encoder that will run on linux or macosx however i haven't really ever looked for one either. also, i will never need to convert my stuff to wma, plus i don't want to be responsible for horrible wma files of things that used to be decent mp3s or oggs, it's like i would be participating in the lowering of the tone in the lossy music compression world by helping to up the quota of wma files out there. No, the whole point was to get rid of the wma files i already had, so i think it's safe to say there isn't any chance of me doing a *2wma one.
[offtopic]Void, I believe PPS is the official abbreviation for a second postscript, not PSS Razz [/offtopic]
you are right! since it's Post Script, then PPS is Post Post Script (while PSS would be Post Script Script, in my opinion this makes about the same amount of sense, but there you go)
Any chance of an m4a to mp3 script?
done, it's already part of the wma2mp3 0.15 tarball, there are actually twelve scripts in all at the moment, taking a large variety of input formats and outputting oggs, mp3s or wavs.
Maybe I just didn't pass the appropriate options.
well, that's not too good! in fact my other aim was to make the commands to use my scripts very simple. i am still agonising over whether to include *any* switches/options at all! certainly the default for my scripts, when run with no specified switches, will be to perform the job with no problems using sensible parameters. I could never understand those utilities (rar and unrar, to mention one) that do nothing unless you put in a string of characters that you have to look up every time you use the utility.

worker201
guru
guru
Posts: 668
Joined: Sun Jun 13, 2004 6:38 pm
Location: Hawaii

Post by worker201 » Fri Sep 22, 2006 3:07 pm

Calum wrote:I could never understand those utilities (rar and unrar, to mention one) that do nothing unless you put in a string of characters that you have to look up every time you use the utility.
I think this practice is a throwback to the toolsmithing age, before the concept of a user interface was even invented. Certainly the guy who invented tar for his own personal use didn't need to look up what the flag for bz2 archives is (FYI: -j). Plus, thousands of people are used to things working that way. Perhaps now that we have millions of people - many of them beginners - using these tools, a better interface is needed.

User avatar
Calum
guru
guru
Posts: 1349
Joined: Fri Jan 10, 2003 11:32 am
Location: Bonny Scotland
Contact:

Post by Calum » Sat Sep 23, 2006 1:33 am

well, i think there's a danger there of making every tool have a horrible windowsy GUI, or at the very least a curses style interface. I can't stand using a system where there's no command line interface, so i hope those little utilities always retain their current usefulness, for something like tar, perhaps this is double standards, but i don't mind the options in tar because i know the ones i am most likely to use. why is this? because i use tar quite regularly, whereas i never really use unrar except once in a blue moon, hence my annoyance with it. On the other hand unzip appears to work with no options, which always saves a minute or so on the rare occasions i use it.

perhaps i'm moaning about nothing.

Post Reply