Saturday, November 20, 2010

Linux Part 2 Lesson 1

Bash Scripts. Make Your own executable shells

Part 2 is about scripts. How do we write a script, what's the syntax and how do we make it executable?

First, there is a basic rule! In the first line we write:
to determine that the script we write is a bash script. (Don't know what is a bash? See Part 1 lesson 1)

And after that we can write code.
Let's see what steps do we need to follow to make a bash script of Part 1 Lesson 4

Bash Scripts can take arguments, which we can access in the script
i.e $1 is the first argument given in the command line.

The code is similar to those we wrote in the command line. However, now we have to make a text file which will contain our code.
We'll use a command line text editor, nano.
->type nano in the command line and press enter.

now determine which script to use (don't know what to write? Start from the beginning again)

This script will get your favourite artist from the command line, and add any songs found in the playlist.

The previous code was:
for music in *line*Dion*/*/*.wma; do
audacious -e "$music" &

know, it will be:

for music in $1/*/*.wma; do
audacious -e "$music" &

press control O and give a name to the file and then enter to save it. Remember, it will be saved in your current directory.

->Make it executable! How? With chmod +x nameOfScript
copy it in the /bin directory so it can be run any time
->now run it:
nameOfScript *Dion*

  1. if you have permission problems with copying to /bin use sudo or login as root and copy it there.
  2. We named our script AddToAudaciousPlaylist
  3. It is a good name to remember what it does!
  4. Remember not to put .wma if your songs are mp3.
  5. And the question of the day: How will we make user to choose the song format? Think about it! Next lesson is about improving our script!!!

And yeahhh! You have make your own first open source program in linux!What is open source? ;)
Remember! The script should be run in the directory that your music files live! Like before!

No comments:

Post a Comment