Solved

How to execute shell script to several files in Windows Command Prompt?

Posted on 2015-01-14
11
225 Views
Last Modified: 2015-01-15
I have a shell script to check blank files. Is it possible for the script to be executed to several files in a folder? Thank you.
0
Comment
Question by:skyberrie
  • 4
  • 4
  • 3
11 Comments
 
LVL 84

Expert Comment

by:ozo
ID: 40550080
for f in several files ; do script $f ; done
0
 
LVL 48

Expert Comment

by:Tintin
ID: 40550187
Your title mentions Windows, but your topic is in Linux.

Which OS are you wanting to run this on?
0
 

Author Comment

by:skyberrie
ID: 40550202
Thanks ozo and Tintin. I'm running .sh in Windows CMD, and I'm a newbie to this. May I know what's the command to type in CMD for a written script to be executed in a folder consisting several files? Thanks!
0
 
LVL 48

Expert Comment

by:Tintin
ID: 40550234
So you are running a Unix shell under Windows or just the standard Windows command prompt?
0
 

Author Comment

by:skyberrie
ID: 40550237
Yes, I'm running cmd.exe in Windows
0
U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

 
LVL 48

Expert Comment

by:Tintin
ID: 40550259
In that case, in your batch file, you'll have something like:

for %%f in (.\*) do @echo %f
0
 

Author Comment

by:skyberrie
ID: 40550676
Thanks. The .sh script contains the following codes where the FileName has been defined earlier:

if [ -f $FileName ]; then      
echo "The file exists"
else
  echo "The file dosn't exist"
./Blank $year $month $mday $hour $min
fi

So, I wonder what's the command to type in CMD to be executed to a folder.
0
 
LVL 84

Assisted Solution

by:ozo
ozo earned 250 total points
ID: 40550702
That .sh script looks like it was written in the same shell language that the answer in http:#a40550080 was written in, so that command could work.

But one issue may be that if you execute it on a folder (directory) it will say it does not exist, because
-f FILE means FILE exists and is a regular file
If you really want to check for existence, regardless of whether it exists as a regular file (-f), a directory (-d), a socket (-S), a symbolic link (-L),  a character special (-c), etc., then you should check -e $FileName
Another issue is that the script does not say which file exists, so when you run it on several files, you won't know what it is referring to when it reports "The file exists" or "The file dosn't exist"

for FileName in * ; do script.sh $FileName ; done
would run the script for all files the current folder,
or
for FileName in folder/* ; do script.sh $FileName ; done
would run it for all files in the specified folder,
but that seems useless because * should only match names that exist (although some of them may not be regular files)

Your reference to "blank" files could also suggest you might have preferred to check -s, which means that the file exists and has a size greater than 0
0
 

Author Comment

by:skyberrie
ID: 40550781
Thank you very much for your explanation. I'm running this command in the current directory (for FileName in * ; do script.sh $FileName ; done), but it returns "FileName was unexpected in this time".
0
 
LVL 84

Expert Comment

by:ozo
ID: 40550795
I don't see anything in http:#a40550676 that returns "FileName was unexpected in this time".
Is there another part of the script you left out?
0
 
LVL 48

Accepted Solution

by:
Tintin earned 250 total points
ID: 40552346
You can't run a Unix/Linux shell script under Windows unless you have a Unix shell installed.
0

Featured Post

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
route 2 traffic streams on single NIC 6 34
linux installs 6 47
AD LDAP LDS 3 47
How to Remove files with a Date in the Filename with Linux Scripting 3 22
Little introduction about CP: CP is a command on linux that use to copy files and folder from one location to another location. Example usage of CP as follow: cp /myfoder /pathto/destination/folder/ cp abc.tar.gz /pathto/destination/folder/ab…
Linux users are sometimes dumbfounded by the severe lack of documentation on a topic. Sometimes, the documentation is copious, but other times, you end up with some obscure "it varies depending on your distribution" over and over when searching for …
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:

930 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now