LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-06-2007, 04:44 AM   #16
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682

One way to read those strings.

Code:
sed 's/\(..\)/\\x\1/g;s/.*/"&"/' | xargs echo -e
 
Old 12-06-2007, 06:34 AM   #17
/bin/bash
Senior Member
 
Registered: Jul 2003
Location: Indiana
Distribution: Mandrake Slackware-current QNX4.25
Posts: 1,802

Rep: Reputation: 47
If you like your code un-obfuscated and easy to understand: :P

Code:
#!/bin/bash
ARRAY=( $(ls -1 *.jpg) )
FIRST=0
LAST=$(( ${#ARRAY[@]} -1 ))
until [ "$FIRST" -ge "$LAST" ];do
  mv ${ARRAY[$FIRST]} ${ARRAY[$LAST]}.tmp 
  mv ${ARRAY[$LAST]} ${ARRAY[$FIRST]}.tmp 
  LAST=$(( --LAST ))
  FIRST=$(( ++FIRST ))
done
rename .tmp '' *.tmp
 
Old 12-06-2007, 06:59 AM   #18
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682
Quote:
Originally Posted by /bin/bash View Post
If you like your code un-obfuscated and easy to understand: :P

Code:
#!/bin/bash
ARRAY=( $(ls -1 *.jpg) )
FIRST=0
LAST=$(( ${#ARRAY[@]} -1 ))
until [ "$FIRST" -ge "$LAST" ];do
  mv ${ARRAY[$FIRST]} ${ARRAY[$LAST]}.tmp 
  mv ${ARRAY[$LAST]} ${ARRAY[$FIRST]}.tmp 
  LAST=$(( --LAST ))
  FIRST=$(( ++FIRST ))
done
rename .tmp '' *.tmp
Code:
ARRAY=( $(ls -1 *.jpg) )
The ls isn't needed as fileglobbing will do it. ( and you are using file globbing anyway with *.jpg).

Also, he just wants to process files with the pattern IMG*.jpg. *.jpg will process all *.jpg files.

The "s" in "files" indicates plurality. But I probably should have used "pictures" instead of file. Using the word "file" has become a habit. I use it for oneliners in for loops working in the shell as well.

Using LAST in place of ${#ARRAY[@]} is a good idea for readability but the form of the for loop I used resembles C and is given if you enter "help for". I don't think using $LAST in the loop would precompute the value of LAST however. That would be the case in C. However, the name "LAST" indicates that it is the last element. Then you decrement it. That adds confusion it itself.
The letter "n" is commonly used to indicate an index. The ${#files[@]} was the only obfuscated part and should have been commented in the code.

Perhaps I should have added comments to the code instead of putting them at the end of the message.

If I have a saving grace, it's that I created a bunch of files matching the filepattern and tested the program before posting. Instead of images, IMG1000.jpg contained the text "IMG1000.jpg". So after the renaming, I cat'ed out the contents of the files to prove that the reordering took place.

Last edited by jschiwal; 12-06-2007 at 07:21 AM.
 
Old 12-06-2007, 01:20 PM   #19
/bin/bash
Senior Member
 
Registered: Jul 2003
Location: Indiana
Distribution: Mandrake Slackware-current QNX4.25
Posts: 1,802

Rep: Reputation: 47
The ls isn't needed as fileglobbing will do it. ( and you are using file globbing anyway with *.jpg).
Just part of the un-obfuscating. Which is more easy to understand?
ARRAY=( $(ls -1 *.jpg) )
ARRAY=( *.jpg )
Maybe just a mater of preference. And I thought ls -1 IMG[0-9][0-9][0-9][0-9].jpg just looked ugly.

The "s" in "files" indicates plurality. But I probably should have used "pictures" instead of file. Using the word "file" has become a habit. I use it for oneliners in for loops working in the shell as well.

I used ARRAY because I was creating an ARRAY. I have no rhyme or reason for the things I do.

If I have a saving grace, it's that I created a bunch of files matching the filepattern and tested the program before posting. Instead of images, IMG1000.jpg contained the text "IMG1000.jpg". So after the renaming, I cat'ed out the contents of the files to prove that the reordering took place.
Yeah I did the exact same thing only mine were named like my camera names them (which explaines the pic000.img. Funny thing is the middle file never gets changed

BTW: I was joking and referring to the perl script, because as everyone knows nothing beats perl for obfuscating!
 
Old 12-06-2007, 08:15 PM   #20
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682
Using the ls is like preceding grep or sed with cat. It isn't un-obfuscating, it's a bad habit.

I'll compromise on the ARRAY, but it shouldn't be in capital letters because capital letters are used in bash to indicate constants. A name like picture_array or pic_array would be better. It communicated the type and what the elements are.

Ditto for FIRST and LAST. It looked strange when you started changing them.

Last edited by jschiwal; 12-06-2007 at 08:21 PM.
 
Old 12-07-2007, 05:20 AM   #21
/bin/bash
Senior Member
 
Registered: Jul 2003
Location: Indiana
Distribution: Mandrake Slackware-current QNX4.25
Posts: 1,802

Rep: Reputation: 47
  1. You really need to chill.
  2. Please don't attempt to correct my scripting style (old dog new trick sort of thing.)
  3. Have a good day.
<mental note> Human race becoming more humorless and up tight.</mental note>
 
Old 12-07-2007, 07:40 AM   #22
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
Quote:
BTW: I was joking and referring to the perl script, because as everyone knows nothing beats perl for obfuscating!
only for people who don't speak perl
 
Old 12-07-2007, 08:29 AM   #23
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Quote:
Originally Posted by jschiwal View Post
Using the ls is like preceding grep or sed with cat. It isn't un-obfuscating, it's a bad habit.

I'll compromise on the ARRAY, but it shouldn't be in capital letters because capital letters are used in bash to indicate constants. A name like picture_array or pic_array would be better. It communicated the type and what the elements are.

Ditto for FIRST and LAST. It looked strange when you started changing them.

actually, it doesn't really matter. You can keep telling people not to use cat because its UUOC, or whatever, in the end, people will still UUOC. Even books that are published (yes , I saw one just now at the bookshop using UUOC ) You can tell people not to use capital letters for whatever reasons, but people will still use it. Its just a matter of choice, when that choice is presented to them. If bash could restrict the use of capital letters just for constants, then people won't be able to use capital letters as variables. but the bash people who created bash doesn't really think about that...you can tell people that Perl is the king of obfuscation, but people still use them, cause once again, the key to this => choice.

Last edited by ghostdog74; 12-07-2007 at 08:34 AM.
 
Old 12-08-2007, 05:22 AM   #24
/bin/bash
Senior Member
 
Registered: Jul 2003
Location: Indiana
Distribution: Mandrake Slackware-current QNX4.25
Posts: 1,802

Rep: Reputation: 47
only for people who don't speak perl
I once studied a perl script to see if I could learn from it. I learned that perl is a "self encrypting language!" :P
 
Old 12-08-2007, 06:00 PM   #25
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,355

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
People will ultimately do what they want (assuming it's syntactically legal), but it doesn't hurt to (gently) encourage them to do it better, especially if there's a danger of them doing it in prod where it will confuse/annoy everbody else.
There is a such a concept as 'best practice'.
 
Old 12-10-2007, 03:56 AM   #26
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
Quote:
I learned that perl is a "self encrypting language!" :P
rubbish! you just need to be a retentive weirdo freak to enjoy it!

 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
mass renaming icons for use with XFE? Mr_Shameless Linux - Software 2 06-02-2007 05:55 AM
Bash - Mass file renaming problem smudge|lala Linux - Software 2 02-14-2007 06:02 PM
mass mp3 directory renaming schbond Linux - General 4 01-05-2007 11:18 AM
a question about renaming files in mass! zeltak Linux - Software 1 05-24-2006 06:12 AM
Simple Renaming Script Boffy Programming 5 08-12-2005 09:07 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:03 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration