LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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 03-24-2005, 12:47 PM   #1
AC97Conquerer
Member
 
Registered: Nov 2004
Location: US
Distribution: Debian Sid
Posts: 50

Rep: Reputation: 15
BASH Shell Scripting -- foreach file in folder???


I've got this:
Code:
#!/bin/bash

foreach file _BadMaps
	echo "$file"
end
But I get this as error
Code:
Shiva:~/.ut2004# ./mapchecker.sh
./mapchecker.sh: line 3: foreach: command not found

./mapchecker.sh: line 5: end: command not found
So how would I make it so that it works lol? Do you recommend tcsh?
 
Old 03-24-2005, 12:50 PM   #2
95se
Member
 
Registered: Apr 2002
Location: Windsor, ON, CA
Distribution: Ubuntu
Posts: 740

Rep: Reputation: 32
Code:
#!/bin/bash

for file in ${_BadMaps[*]}
do
	echo "$file"
end

Last edited by 95se; 03-24-2005 at 12:54 PM.
 
Old 03-24-2005, 01:13 PM   #3
AC97Conquerer
Member
 
Registered: Nov 2004
Location: US
Distribution: Debian Sid
Posts: 50

Original Poster
Rep: Reputation: 15
NVM I got it!! yee hah!

Code:
#!/bin/bash

# Make sure the folder exists!
if [ -d _BadMaps ] ; then
	echo "Good!"
else
	echo "JESUS NNOO!!!!!!"
	exit 1
fi

# Make sure there is at least one file in there
if [ -f _BadMaps/DM-MetalHell.ut2 ] ; then
	echo "Good!"
else
	echo "GOD NNOO!!!!!!"
	exit 1
fi

# Echo out all files in directory!
for file in _BadMaps/*.ut2 ; do
	echo "$file"
done
I get this (no files in directory):
Code:
Shiva:~/.ut2004# ./mapchecker.sh
Good!
Good!
_BadMaps/CTF-(2K4)AcidPipeXmas.ut2
_BadMaps/CTF-Philae_Nightfall.ut2
_BadMaps/CTF-Q3dm18[SpaceChamber].ut2

Last edited by AC97Conquerer; 03-24-2005 at 01:26 PM.
 
Old 03-24-2005, 01:28 PM   #4
gbonvehi
Senior Member
 
Registered: Jun 2004
Location: Argentina (SR, LP)
Distribution: Slackware
Posts: 3,145

Rep: Reputation: 51
Try
Code:
for file in _BadMaps/*; do
	echo "$file"
done
 
Old 03-24-2005, 01:33 PM   #5
AC97Conquerer
Member
 
Registered: Nov 2004
Location: US
Distribution: Debian Sid
Posts: 50

Original Poster
Rep: Reputation: 15
Oohhh man! How could I convert this string:
"_BadMaps/ONS-Jumpmonkey.ut2" to
"Maps/ONS-Jumpmonkey.ut2"

Because I need to move the file from _BadMaps to Maps!
Code:
# Move all _BadMaps folder files to _GoodMaps directory 
for file in _BadMaps/*.ut2 ; do
	echo "mv $file _GoodMaps/$file"
done
I get this:
Code:
mv _BadMaps/ONS-Eire.ut2 _GoodMaps/_BadMaps/ONS-Eire.ut2
---EDIT---
Thank you gbonvehi

Last edited by AC97Conquerer; 03-24-2005 at 01:35 PM.
 
Old 03-24-2005, 01:39 PM   #6
gbonvehi
Senior Member
 
Registered: Jun 2004
Location: Argentina (SR, LP)
Distribution: Slackware
Posts: 3,145

Rep: Reputation: 51
When you append a name you're actually "renaming" the file, try: "mv ./_BadMaps/$file ./_GoodMaps/"

Last edited by gbonvehi; 03-24-2005 at 01:40 PM.
 
Old 03-24-2005, 02:48 PM   #7
AC97Conquerer
Member
 
Registered: Nov 2004
Location: US
Distribution: Debian Sid
Posts: 50

Original Poster
Rep: Reputation: 15
The ./ isn't helping

Code:
# Move all _BadMaps folder files to _GoodMaps directory
for file in _BadMaps/*.ut2 ; do
	echo "mv ./_BadMaps/$file ./_GoodMaps/$file"
done
Code:
mv ./_BadMaps/_BadMaps/ons-Magma1.ut2 ./_GoodMaps/_BadMaps/ons-Magma1.ut2
mv ./_BadMaps/_BadMaps/ons-betamap2.ut2 ./_GoodMaps/_BadMaps/ons-betamap2.ut2
 
Old 03-24-2005, 05:49 PM   #8
gbonvehi
Senior Member
 
Registered: Jun 2004
Location: Argentina (SR, LP)
Distribution: Slackware
Posts: 3,145

Rep: Reputation: 51
I said that you should remove the $file at the end of GoodMaps.. like...
"mv _BadMaps/$file _GoodMaps/"
 
Old 03-24-2005, 06:48 PM   #9
AC97Conquerer
Member
 
Registered: Nov 2004
Location: US
Distribution: Debian Sid
Posts: 50

Original Poster
Rep: Reputation: 15
Oh doh! Thank you lol
 
Old 03-25-2005, 01:35 AM   #10
AC97Conquerer
Member
 
Registered: Nov 2004
Location: US
Distribution: Debian Sid
Posts: 50

Original Poster
Rep: Reputation: 15
PERFECT, here's the final script and the part I was working on, I don't need it after I reinstalled UT2004, because all of the maps don't give me the ReadFile EOF lol:

Code:
#!/bin/bash
#
# MAKE SURE THE MAPS GET SENT WHERE THEY SHOULD
# /root/.ut2004/_ProcBad OR /root/.ut2004/_ProcGood
# CREATED BY a Psycho All Rights Reserved
#


#
# TEST MAP
# 
testMap()
{
	#echo "Currently working on $currentMap" 

	UT2004_DATA_PATH='/root/.ut2004/System'
	cd '/root/.ut2004/System/'
	./ut2004-bin > /home/kleid/Programming/Laboratory/loggit.log &
	sleep 11
	killall ut2004-bin
	sleep 2
	
	# Does the map have an error? 
	#cat /home/kleid/Programming/Laboratory/loggit.log | grep -ic 'Error\|EOF'
	if [ `cat /home/kleid/Programming/Laboratory/loggit.log | grep -ic 'EOF\|Error'` = 0 ] ; then
		echo "$currentMap is a good map"
		mv -uf /root/.ut2004/Maps/$currentMap /root/.ut2004/_ProcGood/$currentMap
	else
		echo "$currentMap is a bad map"
		mv -uf /root/.ut2004/Maps/$currentMap /root/.ut2004/_ProcBad/$currentMap
	fi
}



# Make sure the folder exists! 
if [ -d /root/.ut2004/_BadMaps ] ; then
	echo "Good!"
else
	echo "JESUS NNOO!!!!!!"
	exit 1
fi

mfiles=`ls -1 /root/.ut2004/_BadMaps`
counter=1
for i in $mfiles ; do
	echo "Testing map {$i}..."
	mv /root/.ut2004/_BadMaps/$i /root/.ut2004/Maps/$i
	currentMap="$i"
	testMap
	
	counter=$(($counter+1))
	
done

echo 'MAP SYNC COMPLETE'
 
Old 03-18-2009, 02:30 AM   #11
VectorThorn
LQ Newbie
 
Registered: Nov 2006
Location: Hell's Gate
Distribution: Fc10
Posts: 19

Rep: Reputation: 0
Thumbs up

Thanx for the posts guys. You saved me about 2 to 4 hours of work . I was reading through the BASH manual on gnu.org and the way they put it together made reading it worthless to those who are not already advanced users; it is better suited as a reference manual for experts.

Last edited by VectorThorn; 03-18-2009 at 02:35 AM.
 
Old 07-06-2011, 12:25 AM   #12
arvind.k
LQ Newbie
 
Registered: Jul 2011
Posts: 1

Rep: Reputation: Disabled
Thumbs up move files from one folder to other folder using shell

This script used to move files from one folder to other folder
using shell....

#!/usr/bin/bash

for file in <source folder>* ;
do
mv $file <destination folder>/
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 Shell script : copying a file to multiple folder zamri Programming 14 04-29-2008 10:27 AM
Bash shell scripting Sco Linux - Newbie 1 11-09-2004 11:58 AM
Help with I/O on bash shell scripting Dave6383 Programming 1 06-03-2004 05:24 PM
Bash Shell Scripting Help Tangerine Programming 6 05-06-2003 02:10 PM
BASH Shell scripting help ewarmour Linux - General 8 05-25-2002 07:10 AM


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

Main Menu
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