LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
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 08-13-2012, 06:43 AM   #1
devaj
LQ Newbie
 
Registered: Feb 2009
Posts: 17

Rep: Reputation: 2
Bash noob: Not able to make bash for loop interactive


Hi,
I have been trying to get around renaming a couple of files in the using the bash for loop.However I am not able to make any command passed to for loop interactive whether I am root or not.
Here is the code:

Code:
for i in $(ls);do mv -i "$i" "$i.not";done
Same for removing :

Code:
for i in $(ls);do rm -i $i;done
I do know there are other ways to achieve the same result,but I would just like to know why does the for loop act like this.
 
Old 08-13-2012, 07:42 AM   #2
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192
Tested the rm option here and it worked fine. My only change is that using ls is a complete no no, so try:
Code:
for i in *; do rm -i "$i"; done
 
1 members found this post helpful.
Old 08-13-2012, 07:49 AM   #3
rosehosting.com
Member
 
Registered: Jun 2012
Location: Missouri, USA
Posts: 236

Rep: Reputation: 64
This should work

Code:
#!/bin/bash
for i in $(find /path/to/files/ -type f); do
    read -p "Move $i to ${i}.bak (y/n)" yes_no
    [ "$yes_no" == "y" ] && mv $i ${i}.bak
done
 
Old 08-13-2012, 08:19 AM   #4
H_TeXMeX_H
LQ Guru
 
Registered: Oct 2005
Location: $RANDOM
Distribution: slackware64
Posts: 12,928
Blog Entries: 2

Rep: Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301
See 'man rename', or provide example.

You can just do 'rm -i *'.
 
Old 08-13-2012, 08:52 AM   #5
devaj
LQ Newbie
 
Registered: Feb 2009
Posts: 17

Original Poster
Rep: Reputation: 2
Thank you all for replying.
I do know there are various other ways to renaming or removing a file (eg:the above method by H_TEXMEX_H or via awk,sed).What I really would like to know is when I use mv -i in the terminal,it prompts me for my input,but if I use mv -i in a for loop,it doesn't prompt me,not even once.Could anyone please tell me why does bash do this,the reason behind bash's behaviour.

P.S:Thank you Grail for letting me know that I shouldn't be using ls for input.Shall keep that in mind.May I ask you why exactly should you not use ls for input.

Last edited by devaj; 08-13-2012 at 09:12 AM.
 
Old 08-13-2012, 10:08 AM   #6
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192
Firstly, the answer to your issue with mv. You need to read the man page a little closer (I had to too ). The '-i' option for mv is only interactive if the file you are moving
to already exists:
Code:
$ ls
f1  f2
$ for i in *; do mv -i $i $i.old; done
$ ls
f1.old  f2.old
$ touch f1 f2
$ ls
f1  f1.old  f2  f2.old
$ for i in *; do mv -i $i $i.old; done
mv: overwrite `f1.old'? n
mv: overwrite `f2.old'? n
$ ls
f1  f1.old.old  f2  f2.old.old
So the things to notice here are:

1. When the destination name does not exist there is no prompting
2. Due to step 1, the final results are probably not what was expected
Quote:
May I ask you why exactly should you not use ls for input.
Always happy to answer questions See the following link for answers:

http://mywiki.wooledge.org/ParsingLs
 
1 members found this post helpful.
Old 08-13-2012, 10:42 AM   #7
devaj
LQ Newbie
 
Registered: Feb 2009
Posts: 17

Original Poster
Rep: Reputation: 2
Thank you grail.I got more than I asked for.You Sir, ROCK
Shall be marking this thread solved.
GOD bless all.

Last edited by devaj; 08-13-2012 at 10:43 AM.
 
  


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
[SOLVED] Bash - While Loop reading from two lists simultaneously - nested while loop wolverene13 Programming 11 10-01-2011 05:00 PM
Make a bash script loop from start evansd321 Linux - Newbie 5 08-29-2008 11:37 AM
bash script works when interactive, endless loop when started via cron dguy Linux - General 5 04-10-2006 11:39 AM
make bash interactive and change prompt DropSig Linux - Newbie 3 05-17-2004 08:16 AM

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

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