Friday, May 13, 2011

Best Bash Scripting Tutorial

http://www.youtube.com/watch?v=zaULBbY4SVs&feature=related

My Best Bash scripting tutorial involved two tutorials that depend on one another for the script to work.
First, a program called lynx runs in Bash and can take whole html files from websites and display them in the shell.  

 67 cd test
68 ls  

69 lynx --dump http://ccmixter.org/view/media/remix
# Get the url of the website and call lynx to dump the 
#results from the specified website   

70 lynx --source http://ccmixter.org/view/media/remix
# lynx displays the source code of the webpage  

71 lynx --source http://ccmixter.org/view/media/remix|grep "mp3"
# After the source code is shown, the grep command shows 
all the words or characters that are contained within the 
specified string. The symbol | was referred to by the narrator as piing through.
 Maybe this means that several commands can be written in one statement as long as there
 is a pipeline present.  

 72* lynx --source http://ccmixter.org/view/media/remix|grep "mp3"|cut -d\' -f4
#another command is added : cut. The commands (-d\' and -f4 for field 4) select just the urls of the actual song themselves.

73 ffplay http://ccmixter.org/content/AlexBeroza/AlexBeroza_-_Start_Again.mp3
# ffplay play this song which I cut and paste onto the command line.

74 history > whileloop.txt   
75 history >> whileloop.txt 
 76 history >> whileloop2.txt
# I saved my history multiple times.

http://www.youtube.com/watch?v=R-ZTyn1iETE

The next tutorial deals with while loops in bash, and the while loop that is created will play, one by one, the songs I grabbed offline with lynx, grep, and cut. But first, a simpler example was shown to relay the concept.

66 name=Jennifer
# I set the variable (name) to Jennifer.  
 67 echo $name    
 #this produces my name on a single line with nothing else.

68 name=Jenn
# I reset the variable to store Jenn. 

 69 echo $name
# this prints out the single line of Jenn.  

70 echo "my name is $name"
 # This prints: my name is Jenn.

   71  let x=1
# sets the variable x to 1.

   72  echo 4x
# was supposed to echo $x

   73  echo $x
# actually echoes $x on a single line.

   74  let x=$x+1
# x is now equal to itself plus one.

   75  echo $x
# X = 2 now because 1 was added to 1 in the above line. The previous value of $x was 1.

   76  let x=$x+1;echo $x
# x = 3

   77  let x=$x+1;echo $x
# x = 4

   78  let x=$x+1;echo $x
#  x = 5

   79  let x=$x+1;echo $x
# x = 6

   80  while [ 1 ]; do echo "Hello World."; done
# This while loop is a preliminary example for the better, more advanced while loop later using sound. The loop states that while the result occurs one time, do this echo "Hello World."), and then it is done.

   81  while [ 1 ]; do echo "Hello World.";sleep 1;done
# This produces the same result as the line above, but before the loop is finished, it states that after every time the loop prints the statement, the "program" will wait one second, then continue. 

   82  let x=1;while [ $x -lt 10 ]; do echo "x is: $x";let x=$x+1;sleep 1;done
# This while loops states that x will equal one. Then the while loops starts with the statement that while the $x is -lt (less then) the integer 10, echo the statement
x is : a number less than 10. It then increases the value of $x until it get to number 9 after waiting one second before moving on to the next print statement.

   83  history > whileloop.txt

   77  ls
  
78  ls|while read file;do echo "file found: $file"; done
#This for loop displays all the files in whatever directory I am in at the time. It reads all the files in that directory and prints: file found and then the current file stored in the variable file.

   79  lynx --source http://ccmixter.org/view/media/remix|grep "mp3"|cut -d\' -f4
#another command is added : cut. The commands (-d\' and
-f4 for field 4) select just the urls of the actual song themselves. I redid the command here to show the urls again.


   80  lynx --source http://ccmixter.org/view/media/remix|grep "mp3"|cut -d\' -f4|while read mp3;do ffplay "$mp3";done
#the command takes the above line and makes a while loop out of it. It states that while bash it reading all the mp3 files, it should be them all until there are no more songs in the list. 
  
81  history >> whileloop2.txt
  
82  ls
83  history >> whileloop2.txt
#saving history to the file.

No comments:

Post a Comment