[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

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

Posted on 2011-10-27
4
Medium Priority
?
406 Views
Last Modified: 2012-08-13
In C shell How do I get the folder name using a foreach loop
0
Comment
Question by:wademi
  • 2
  • 2
4 Comments
 
LVL 81

Expert Comment

by:arnold
ID: 37042560
foreach directory (`ls`)
test -d "$directory" && echo "This is a directory: $directory"
end
0
 

Author Comment

by:wademi
ID: 37042572
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
 

Author Comment

by:wademi
ID: 37042585
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
 
LVL 81

Accepted Solution

by:
arnold earned 2000 total points
ID: 37042605
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

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

My attempt to use PowerShell and other great resources found online to simplify the deployment of Office 365 ProPlus client components to any workstation that needs it, regardless of existing Office components that may be needing attention.
In the absence of a fully-fledged GPO Management product like AGPM, the script in this article will provide you with a simple way to watch the domain (or a select OU) for GPOs changes and automatically take backups when policies are added, removed o…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
Loops Section Overview
Suggested Courses

872 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question