Tuesday, October 19, 2010

Bash troubles and their solutions

First off I have not had to do bash scripting since first semester in ULI101, because of this I remembered approximately 0.01217% of how to bash script!

So after a lot of googling, I finally feel fairly comfortable working with bash again. I'll list the issues that came up in case anyone else is perusing this and comes upon them.

1 - I've gotten super used to programming languages, therefore I forgot that shell scripting is essentially just calling command after command. So something like sending a string of text into a file isn't as simple as

"This is a string of text" > thisisafile

In order to do something simple like this we have to use a command like echo

echo "This is a string of text now not generating errors!" > stillafile

2 - Similar to my first issue, I was stuck firmly in a programming frame of mind, and was repeatedly attempting to read in a bunch of test files into arrays so that I could merge them together and send them off into a new merged file. Although you can do this, it makes much more sense to just cat everything together and call it a day

cat file1 file2 > muchsimplerthanreadingstuffin

3 - White space fun! Alright, not really fun, bash is a pretty grumpy gus about white space, take this example:

if [ "$variable" = "this works" ]
then
     #do something
fi

if ["$variable" = "this doesn't"]
then
    #do nothing because you'll get an error message!
fi

4 - The error messages are not very helpful at all, for instance the error I continued to get for was command not found.


After figuring out all of these little inconveniences, I'm feeling pretty confident about shell scripting again.

No comments:

Post a Comment