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:
#!/bin/bash
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
$index
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" &
done

know, it will be:

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

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*


Notes:
  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!

Friday, November 12, 2010

Lesson 5 Revise, Answers of the Questions

Today we'll make a revision of the previous lessons answering the questions of lesson 4.
Here are the questions:
  1. Why have we used '&' after audacious -e "$music"? What does it means? try to open other programs with '&' at the end and without it!
  2. Why we put the audacious -e "$music" & in the for loop and didn't used a playlist variable like before? What script is better?
  3. Why music variable has a '$' in the for loop?
  4. Why have we put *.wma instead of *.mp3? Are you going to do the same?
  5. Why the -E became a -e? Check man audacious to find out!

  1. & runs the command or program in background, so the terminal is free for putting other commands!
  2. Because the -e adds songs to the current playlist.
  3. If a variable is going to be used for output must have $ in front
  4. The songs of Celine Dion was in wma format. If yours are mp3 or other format you shouldn't use wma.
  5. Because -E creates a temporary playlist.

Monday, November 8, 2010

Linux Lessons Part 1 Lesson 4

So, here is our case for today:
We want to make a playlist with our favorite artists.

This is a problem that needs string matching.
String is a sequence of alphanumerical characters.

So, let's consider the problem:

One of our favorite artists is Celine Dion.

But there are some problems in our folders:
The name of Celine Dion is correct, but in french, i.e. it's Céline Dion. Also, not all folders have just the artist's name. E.g there is a folder Celine Dion - Titanic.
So, let's go back to previous lessons.
Remember the * sign?
It means whatever characters. So if I write *l* means, all the strings that contain the letter l.
In the previous lessons we were searching for mp3's like that, *.mp3. It means whatever string ends with the characters .mp3 .
So, we are going to use that.
E.g if we are searching for folders of Celine Dion we are going to search only the characters that are common. Remember! Neither use so many nor so little:
We used *line*Dion* : this means, whatever characters, then 'line' then whatever characters and then Dion. We could use *Dion* or *C*line*Dion, but it's up to you what you think is right for your case.

So that's the previous code:

for music in */*/*.mp3; do
playlist+=$music;
done; audacious "$playilist" -E

Now we want to make a permanent playlist with our favourite artists!
Make sure that the playlist is empty:
audacious &
choose playlist, delete playlist (it closes the playlist, if you want
the playlist export it before deleting)

run that command:

for music in *line*Dion*/*/*.wma; do
audacious -e "$music" &
done


So after that you can run as many scripts as you want and add your favorite songs.
Then export the playlist so you can use it again.

Questions to consider until Saturday:
  • Why have we used '&' after audacious -e "$music"? What does it means? try to open other programs with '&' at the end and without it!
  • Why we put the audacious -e "$music" & in the for loop and didn't used a playlist variable like before? What script is better?
  • Why music variable has a '$' in the for loop?
  • Why have we put *.wma instead of *.mp3? Are you going to do the same?
  • Why the -E became a -e? Check man audacious to find out!
The answers of these questions will be published on Saturday, but it would be great if you share your opinions in our facebook wall!

Saturday same place :P

Saturday, November 6, 2010

Linux Lessons Part 1 Lesson 3

This lesson is about music. We all struggle with the GUI's to create a playlist.
In this lesson we will see how to create a simple playlist and on next lesson we will make it a little more complicated, so the playlist will have the exact songs we want.
The commands below was executed in linux Ubuntu Ultimate Edition 2.8.
If you have an other distribution of linux you will need to install audacious if you don't have it.
To install it in Ubuntu : sudo apt-get install audacious
In fedora: yum install audacious.
If you can't install it google it.

To create a playlist: check man audacious.
check those parts:






So let's say that:
Our songs are in directories which are in other directories.
All music files are .mp3

we will use the very useful for statement again.
we will use audacious but we will call it in the command line.
first use the command cd and change directory where your music files are.
my is cd $HOME/Music
and then create a list with the songs and load it on audacious like that:

for music in */*/*.mp3; do
playlist+=$music;
done; audacious "$playilist" -E

The first command is for that goes through each directory, and then goes through each another directory which are in the first directory. Then finds all mp3 files and adds them to the variable playlist.
The done closes the for, and then loads the playlist on audacious as a temporary playlist.

Question to consider until Wednesday:
How can you load both mp3 and wma ?

See you on Wednesday.
You can email your answer!

PS: if you can't install audacious try a similar command with the music player you have on your machine after you check its man pages!!!