LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
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 02-14-2008, 06:34 AM   #1
geeyathink
Member
 
Registered: Apr 2006
Distribution: Mandy 2007, Kubuntu
Posts: 56

Rep: Reputation: 15
mv in for loop stops after first file it comes to


I saw this in a post:

Code:
#You can also use variable substitution:
#for file in *.jpg; do
#mv "$file" "${file//b/_}"
#done
And wanted to try to use it because; A. It looks simple, and B. I think its straight Bash as opposed to Perl or something.

I did this with it:

Code:
#!/bin/bash
exec 2>/tmp/script1.log
set -x

grt=`ls  /home/teabear/desktop/underfiles`

 for file in /home/teabear/desktop/underfiles/$grt  ; do
  mv "$file" "${file//_files/ }"

 done


This moves (renames) only the first folder or file it comes to in the "underfiles" folder. After that the path is lost and mv complains "cannot stat, no such file or directory" for the remaining stuff that is in the "underfiles" folder.

Here is the log file from `exec 2>'

Code:
+++ ls /home/teabear/desktop/underfiles
++ grt='b_files
d_files'
++ for file in '/home/teabear/desktop/underfiles/$grt'
++ mv /home/teabear/desktop/underfiles/b_files '/home/teabear/desktop/underfiles/b '
++ for file in '/home/teabear/desktop/underfiles/$grt'
++ mv d_files 'd '
mv: cannot stat `d_files': No such file or directory
What do I need to understand to go through and rename with mv, all the files in the "underfiles" folder, instead of just the first file it comes to?
Should my question be: "How do I supply the path for $grt to all the iterations in the for loop?"

Thank you for any help.
 
Old 02-14-2008, 06:58 AM   #2
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
The path to a variable does not make sense. When you create a variable in a shell it is available regardless of what directory you are in.

In you example, suppose that $grt = a b c. The for loop sees "/home/teabear/desktop/underfiles/a b c" This is seen as 3 filenames, but only one with a complete path.

Try:

for file in /home/teabear/desktop/underfiles/*; do....etc.
 
Old 02-14-2008, 07:34 AM   #3
geeyathink
Member
 
Registered: Apr 2006
Distribution: Mandy 2007, Kubuntu
Posts: 56

Original Poster
Rep: Reputation: 15
Thank you pixellany,

I don't know how I got off on the "path in a variable" tangent, but you have me straightened out on that. Works great.

While on the topic, I think in other parts of the script I am working on I either used or am trying to use the "path in a variable" idea just so I don't have to type as much.

Is that an always major no-no or is it ok at some times?

Thanks again for the help.
 
Old 02-14-2008, 08:09 AM   #4
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
I thought I heard "path TO a variable".....What goes IN a variable is anything you want. The trick is how a variable gets interpreted in the context in which it is used.

I'm not that swift at understanding all the rules---I usually just try things until I find what works. (I would probably not be able to make a living at programming....)
 
Old 02-14-2008, 11:18 PM   #5
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,358

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
Like he said, you just have to think carefully (or echo out) what you have in your variable (which can be anything you like).
A common thing to remember is that the find cmd tends to prefix all the 'filenames' with the path when it returns the files matched, whereas bare ls doesn't (but note ls -R will .. sort of)
 
  


Reply



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
how to loop over text file lines within bash script for loop? johnpaulodonnell Linux - Newbie 9 07-28-2015 03:49 PM
File download says done and stops before file is completely downloaded in Firefox royeo Linux - General 2 12-24-2006 10:02 PM
tmpfs-like loop file? generic_user Linux - General 1 09-17-2006 01:50 AM
Geting error loop QFike:getch: File not open QFile:atEnd: this file is not open badgerbox76 Linux - Newbie 6 01-07-2006 05:30 PM
Install stops at "Calibrating delay loop" eam Linux - General 0 07-23-2004 07:39 PM

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

All times are GMT -5. The time now is 02:08 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