Thread: [Linuxcommand-discuss] Greetings & a Script
Brought to you by:
bshotts
From: Bruce B. <bbu...@ea...> - 2002-08-25 17:26:35
|
Hello everyone.....Like I assume most of you are, I am new to Linux and learning Bash..... Here's a script I wrote that I woouldn't mind some feedback on.......It's for changing the extensions on files in the pwd and skips over files without the extension entered as the first argument..... #!/bin/bash # 2.05 old=$1 new=$2 for i in $( ls ); do if [[ $i != *$old ]]; then shift else k=$( basename $i ${old} ) mv $i ${k}${new} fi done Thanks to HoBoJack for telling me about LC. I have yet to receive any mail from the list, so maybe someone could look into that? Bruce<+> |
From: William S. <wes...@co...> - 2002-08-27 01:08:29
|
On Sunday 25 August 2002 01:26 pm, Bruce Burhans wrote: > #!/bin/bash > # 2.05 > old=$1 > new=$2 > for i in $( ls ); do > if [[ $i != *$old ]]; then > shift > else > k=$( basename $i ${old} ) > > mv $i ${k}${new} > fi > done First, you could simplify it by doing the following: #!/bin/bash old=$1 new=$2 for i in *$old; do k=$(basename $i $old) mv $i $k$new done I haven't tested this, but it looks right ;-) -- ||||| William Shotts, Jr. (bshotts AT panix DOT com) ||||| Be a Linux Commander! Follow me to http://linuxcommand.org |
From: Bruce B. <bbu...@ea...> - 2002-08-27 02:17:05
|
----- Original Message ----- From: "William Shotts" <wes...@co...> To: <lin...@li...> Sent: Monday, August 26, 2002 6:08 PM Subject: Re: [Linuxcommand-discuss] Greetings & a Script > On Sunday 25 August 2002 01:26 pm, Bruce Burhans wrote: > > > #!/bin/bash > > # 2.05 > > old=$1 > > new=$2 > > for i in $( ls ); do > > if [[ $i != *$old ]]; then > > shift > > else > > k=$( basename $i ${old} ) > > > > mv $i ${k}${new} > > fi > > done > > First, you could simplify it by doing the following: > > #!/bin/bash > > old=$1 > new=$2 > > for i in *$old; do > k=$(basename $i $old) > mv $i $k$new > done > > I haven't tested this, but it looks right ;-) > > -- > ||||| William Shotts, Jr. (bshotts AT panix DOT com) > ||||| Be a Linux Commander! Follow me to http://linuxcommand.org > > Will give it a try, Bill........But will it skip over files that do not have the old extension? Here's one that I use all the time.....I call it fe for "file explorer"...... There are actually two- the main one puts the $PWD in a temp file in my $HOME when I choose EXIT, then . dc (that's a dot and a space for 'source' ) cds me to that directory . ----------------------------------------------------- #!/bin/bash ###2.05 ### fe while : ; do select fname in $HOME / EXIT $(ls -aF) ; do if [ "$fname" = EXIT ] ; then echo "$PWD" > /home/farley931/.t2 exit; fi if [ -f "$fname" ] ; then less "$fname" elif [ -d "$fname" ] ; then cd "$fname" echo "$PWD" break fi done done ---------------------------------------------------------- ### dc cd $(cat /home/farley931/.t2) -------------------------------------------------------- Bruce<+> |
From: William S. <wes...@co...> - 2002-08-27 02:33:23
|
On Monday 26 August 2002 10:16 pm, Bruce Burhans wrote: > > #!/bin/bash > > > > old=$1 > > new=$2 > > > > for i in *$old; do > > k=$(basename $i $old) > > mv $i $k$new > > done > Will give it a try, Bill........But will it skip over > files that do not have the old extension? I think so. The wildcard expansion in the "for" command should only include files that end with the old extension. > Here's one that I use all the time.....I call it fe for > "file explorer"...... > > There are actually two- the main one puts the $PWD > in a temp file in my $HOME when I choose EXIT, > then . dc (that's a dot and a space for 'source' ) cds > me to that directory . > > > > ----------------------------------------------------- > > > #!/bin/bash > ###2.05 > ### fe > while : ; do > select fname in $HOME / EXIT $(ls -aF) ; do > if [ "$fname" = EXIT ] ; then > echo "$PWD" > /home/farley931/.t2 > exit; fi > if [ -f "$fname" ] ; then > less "$fname" > elif [ -d "$fname" ] ; then > cd "$fname" > echo "$PWD" > break > fi > done > done > > ---------------------------------------------------------- > ### dc > cd $(cat /home/farley931/.t2) > > -------------------------------------------------------- One weird script at a time, man ;-) -- ||||| William Shotts, Jr. (bshotts AT panix DOT com) ||||| Be a Linux Commander! Follow me to http://linuxcommand.org |
From: Bruce B. <bbu...@ea...> - 2002-08-27 02:42:54
|
----- Original Message ----- From: "William Shotts" <wes...@co...> To: <Lin...@li...> Sent: Monday, August 26, 2002 7:33 PM Subject: Re: [Linuxcommand-discuss] Greetings & a Script > > Here's one that I use all the time.....I call it fe for > > "file explorer"...... > > > > There are actually two- the main one puts the $PWD > > in a temp file in my $HOME when I choose EXIT, > > then . dc (that's a dot and a space for 'source' ) cds > > me to that directory . > > > > > > > > ----------------------------------------------------- > > > > > > #!/bin/bash > > ###2.05 > > ### fe > > while : ; do > > select fname in $HOME / EXIT $(ls -aF) ; do > > if [ "$fname" = EXIT ] ; then > > echo "$PWD" > /home/farley931/.t2 > > exit; fi > > if [ -f "$fname" ] ; then > > less "$fname" > > elif [ -d "$fname" ] ; then > > cd "$fname" > > echo "$PWD" > > break > > fi > > done > > done > > > > ---------------------------------------------------------- > > ### dc > > cd $(cat /home/farley931/.t2) > > > > -------------------------------------------------------- > > One weird script at a time, man ;-) > :-) Actually this one is very practical, and I literally use it all the time......Haven't used mc since I wrote it... Will get back to you on the streamlined script soon. Bruce<+> > -- > ||||| William Shotts, Jr. (bshotts AT panix DOT com) > ||||| Be a Linux Commander! Follow me to http://linuxcommand.org > > > ------------------------------------------------------- > This sf.net email is sponsored by: OSDN - Tired of that same old > cell phone? Get a new here for FREE! > https://www.inphonic.com/r.asp?r=sourceforge1&refcode1=vs3390 > _______________________________________________ > Linuxcommand-discuss mailing list > Lin...@li... > https://lists.sourceforge.net/lists/listinfo/linuxcommand-discuss |
From: Bruce B. <bbu...@ea...> - 2002-08-29 00:36:27
|
----- Original Message ----- From: "William Shotts" <wes...@co...> To: <lin...@li...> Sent: Monday, August 26, 2002 6:08 PM Subject: Re: [Linuxcommand-discuss] Greetings & a Script > On Sunday 25 August 2002 01:26 pm, Bruce Burhans wrote: > > > #!/bin/bash > > # 2.05 > > old=$1 > > new=$2 > > for i in $( ls ); do > > if [[ $i != *$old ]]; then > > shift > > else > > k=$( basename $i ${old} ) > > > > mv $i ${k}${new} > > fi > > done > > First, you could simplify it by doing the following: > > #!/bin/bash > > old=$1 > new=$2 > > for i in *$old; do > k=$(basename $i $old) > mv $i $k$new > done > > I haven't tested this, but it looks right ;-) > Bill.....I have bad news and good news..... The script above puts the new extension on every file in the directory, regardless of its present extension... The test part is needed.... I am a good way through your scripting tutorial, and it is the best one out of a half-dozen I have on my box.. You take the time to explain what is going on, to answer obvious questions that pop up, and are oriented towards functionality. For example: Not one of the other tutorials mentions that you bash has a help command..... I've read the entire bash reference manual, and had no idea that such a thing existed.......Nor do any of them tell you that you put a function in your .bashrc.....! (why don't you need to chmod +rx it ? ) or enter them on the cli..... You have the makings of a really good book there... (e or otherwise ) Thanks, Bruce<+> > -- > ||||| William Shotts, Jr. (bshotts AT panix DOT com) > ||||| Be a Linux Commander! Follow me to http://linuxcommand.org > > > ------------------------------------------------------- > This sf.net email is sponsored by: OSDN - Tired of that same old > cell phone? Get a new here for FREE! > https://www.inphonic.com/r.asp?r=sourceforge1&refcode1=vs3390 > _______________________________________________ > Linuxcommand-discuss mailing list > Lin...@li... > https://lists.sourceforge.net/lists/listinfo/linuxcommand-discuss |
From: Alaa T. G. <al...@an...> - 2002-08-29 00:58:29
|
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 nice script, I modified it a bit to be handle filenames with spaces (they are not a good idea I know) #!/bin/bash old=$1 new=$2 for i in *$old; do echo mv \"$i\" \"`basename "$i" $old`$new\"; done I prefer it to print the commands on stdout first just to make sure that everything is OK and then pipe the output to bash for the actual renaming to happen. cheers, Alaa - -- Perilous to all of us are the devices of an art deeper than we ourselves possess. -- Gandalf the Grey [J.R.R. Tolkien, "Lord of the Rings"] -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.0.6 (GNU/Linux) iD8DBQE9bXCQqIWBQ7ecPHcRAudiAJ9v0GMzAtqk5BqpGYcBEtU/VUh9bwCghC+M xIYgdHy/0trhSLFzqc/8zag= =aHkG -----END PGP SIGNATURE----- |
From: Bruce B. <bbu...@ea...> - 2002-08-29 16:28:01
|
----- Original Message ----- From: "Alaa The Great" <al...@an...> To: "Bruce Burhans" <bbu...@ea...> Sent: Thursday, August 29, 2002 12:48 AM Subject: Re: [Linuxcommand-discuss] Greetings & a Script > -----BEGIN PGP SIGNED MESSAGE----- > Hash: SHA1 > > On Wed, 28 Aug 2002 20:15:34 -0700 > "Bruce Burhans" <bbu...@ea...> wrote: > > > > > I modified it a bit to be handle filenames with spaces (they are > > > not a good idea I know) > > > > > They are a drag...Linux doesn't know how to cope with them.....I > > end up > > having to do mv foo* and ls > > divides the names into seperate filenames..... > > it copes with them fine if you know bash quoting rules, they are very > complex however and I always have to try several combinations before I > get it right. > I was referring to files transferred from that legacy system....... > > > > #!/bin/bash > > > old=$1 > > > new=$2 > > > for i in *$old; > > > do > > > echo mv \"$i\" \"`basename "$i" $old`$new\"; > > > done > > > > > > > this works without the $k part? Sure looks like it > > would...... Will > > yes it works because of the back ticks which puts the output of > basename in front of echo. > Yes, I caught that.......Bill says that backticks are ugly and to use $( ). But more is involved : With a poor editor like the one in OE, it is very hard to tell whether they are forward or back quotes..... > > have to try it....What if there IS no space....? If it works it > > sure is efficient.......... > of course it works if there are no spaces, it handles any other > special character except the double quote itself. > > > How do you pipe something on stdout to bash? > sh foo | bash > > this pipes the output of the script foo to bash > > if foo is executable then simply > > foo | bash > > this is one of the things that no one tells you, bash reads from stdin > like any other GNU command. > THAT is valuable information....And until now I had no clue......It is hard to relate to bash as just another executable..... > I use this technique all the time specially because of bad filenames, > you can never be sure what your script is going to do. > > P.S. loved the fe script, it amazing how much you can achieve in few > lines, maybe you should work on adding more options > Thanks. It would be nice to have one to run <file> or <ls -lha> or <zless> instead of <less>......Needs a few real registers (In RAM, which I don't know how to create Would you just create a ramdisk?) for file operations like cp and mv....... Here's a simple script I wrote so that I could have a running clock to set my house clock.... blah while : do date sleep 1 clear done # ^c to exit . Bruce<+> > > cheers, > Alaa > - -- > Perilous to all of us are the devices of an art deeper than we > ourselves > possess. > -- Gandalf the Grey [J.R.R. Tolkien, "Lord of the > Rings"] > -----BEGIN PGP SIGNATURE----- > Version: GnuPG v1.0.6 (GNU/Linux) > > iD8DBQE9bdHfqIWBQ7ecPHcRAhY2AJ9PveFole60a+LA0RqkDNDvO171pgCffwQ0 > fcfAztidmRlWRTrDFDOj14E= > =0fkM > -----END PGP SIGNATURE----- |
From: Alaa T. G. <al...@an...> - 2002-08-29 16:59:44
|
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 On Thu, 29 Aug 2002 09:27:51 -0700 "Bruce Burhans" <bbu...@ea...> wrote: > > I was referring to files transferred from that legacy > system....... yes thats why I have to make my scripts space safe, most of my media files are from the dark days when all I had was TLS :-) > > yes it works because of the back ticks which puts the output of > > basename in front of echo. > > Yes, I caught that.......Bill says that backticks are > ugly and to use $( ). But more is involved : With a > poor editor like the one in OE, it is very hard to tell > whether they are forward or back quotes..... but on the other hand they are identical?? or is there a difference?? I prefer the backticks brackets in front of a dollar sign have many meaning its confusing to memorize them all, anyways I use mcedit when I'm writing bash script, backticks have a black background which makes them very visible against the blue screen. > THAT is valuable information....And until now I had > no clue......It is hard to relate to bash as just another > executable..... yes but it makes sense, I use this alot to do jobs like find files using locate or find | filter with grep etc | add a command in front of each line using awk | bash to execute this script locate core | grep "/core$" | awk '{print "rm " $0}' | bash and it removes all core files (or at least all core files the slocate data base knows about) and a small script (a wrapper really) to rename mp3 files according to their id3 data, it uses mp3info for this. #!/bin/bash #mp3rename for i in $*; do mp3info -p "mv \"%f\" \"%a_-_%t.mp3\"\n" "$i" | grep -v "_-_.mp3" |tr [\'\;\&] " "; done I need an ogg one but ogginfo isn't as flexible as mp3info anyone with ideas?? I'm also thinking about one that does the opposite (change id tags according to filenames) BTW I'm writing a command line and Bash tutorial for my LUG, unfortunatly I have to write in English first and then translate it to my native language arabic (it is much easier this way) anypne care to review it for me I really need feedback. cheers, Alaa - -- Perilous to all of us are the devices of an art deeper than we ourselves possess. -- Gandalf the Grey [J.R.R. Tolkien, "Lord of the Rings"] -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.0.6 (GNU/Linux) iD8DBQE9blINqIWBQ7ecPHcRAhIeAJ9I25GB7e2cloBcSxHkKUkFW2cNzgCdECyl qyDXBo+Crv3pOD/A8pklJW8= =iR/r -----END PGP SIGNATURE----- |
From: William S. <wes...@co...> - 2002-08-29 02:14:43
|
On Wednesday 28 August 2002 08:36 pm, Bruce Burhans wrote: > > First, you could simplify it by doing the following: > > > > #!/bin/bash > > > > old=$1 > > new=$2 > > > > for i in *$old; do > > k=$(basename $i $old) > > mv $i $k$new > > done > > > > I haven't tested this, but it looks right ;-) > > Bill.....I have bad news and good news..... > > The script above puts the new extension on every file > in the directory, regardless of its present extension... > The test part is needed.... Try it again Bruce. The "for" will only select files that have the old extension. Make sure you are using: for i in *$old; and not for i in * $old; If you include a space between the "*" and the "$old", it will process all the files. > > I am a good way through your scripting tutorial, and it > is the best one out of a half-dozen I have on my box.. > > You take the time to explain what is going on, to answer > obvious questions that pop up, and are oriented towards > functionality. For example: Not one of the other tutorials > mentions that you bash has a help command..... > > I've read the entire bash reference manual, and had no > idea that such a thing existed.......Nor do any of them > tell you that you put a function in your .bashrc.....! > (why don't you need to chmod +rx it ? ) or enter them > on the cli..... You don't need to make it executable because it's not executed, it's sourced. That is, it is read as input by another script, usually .bash_profile. > You have the makings of a really good book there... > (e or otherwise ) Maybe someday. I'd be happy just to have enough time to write the next lesson. -- ||||| William Shotts, Jr. (bshotts AT panix DOT com) ||||| Be a Linux Commander! Follow me to http://linuxcommand.org |
From: Bruce B. <bbu...@ea...> - 2002-08-29 03:16:20
|
----- Original Message ----- From: "William Shotts" <wes...@co...> To: <lin...@li...> Sent: Wednesday, August 28, 2002 7:14 PM Subject: Re: [Linuxcommand-discuss] Greetings & a Script > On Wednesday 28 August 2002 08:36 pm, Bruce Burhans wrote: > > > > First, you could simplify it by doing the following: > > > > > > #!/bin/bash > > > > > > old=$1 > > > new=$2 > > > > > > for i in *$old; do > > > k=$(basename $i $old) > > > mv $i $k$new > > > done > > > > > > I haven't tested this, but it looks right ;-) > > > > Bill.....I have bad news and good news..... > > > > The script above puts the new extension on every file > > in the directory, regardless of its present extension... > > The test part is needed.... > > Try it again Bruce. The "for" will only select files that have the old > extension. Make sure you are using: > > for i in *$old; > > and not > > for i in * $old; > > If you include a space between the "*" and the "$old", it will process all > the files. Ahh! Oops! Dot thy i's and cross thy t's..... > > > > > I am a good way through your scripting tutorial, and it > > is the best one out of a half-dozen I have on my box.. > > > > You take the time to explain what is going on, to answer > > obvious questions that pop up, and are oriented towards > > functionality. For example: Not one of the other tutorials > > mentions that you bash has a help command..... > > > > I've read the entire bash reference manual, and had no > > idea that such a thing existed.......Nor do any of them > > tell you that you put a function in your .bashrc.....! > > (why don't you need to chmod +rx it ? ) or enter them > > on the cli..... > > You don't need to make it executable because it's not executed, it's sourced. > That is, it is read as input by another script, usually .bash_profile. > > > You have the makings of a really good book there... > > (e or otherwise ) > > Maybe someday. I'd be happy just to have enough time to write the next > lesson. > Am looking forward to it..... Bruce<+> > -- > ||||| William Shotts, Jr. (bshotts AT panix DOT com) > ||||| Be a Linux Commander! Follow me to http://linuxcommand.org > > > ------------------------------------------------------- > This sf.net email is sponsored by: Jabber - The world's fastest growing > real-time communications platform! Don't just IM. Build it in! > http://www.jabber.com/osdn/xim > _______________________________________________ > Linuxcommand-discuss mailing list > Lin...@li... > https://lists.sourceforge.net/lists/listinfo/linuxcommand-discuss |
From: William S. <wes...@co...> - 2002-08-29 02:21:50
|
On Wednesday 28 August 2002 08:53 pm, Alaa The Great wrote: > nice script, > I modified it a bit to be handle filenames with spaces (they are not a > good idea I know) > > #!/bin/bash > old=$1 > new=$2 > for i in *$old; > do > echo mv \"$i\" \"`basename "$i" $old`$new\"; > done > > > I prefer it to print the commands on stdout first just to make sure > that everything is OK and then pipe the output to bash for the actual > renaming to happen. > > cheers, > Alaa Yes! That is an excellent technique for testing your work. Everybody should look at this and see how Alaa is using echo to see the command that is created. I do this same trick all the time while I am developing. -- ||||| William Shotts, Jr. (bshotts AT panix DOT com) ||||| Be a Linux Commander! Follow me to http://linuxcommand.org |
From: Bruce B. <bbu...@ea...> - 2002-08-29 17:33:27
|
----- Original Message ----- From: "Alaa The Great" <al...@an...> To: <lin...@li...> Sent: Thursday, August 29, 2002 9:55 AM Subject: Re: [Linuxcommand-discuss] Greetings & a Script > -----BEGIN PGP SIGNED MESSAGE----- > Hash: SHA1 > > On Thu, 29 Aug 2002 09:27:51 -0700 > "Bruce Burhans" <bbu...@ea...> wrote: > > > > > I was referring to files transferred from that legacy > > system....... > > yes thats why I have to make my scripts space safe, most of my media > files are from the dark days when all I had was TLS :-) > > > > yes it works because of the back ticks which puts the output of > > > basename in front of echo. > > > > Yes, I caught that.......Bill says that backticks are > > ugly and to use $( ). But more is involved : With a > > poor editor like the one in OE, it is very hard to tell > > whether they are forward or back quotes..... > > but on the other hand they are identical?? or is there a difference?? > I prefer the backticks brackets in front of a dollar sign have many > meaning its confusing to memorize them all, anyways I use mcedit when > I'm writing bash script, backticks have a black background which makes > them very visible against the blue screen. > > > THAT is valuable information....And until now I had > > no clue......It is hard to relate to bash as just another > > executable..... > yes but it makes sense, I use this alot to do jobs like > > find files using locate or find | filter with grep etc | add a command > in front of each line using awk | bash to execute this script > > locate core | grep "/core$" | awk '{print "rm " $0}' | bash > > and it removes all core files (or at least all core files the slocate > data base knows about) > > and a small script (a wrapper really) to rename mp3 files according to > their id3 data, it uses mp3info for this. > > #!/bin/bash > #mp3rename > > for i in $*; > do > mp3info -p "mv \"%f\" \"%a_-_%t.mp3\"\n" "$i" | grep -v "_-_.mp3" > |tr [\'\;\&] " "; > > done > > I need an ogg one but ogginfo isn't as flexible as mp3info anyone with > ideas?? > > I'm also thinking about one that does the opposite (change id tags > according to filenames) > > BTW I'm writing a command line and Bash tutorial for my LUG, > unfortunatly I have to write in English first and then translate it > to my native language arabic (it is much easier this way) anypne care > to review it for me I really need feedback. > Be happy to, IF you think I am qualified....Have yet to even begin learning AWK.........Send in plain text if possible, in the main body of the message..... Your English is excellent.... Bruce<+> > cheers, > Alaa > > - -- > Perilous to all of us are the devices of an art deeper than we > ourselves > possess. > -- Gandalf the Grey [J.R.R. Tolkien, "Lord of the > Rings"] > -----BEGIN PGP SIGNATURE----- > Version: GnuPG v1.0.6 (GNU/Linux) > > iD8DBQE9blINqIWBQ7ecPHcRAhIeAJ9I25GB7e2cloBcSxHkKUkFW2cNzgCdECyl > qyDXBo+Crv3pOD/A8pklJW8= > =iR/r > -----END PGP SIGNATURE----- > > > ------------------------------------------------------- > This sf.net email is sponsored by:ThinkGeek > Welcome to geek heaven. > http://thinkgeek.com/sf > _______________________________________________ > Linuxcommand-discuss mailing list > Lin...@li... > https://lists.sourceforge.net/lists/listinfo/linuxcommand-discuss |
From: Alaa T. G. <al...@an...> - 2002-08-29 18:10:31
|
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 ooops very sorry for sending this on the list, I didn't realize what I was doing until it was too late, sorry. Alaa -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.0.6 (GNU/Linux) iD8DBQE9bmKnqIWBQ7ecPHcRAsJrAJ97ZuiSdjWfjyG6Ti2UyzU4KH4Q6wCePIs7 bBoO9BuoZ3iTErsIab4TVW0= =hzEu -----END PGP SIGNATURE----- |