![]() |
2010-07-14
, 03:27
|
|
Posts: 387 |
Thanked: 1,700 times |
Joined on Feb 2010
@ Cambridge, MA, USA
|
#11
|
![]() |
2010-07-14
, 14:08
|
Posts: 78 |
Thanked: 32 times |
Joined on May 2008
|
#12
|
![]() |
2010-07-15
, 15:02
|
Posts: 838 |
Thanked: 292 times |
Joined on Apr 2010
|
#13
|
![]() |
2010-07-15
, 15:26
|
Posts: 89 |
Thanked: 48 times |
Joined on Dec 2009
@ Glasgow
|
#14
|
despite its shortcomings I bought the n900 really only to learn shell scripting and perl.
#!/bin/bash convert= case $1 in group) convert=$(echo 1) ;; album) convert=$(echo 2) ;; label) convert=$(echo 3) ;; year) convert=$(echo 4) ;; *) echo "please input group, album, label, or year." ; exit 1 ;; esac cut -f$(echo $convert) -d\| albums
#!/bin/bash case $1 in group) convert=1 ;; album) convert=2 ;; label) convert=3 ;; year) convert=4 ;; *) echo "please input group, album, label, or year." exit 1 ;; esac cut -f$convert -d\| albums
awk 'BEGIN {FS="|"} {print $'$convert'}' albums
![]() |
2010-07-15
, 15:37
|
Posts: 838 |
Thanked: 292 times |
Joined on Apr 2010
|
#15
|
Great! I hope you don't mind if I offer a couple of helpful comments on your code...
You can tidy this slightly:Code:#!/bin/bash convert= case $1 in group) convert=$(echo 1) ;; album) convert=$(echo 2) ;; label) convert=$(echo 3) ;; year) convert=$(echo 4) ;; *) echo "please input group, album, label, or year." ; exit 1 ;; esac cut -f$(echo $convert) -d\| albums
So the substitution of convert drops straight in place on your 'cut' command.Code:#!/bin/bash case $1 in group) convert=1 ;; album) convert=2 ;; label) convert=3 ;; year) convert=4 ;; *) echo "please input group, album, label, or year." exit 1 ;; esac cut -f$convert -d\| albums
Another really useful tool to learn is AWK. You could replace your 'cut' command with a simple 'awk' program:
To explain, AWK operates on each line in a file. AWK treats each line as a series of fields ($1 is the first, $2 is second, $0 is the entire line). Here, I've set the field separator (FS) to the pipe character ("|") before any lines of text have been read, and I've told AWK to print out the correct field on each line. It may be a bit more verbose than cut in this example, but AWK is a powerful tool which I use all the time.Code:awk 'BEGIN {FS="|"} {print $'$convert'}' albums
Good luck and happy coding!
![]() |
2010-07-15
, 15:48
|
Posts: 89 |
Thanked: 48 times |
Joined on Dec 2009
@ Glasgow
|
#16
|
I thought any time you needed to get the value from a variable you had to use echo? no I guess not.
foo="hello cruel world" echo $foo | awk '{print $1,$3}'
I will investigate your awk command further, I know a tiny about it but know it is its own programming language and am scared to even look at the chapter on it in my book
![]() |
2010-07-15
, 15:50
|
Posts: 755 |
Thanked: 406 times |
Joined on Feb 2008
@ UK
|
#17
|
im not fond of writing shell scripts (i use my pc with an SSH for that, ever so rarely)..
it would be great if we had a mini GCC or C++ compiler for the n900 so we can create even little console programs.
![]() |
2010-07-15
, 15:54
|
Posts: 838 |
Thanked: 292 times |
Joined on Apr 2010
|
#18
|
![]() |
2010-07-15
, 16:11
|
Posts: 466 |
Thanked: 418 times |
Joined on Jan 2010
|
#19
|