LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 11-21-2011, 05:16 PM   #1
me.
LQ Newbie
 
Registered: Nov 2011
Posts: 4

Rep: Reputation: Disabled
help with counting variables


HI have here script that should copy file by file to different directory
but I think I messed with the counter, help figuring out appreciated.

Code:
#!/bin/bash
dir=/home/andre/Documents/Scripts/Copy
files=$(pwd | ls -m )
counter= #counter set to number of files counted with wc -c
numberfiles=$(ls -m | wc -w )

echo " files in these folder are $files "
if [ ! -d $dir ]; then
mkdir Copy
else
echo " $dir exists so now will copy files "
fi

while [ $counter -lt $numberfies ];
do
cp ${file}s ${dir}
let counter=counter+1
until [ $counter=numberfiles ] do
shift 1

#cp $files $dir/$files
#echo " $files has been copied "
 
Old 11-21-2011, 06:37 PM   #2
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,516

Rep: Reputation: 2893Reputation: 2893Reputation: 2893Reputation: 2893Reputation: 2893Reputation: 2893Reputation: 2893Reputation: 2893Reputation: 2893Reputation: 2893Reputation: 2893
Is this homework? Neither of your loops are terminated and this appears to be the really long way to do this. You also make assumptions that you are in the correct
directory when executing it.

Maybe answer some of the above first as I do not wish to send you on a different angle if you are trying to learn some of the above concepts first.
 
Old 11-22-2011, 01:51 AM   #3
me.
LQ Newbie
 
Registered: Nov 2011
Posts: 4

Original Poster
Rep: Reputation: Disabled
hi gral,

yes it is for my home network,
these loops are not terminated I know I just come up with ideas which doesnt work,
but I think you understand what I am trying to achieve and hopefully you can give me solution, an easy, short solution.

I want copy file by file and have it displayed...
 
Old 11-22-2011, 03:06 AM   #4
salemhouda
LQ Newbie
 
Registered: Nov 2011
Posts: 8

Rep: Reputation: Disabled
In the end of the loop you miss done.
Code:
while [ condition ]
do
   command1
   command2
   command3
done
Code:
  #!/bin/bash 
         COUNTER=20
         until [  $COUNTER -lt 10 ]; do
             echo COUNTER $COUNTER
             let COUNTER-=1
         done
 
Old 11-23-2011, 07:10 AM   #5
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1960Reputation: 1960Reputation: 1960Reputation: 1960Reputation: 1960Reputation: 1960Reputation: 1960Reputation: 1960Reputation: 1960Reputation: 1960Reputation: 1960
Code:
files=$(pwd | ls -m )
Um, what? Trying to pipe the output of pwd into ls? ls doesn't read from stdin, so this is pointless. Oh, and since the -m option adds a comma to each entry, any subsequent command that tries to read the name will probably fail to find it. Perhaps you want ls "$PWD" instead?

But in any case, this isn't a good way to go about it. first of all, Parsing ls for filenames is generally a bad idea. Try using simple globs instead. Second, when working on lists. you should be storing them in arrays rather than scalar variables.

Since arrays are indexed by number, it also becomes much easier to count and track them without the need for external tools like wc.

Code:
cp ${file}s ${dir}
${file}s simply takes the undefined variable "file" and adds "s" to it, so you get the output "s". I'm assuming this is a typo?

But in addition, always quote your variable expansions, unless you want word-splitting to occur. It's vital in scripting to understand exactly how the shell handles arguments and whitespace:
http://mywiki.wooledge.org/Arguments
http://mywiki.wooledge.org/WordSplitting
http://mywiki.wooledge.org/Quotes

Although as written, your script actually depends on the variable "files" being word-split so that cp can grab the individual filenames. The first name with a space in it is going to mess it up. Again, use an array instead.

Finally, learn the value of clear formatting and commenting. Lining things up in neat blocks would help you notice and avoid simple syntax problems like missing terminators.

Edit: One more.
Code:
until [ $counter=numberfiles ] do
Check out bash pitfalls 4, 7, and 10 to understand what's wrong here.

http://mywiki.wooledge.org/BashPitfalls

Last edited by David the H.; 11-23-2011 at 08:04 AM. Reason: small change and addition
 
  


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
Counting the number of exit variables - Bash. 0bfuscated Programming 3 07-02-2010 11:43 AM
Bash Script: parse active process stderr, strip, dump into variables, use variables TimeFade Programming 1 02-13-2010 06:09 AM
LXer: C/C++ reference counting with atomic variables and gcc LXer Syndicated Linux News 0 05-27-2009 02:11 PM
having probrems counting variables carl0ski Linux - General 2 01-30-2006 04:40 AM
Threads synchronisation problem (mutex variables and contitional variables) zahadumy Programming 6 12-07-2005 12:30 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 11:27 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration