LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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-20-2004, 08:28 AM   #1
drisay
Member
 
Registered: Sep 2004
Distribution: Slackware 10
Posts: 167

Rep: Reputation: 30
bash programming


hi all,

i'm currently trying to teach myself bash. in the script i'm trying to rename all files from IMG_00xx.JPG to PIC_00xx.JPG. Can anyone spot the error.

Note: I was reading up on sed on a few web sites and this seems to be the way they assign the result to a variable... but obviously it's not working. The output of tmpvar is simply "$i | sed "s/IMG/PIC/"".

script:
Code:
#!/bin/bash

tmpvar=""

for i in $(ls *.JPG); do
  tmpvar='echo $i | sed "s/IMG/PIC/"'
  mv $i $tmpvar
done;

Result:
$ . pics.sh
mv: when moving multiple files, last argument must be a directory
Try `mv --help' for more information.
mv: when moving multiple files, last argument must be a directory
Try `mv --help' for more information.
mv: when moving multiple files, last argument must be a directory
Try `mv --help' for more information.
mv: when moving multiple files, last argument must be a directory
Try `mv --help' for more information.
mv: when moving multiple files, last argument must be a directory
Try `mv --help' for more information.

Thanks in advance,
drisay.
 
Old 12-20-2004, 08:32 AM   #2
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,415

Rep: Reputation: 1968Reputation: 1968Reputation: 1968Reputation: 1968Reputation: 1968Reputation: 1968Reputation: 1968Reputation: 1968Reputation: 1968Reputation: 1968Reputation: 1968
you're using quotes and not backticks, so rather than tmpvar being the output of that command, it IS that command....

so it's
Code:
tmpvar=`echo.....`
rather than
Code:
tmpvar='echo...'
 
Old 12-20-2004, 08:40 AM   #3
drisay
Member
 
Registered: Sep 2004
Distribution: Slackware 10
Posts: 167

Original Poster
Rep: Reputation: 30
wow, ok thanks. i never would have found that.
 
Old 12-20-2004, 09:19 AM   #4
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: FreeBSD, Debian, Mint, Puppy
Posts: 3,314

Rep: Reputation: 175Reputation: 175
or, the new way is


Code:
tmpvar=$(   echo $i | sed "s/IMG/PIC/"     )
 
Old 12-20-2004, 09:25 AM   #5
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,415

Rep: Reputation: 1968Reputation: 1968Reputation: 1968Reputation: 1968Reputation: 1968Reputation: 1968Reputation: 1968Reputation: 1968Reputation: 1968Reputation: 1968Reputation: 1968
that echo and pipe always bugs me... would be nicer to be able to give the variable as a parameter to sed in the first place really.
 
Old 12-20-2004, 09:52 AM   #6
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: FreeBSD, Debian, Mint, Puppy
Posts: 3,314

Rep: Reputation: 175Reputation: 175
true, true....

could do:
Code:
ls -1 *.jpg|sed -n '
s/\.jpg/\.IMG/g
s/.*/& &/
s/\.IMG/\.jpg/
p' |  xargs -n2 cp
or better:

Code:
ls -1 *.jpg|sed -n '
s/\.jpg/\.IMG/g
s/.*/& &/
s/\.IMG/\.jpg/
p'     > ~/1
give the output an eyeball
to check it's ok then do

. ~/1

so you've got a temp record too.

billy
 
Old 12-20-2004, 10:09 AM   #7
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: FreeBSD, Debian, Mint, Puppy
Posts: 3,314

Rep: Reputation: 175Reputation: 175
OR, best of all:

Code:
for file in *.jpg
do  
    echo  $file ${file%.jpg}.IMG
done
 
  


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
Bash programming winterhunter Linux - Newbie 6 11-26-2007 08:14 AM
Bash Programming moodupani Programming 1 08-31-2005 07:50 PM
bash programming alaios Programming 2 07-23-2005 01:52 AM
bash programming cpukiller Linux - Newbie 1 11-04-2003 07:45 PM
bash programming bulliver Programming 2 04-05-2003 10:30 PM


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

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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration