Go4Expert

Go4Expert (http://www.go4expert.com/)
-   Unix (http://www.go4expert.com/articles/unix/)
-   -   Bash Simple Tricks (Continuation) (http://www.go4expert.com/articles/bash-simple-tricks-continuation-t24812/)

lionaneesh 31Jan2011 20:56

Bash Simple Tricks (Continuation)
 
This is continuation of my previous article - Bash Simple Tricks Tutorial

Again I am following a question – solution approach to demonstrate my scripts..

Scripts



Write a script that displays the number of lines in a file that starts with a or A
Script :-

displayA.sh
Code:

#!/bin/bash

cat $1 | grep ^[aA] | wc -l

Sample file :-

file1
Code:

Aaaaaa
ssss
aaaaaa
wwwww
aa

Use :-

Code:

aneesh@aneesh-laptop:~/articles/Bash$ ./displayA.sh file1
3

Write a bash script that displays the no of lines in a file that ends with a 'z'
Script :-

endZ.sh
Code:

#!/bin/bash

cat $1 | grep [z]$ | wc -l

Sample file :-

file1
Code:

Aaaaaa
ssss
aaaaaa
wwwww
aa
Aneeshz
lionaneeshz
Woah!!
wowz
llllllo
ooooz

Use :-
Code:

aneesh@aneesh-laptop:~/articles/Bash$ ./endZ.sh file1
4

Write a bash script to show to no. of lines in a file that contains 'e or E' after 2 characters …
Script :-
Code:

#!/bin/bash

cat $1 | grep '..[eE]' | wc -l

Sample file :-
Code:

aaessss
aaaesss
aaaaaaaa
lionaneesh
ssssssssss

Use :-
Code:

aneesh@aneesh-laptop:~/articles/Bash$ ./char2.sh  file1
3

Write a shell script that lists all the filenames in the working directory starting with vowels?
Script :-
Code:

#!/bin/bash

ls | grep ^[aeiouAEIOU] | wc -l

Sample :-
Code:

aneesh@aneesh-laptop:~/articles/Bash$ ls
char2.sh  displayA.sh  endZ.sh  file1  vovelFile.sh

Use :-
Code:

aneesh@aneesh-laptop:~/articles/Bash$ ./vovelFile.sh
1

Write a bash script to play a song using mplayer
Script :-
Code:

#!/bin/bash

mplayer $1

Use :-
Code:

aneesh@aneesh-laptop:~/Desktop/Linkin Park - A Thousand Suns {MP3 320kbs}$ ./player.sh music.mp3

MPlayer SVN-r1.0~rc3+svn20090426-4.4.3 (C) 2000-2009 MPlayer Team

mplayer: could not connect to socket

mplayer: No such file or directory

Failed to open LIRC support. You will not be able to use your remote control.

Playing music.mp3.

Audio only file format detected.

==========================================================================

Opening audio decoder: [mp3lib] MPEG layer-2, layer-3

AUDIO: 48000 Hz, 2 ch, s16le, 320.0 kbit/20.83% (ratio: 40000->192000)

Selected audio codec: [mp3] afm: mp3lib (mp3lib MPEG layer-2, layer-3)

==========================================================================

AO: [pulse] 48000Hz 2ch s16le (2 bytes per sample)

Video: no video

Starting playback...

A:  16.6 (16.6) of 121.0 (02:01.0)  1.2%

MPlayer interrupted by signal 2 in module: play_audio

A:  16.8 (16.8) of 121.0 (02:01.0)  1.2%

Write a bash script to play your music using mplayer but after a specified seconds
Script :-
Code:

#!/bin/bash

sleep $2

mplayer $1

Use :-

Code:

aneesh@aneesh-laptop:~/Desktop/Linkin Park - A Thousand Suns {MP3 320kbs}$ ./player.sh music.mp3 3

MPlayer SVN-r1.0~rc3+svn20090426-4.4.3 (C) 2000-2009 MPlayer Team

mplayer: could not connect to socket

mplayer: No such file or directory

Failed to open LIRC support. You will not be able to use your remote control.

Playing music.mp3.

Audio only file format detected.

==========================================================================

Opening audio decoder: [mp3lib] MPEG layer-2, layer-3

AUDIO: 48000 Hz, 2 ch, s16le, 320.0 kbit/20.83% (ratio: 40000->192000)

Selected audio codec: [mp3] afm: mp3lib (mp3lib MPEG layer-2, layer-3)

==========================================================================

AO: [pulse] 48000Hz 2ch s16le (2 bytes per sample)

Video: no video

Starting playback...

A:  11.7 (11.7) of 121.0 (02:01.0)  1.2%

Stay tuned guyz for more interesting bash scripts articles and hope you like this one too!!
Thanks...

lionaneesh 2Feb2011 11:52

Re: Bash Simple Tricks (Continuation)
 
Thanks for accepting...
And I think other people can post the questions they want to be solved in my next article...


All times are GMT +5.5. The time now is 13:19.