In C shell How do I get the folder name using a foreach loop

In C shell How do I get the folder name using a foreach loop
wademiAsked:
Who is Participating?
 
arnoldCommented:
1) you are only providing a single element in the foreach loop which is the value from the $SourceDir variable which is /home/milligram/mnt.
2) you are outputing the wrong variable $SourceDir versus the variable to which foreach will assign values which is $directory.

Your options are either to change into the $SourceDir location and run the script as I outlined or replace $SourceDir `ls $SourceDir`which means that you will have to prepend "$SourceDir/" to $directory if you want to perform any test/action on the returned data since you can not assume that the location from which you are executing this script will be in the $SourceDir. When the script is run outside the $SourceDir location, you will get file/directory not found errors.

#! /bin/csh -f
set SourceDir = /home/milligram/mnt
set DestDir_IOS = Myvideos/IOS
chdir $SourceDir

foreach directory (`ls`)

test -d "$SourceDir/$directory" && echo "$SourceDir/$directory"

end
0
 
arnoldCommented:
foreach directory (`ls`)
test -d "$directory" && echo "This is a directory: $directory"
end
0
 
wademiAuthor Commented:
This is what I had before:

#! /bin/csh -f
set SourceDir = /home/milligram/mnt
set DestDir_IOS = Myvideos/IOS


foreach directory ($SourceDir)

echo $SourceDir

end


can you corect it for me. Thanks a million!!!!!!!!!
0
 
wademiAuthor Commented:
Can you explain

what is the 'ls' for?
 what does the "test" do? is it a cshell syntax?
Can variables be placed in quotes.."$directory"


foreach directory (`ls`)
test -d "$directory" && echo "This is a directory: $directory"
end
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.