f



Unifying files and no files.

Is there an idiom that folds the no-files case into the files-exist case so
they can be handled with a single mechanism?

  $ bash --version
  GNU bash, version 3.1.17(1)-release (i486-pc-linux-gnu)
  Copyright (C) 2005 Free Software Foundation, Inc.

  $ ls

  $ chmod a-x *
  chmod: cannot access `*': No such file or directory

  $ 

I could do this

  $ chmod a-x * 2>/dev/null

  $ 

but that just hides the error; there are still two cases (the exit status tells
the tale).  I could do this

  $ chmod a-x * 2>/dev/null ; :

  $ 

but that's more hiding.  This works

  $ files=$(find . -type f)

  $ [ -z "$files" ] || chmod a-x $files

  $ 

and is, I suppose, good documentation, but it's too much machinery.  This
problem is similar to handling zero-iteration loops in programs, but it's
irratating that

  $ for i in * ; do chmod a-x $i ; done
  chmod: cannot access `*': No such file or directory

  $ 

doesn't work.  Anything else?
0
rvclayton (11)
3/21/2008 3:33:45 AM
comp.unix.shell 15484 articles. 3 followers. Post Follow

4 Replies
1179 Views

Similar Articles

[PageSpeed] 18

R. Clayton wrote:
> Is there an idiom that folds the no-files case into the files-exist case so
> they can be handled with a single mechanism?
> 
>   $ bash --version
>   GNU bash, version 3.1.17(1)-release (i486-pc-linux-gnu)
>   Copyright (C) 2005 Free Software Foundation, Inc.
> 
>   $ ls
> 
>   $ chmod a-x *
>   chmod: cannot access `*': No such file or directory
> 
>   $ 
> 
> I could do this
> 
>   $ chmod a-x * 2>/dev/null
> 
>   $ 
> 
> but that just hides the error; there are still two cases (the exit status tells
> the tale).  I could do this
> 
>   $ chmod a-x * 2>/dev/null ; :
> 
>   $ 
> 
> but that's more hiding. 

The "problem" in your example is that chmod(1) requires files to operate
on; a simple  chmod a-x  without arguments won't work, even if the shell
file globbing mechanism would produce an empty argument list instead of
taking the * literally.

> This works
> 
>   $ files=$(find . -type f)
> 
>   $ [ -z "$files" ] || chmod a-x $files
> 
>   $ 

You could do

   set -- *
   [ -f "$1" ]

but I am not sure whether you like that since it does not avoid the test
operation that you seem to dislike.

> and is, I suppose, good documentation, but it's too much machinery.  This
> problem is similar to handling zero-iteration loops in programs, but it's
> irratating that
> 
>   $ for i in * ; do chmod a-x $i ; done
>   chmod: cannot access `*': No such file or directory
> 
>   $ 
> 
> doesn't work.  Anything else?

A while/read loop

   find . -type f | while read -r f ; do chmod a-x "$f" ; done


Janis
0
Janis
3/21/2008 4:51:00 AM
R. Clayton wrote:

>   $ chmod a-x *
>   chmod: cannot access `*': No such file or directory

As Janis said, the error is due to chmod /always/ expecting an argument.
However, you might find the nullglob feature of bash useful sometimes.

$ ls
$ echo *
$ *
$ shopt -s nullglob
$ echo *

$

Note, however, that this does not solve the problem for all those situations
where an argument is expected anyway, like eg chmod:

$ chmod a-x *
chmod: missing operand after `a-x'
Try `chmod --help' for more information.

This is something that cannot really be avoided, AFAIK.

-- 
All the commands are tested with bash and GNU tools, so they may use
nonstandard features. I try to mention when something is nonstandard (if
I'm aware of that), but I may miss something. Corrections are welcome.
0
pk
3/21/2008 8:49:37 AM
  The "problem" in your example is that chmod(1) requires files to operate on;
  a simple chmod a-x without arguments won't work, even if the shell file
  globbing mechanism would produce an empty argument list instead of taking the
  * literally.

True, but I was whining more about misbehaving glue code.  Here's another
irritating one what doesn't work:

  $ find . -type f | xargs chmod a-x
  chmod: missing operand after `a-x'
  Try `chmod --help' for more information.

  $

although there is a gnu extension that makes it behave:

  $ find . -type f | xargs --no-run-if-empty chmod a-x

  $    
0
rvclayton
3/22/2008 12:20:55 AM
R. Clayton wrote:
>   The "problem" in your example is that chmod(1) requires files to operate on;
>   a simple chmod a-x without arguments won't work, even if the shell file
>   globbing mechanism would produce an empty argument list instead of taking the
>   * literally.
> 
> True, but I was whining more about misbehaving glue code. 

> Here's another irritating one what doesn't work:

Yes, but actually it's the same case as upthread; if the list provided
by find is empty chmod won't get the arguments it expects.

Janis

> 
>   $ find . -type f | xargs chmod a-x
>   chmod: missing operand after `a-x'
>   Try `chmod --help' for more information.
> 
>   $
> 
> although there is a gnu extension that makes it behave:
> 
>   $ find . -type f | xargs --no-run-if-empty chmod a-x
> 
>   $    
0
Janis
3/22/2008 12:38:00 AM
Reply:

Similar Artilces:

file extention from file file location
Hi, I am new to perl (only working on this one problem) but I program in other languages. We have an upload script written in cgi. Can anyone tell me how to get the file extension from the variable $File1 (actual file location on uploader's computer) then find out if it is equal to "txt" Thanks in advance for not telling me to read the manual. Ron -- PLEASE NOTE: comp.infosystems.www.authoring.cgi is a SELF-MODERATED newsgroup. aa.net and boutell.com are NOT the originators of the articles and are NOT responsible for their content. HOW TO POST to co...

Iteration through File.file? misses entries for which File.file?(entry) == true
Hello everyone, I'm new to Ruby, so I'm most likely making an elementary mistake. However, searching Google didn't help me find my answer, and after mucking around with irb I still haven't figured it out. I wrote a method that was intended to take all of the files in a given directory and put them into an array. Here's the code I wrote: def getFiles(dir) pwdFiles = Array.new Dir.foreach(dir) do |entry| pwdFiles.push(entry) if File.file?(entry) == true end end This works fine when called on the working directory --- that is, getFile...

Batch file to Shell file
I have given my batch . CAn anyone please convert this into Shell file. It is v urgent. @echo off REM Locations set Enigma_Home=D:\Enigma3C set Vin_Home=D:\VIN_Test_Suite\VIN set PartSet_Home=D:\VIN_Test_Suite\PartSet REM Pre_Process set Vin_Desc=Ford_Vin_Descriptor.xml set Vin_Desc_out=Ford_Vin_out_Descriptor.xml set PartSet_Desc=Ford_PartSet_Descriptor.xml set PartSet_Desc_out=Ford_PartSet_out_Descriptor.xml REM Create Packs set VIN_title_Name=FordSample set PartSet_title_Name=PartSetSample set Work_Dir=D:\VIN_Test_Suite\WorkFiles set PACKS_DIR=D:\VIN_Test_Suite\Packs REM For Update ...

object files, header files, source files?
I can't find any books describing the anatomy of C/C++ programs. What is an object file? Can it be generated from a header file?, what is linking?, why does a source file need an object file? etc. I have looked in The C programming Language, The C++ Programming Language, Accelerated C++, GNU make etc but they don't describe these basic subject. Any literature that covers these subjects? saneman wrote: > I can't find any books describing the anatomy of C/C++ programs. What is > an object file? Can it be generated from a header file?, what is > linking?, why does a s...

write file + input file + write file
Hi, I want to write some latex command to a file (file1), input the file at the end of the document. These command shall write to another file (file2) and at a second run I use the file2. Whatever I have tried nothing works and I don't known why. The goal is to save a set of special commands and run them at the end of the document, because they use some values which are only available at the end! Can anybody please help me with that? Thanks in advance! I should be something like: \begin{document} \usepackage{style} \input{file2} %file exists from a previous run \end{document} style.sty: \newwrite\fileone \openout\fileone=file1.out \newwrite\filetwo \openout\filetwo=file2.out \newcommand{\writetofiletwo}[1]{% \write\filetwo{#1}% } \newcommand{\writetofileone}[1]{% \write\fileone{\writetofiletwo{some text}}% } \AtEndDocument{\closeout\fileone% \input{file1}% \closeout\filetwo% } Problem solved :-) \immediate\closeout before \input was required. greenux wrote: > Hi, > I want to write some latex command to a file (file1), input the file > at the end of the document. These command shall write to another file > (file2) and at a second run I use the file2. > Whatever I have tried nothing works and I don't known why. The goal is > to save a set of special commands and run them at the end of the > document, because they use some values which are only available at the > end! > Can anybody please help me with that? > Thanks in advance! &...

UNIX header files to Windows header files
Hi, I need to port some C code from UNIX SGI to Windows (MS VC++ compiler). The following header files arent available in windows: #include <sys/param.h> #include <sys/ioctl.h> #include <sys/socket.h> #include <sys/socketvar.h> #include <netinet/in.h> #include <sys/types.h> #include <sys/stat.h> #include <netdb.h> #include <arpa/inet.h> Does anyone have any suggestions on how to get windows versions of these? Thanks! anand.ba@gmail.com wrote: > > I need to port some C code from UNIX SGI to Windows (MS VC++ compiler). &g...

Executable file for .m files for Cluster/UNIX
Hi all, I have a set of .m files which run successively to generate output matrix. I need to run all these .m files on cluster/UNIX. How do I write an executable file for this? Help would be appreciated. ...

how to convert dos file to unix file in eclipse
I am working on dos environment for some java files but need to check in to unix sytem.how to convert dos file to unix file in eclipse? Thanks, David "david wolf" <yihucd@gmail.com> wrote in message news:1145547009.141508.69230@z34g2000cwc.googlegroups.com... >I am working on dos environment for some java files but need to check > in to unix sytem.how to convert dos file to unix file in eclipse? It's probably easier if you use a seperate tool rather than Eclipse for this task. Within Eclipse, you can right click on the project and choose "properties&q...

Unix files saved in a D3 file save are changed
Hello All: When I restore an account where the D3 file is actually a Q-pointed Linux directory, a line containing the name of the Linux file is inserted before the rest of the file item. Does anyone know why or how to avoid this? D3 item looks like this: :ED MD DS2.UQ.BIN top ..P 001 QS 002 003 /dsol/dscope/da/bin eoi 003 Linux file before restore: :ED DS2.UQ.BIN mvhttp-client top ..P 001 #!/usr/bin/perl 002 # 003 # Copyright (C) 2015... Linux file after restore: :ED DS2.UQ.BIN mvhttp-client top ..P 001 mvhttp-client 002 #!/usr/bin/perl 003 # 004 # Copyright (C) 2015... System info: D3 Version: D3 lx-7.5.12-.Z Linux Version: RedHat Enterprise Linux (RHEL) 5.2 x64 running kernel 2.6.18-92.1.22.el5PAE Running on: Dell PowerEdge 1900 with dual Intel Xeon E5320 processors with a PERC 5/i RAID controller in (I believe) RAID 5 configuration I'd ask this question on the Pick & Multivalue group on google, as this newsgroup is essentially defunct First port of call wuld be to upgrade D3 ... at least to latest versions in 7.5.x, if not beyond to 9.x --> check release notes as this could be an issue that was/is resolved ...

Unix Executable File Supplants MT-NW File, How?
Well I am mystified for sure. Attempting to Append a post to a file I have for that purpose, I discovered that the file was no longer accessible from the Save dialogue. Looking in the Finder, I discovered that it had been mysteriously transformed into a Unix Executable File. I have not opened this file. What I want to know is how did it get there and what happened to my original file and is it safe to open it? Please note that I only switched to OS X a month ago and have not used Unix yet at all. Madeleine On Mon, 01 Dec 2003 12:38:45 -0600, Madwen wrote: > Well I am mystifie...

Random file in a file
I am trying to take a random file from a folder what can I do to get random to find a file but not numbers? On Tue, 20 Oct 2015 19:24:03 +0000, Baldvin Vigfússon wrote: > I am trying to take a random file from a folder what can I do to get > random to find a file but not numbers? You could get a list of files in the folder, generate a random integer between 1 and the number of files, then use that RN to index the file list. Bruce ...

Why not FILE instead of FILE*?
Why is the standard C type for file handles FILE* instead of FILE? AFAIK the type FILE is a pre-defined typedef for some other type anyway. So why not make it instead a typedef for a *pointer* to that type? For one thing, that would stop people trying to poke around at the insides of a FILE. For another, it could allow for cases where the "real" type behind FILE is not a pointer to anything. -- /-- Joona Palaste (palaste@cc.helsinki.fi) ------------- Finland --------\ \-- http://www.helsinki.fi/~palaste --------------------- rules! --------/ "It sure is cool having money and c...

File.renameTo(File)
Thanks, I'm running 1.5 version of java and I'm simply attempting to move a file from a source directory to a target directory. It seems the renameTo works fewer times than it does. I print the absolute path to ensure they're correct and they are. Someone mentioned there's a renameTo bug. Is there a workaround? I'm thinking of exec'ing a process and running a dos move. Thank again. Sly On Tue, 4 Apr 2006 20:38:18 -0400, ":-o" <fcrutch@save-net.com> wrote, quoted or indirectly quoted someone who said : > >It seems the renameTo works few...

What is self.file = file for?
Hello! I have trouble understanding something in this code snippet: class TextReader: """Print and number lines in a text file.""" def __init__(self, file): self.file = file . . . When would you do a thing like self.file = file ? I really don't find an answer on this. Please help me understand this. wxPythoner@gmail.com wrote: > Hello! > > I have trouble understanding something in this code snippet: > > class TextReader: > """Print and number lines in a text file.""&q...

How to find New File,Write file and close file?
Hi Anyone can show me how to find New File,Write File and close file in block diagram.The picture of file names are as show in pics. Thanks &nbsp; Nyan Lin File find.JPG: http://forums.ni.com/attachments/ni/170/322096/1/File find.JPG Hi. The functions You're looking for are under "File I/O --&gt; Advanced File Functions" (At least the NEW File) The 2 others are in the "File I/O" menu on the functions palette. Good luck.Message Edited by BCL@Servodan on 05-08-2008 03:38 AM Hi I can't find&nbsp; my files at Advanced file functions that which u say.. I am using LabVIEW PDA Module Version 8.5.So is there any concern with version? &nbsp; Thanks for ur reply Best Regards NyanLin Dear <a href="mailto:BCL@Servo" target="_blank">BCL@Servo</a>, Please give a screenshot. &nbsp; Thanks, Mathan Hi again. My mistake. I was looking in my LabView7. In 8.5.1 the Icons have changed........ I cannot give a screenshot of the 8.5.1, as it is on&nbsp;another PC, but the function is called Open/Create/Replace File, Write text to file and Close file. They are in the "Programming --&gt; File I/O" palette. Sorry for the inconvienience. &nbsp; Here a screenshot in LV8.5 of what <a href="mailto:BCL@Servo" target="_blank">BCL@Servo</a> told. Right click anywhere on block diagram window, click on the Floppy symbol in programming menu named File I/O. there everyt...

Have a shell script check for a file to exist before processing another file
I have a shell script that runs all the time looking for a certain type of file and then it processes the file through a series of other scripts. The script is watching a directory that has files uploaded to it via SFTP. It already checks the size of the file to make sure that it is not still uploading before it starts processing. I would like to place another check in the script that looks for the existance of another file before processing begins. The script looks like: [CODE] #!/bin/ksh PATH=/gers/nurev/menu/pub/sbin:/gers/nurev/menu/pub/bin:/gers/nurev/menu/pub/mac :/gers/nurev/menu/ad...

[vim] using a line in a file as shell command for output into same file
I have a file that quotes some linux commands, and want to be able to run that command from within the file and have the output come below it. For example, some lines in the file might be: blah blah $ vim --version | head -n 1 - I'll move the cursor to the line with the command on it, then hit some bound key, and want the output to look like this: blah blah $ vim --version | head -n 1 - VIM - Vi IMproved 7.1 (2007 May 12, compiled Oct 31 2007 06:25:17) I know I can read the output of the command by pasting it on the vim commandline with :r!command but I'm not sure h...

My shell script is changing the file permission of all files in which the script lies.
Hi all, I have written a shell script that works as intended. But one strange thing happens. Whenever I run this script, it removes the execute permission from all the files of the directory where the script is. What can be the reason? Thanks & Regards, Amit On Feb 5, 6:51=A0am, kami...@gmail.com wrote: > Hi all, > > I have written a shell script that works as intended. But one strange > thing happens. Whenever I run this script, it removes the execute > permission from all the files of the directory where the script is. > What can be the reason? > > Thanks &...

Open folder, read file, close file, open new folder, read file close file etc....
Hello, My current directory has several folders within it. Each folder has the same type of file within it. How do i go from the current directory, into one of the folders, edit the file, then come out of the folder, then go into another folder, edit a file then come out of the folder then into a new one etc etc etc. If this does not make sense i apologise and will try to explain my problem a little better. Any help would be much appreciated. Cheers, Phil Phil: I'm not sure it makes sense. Personally I use the mouse and keyboard for doing this. Were you thinking of something else?...

Generic file I/O error, the file is not a valid labview file
Hi, &nbsp; I have created a project in LV 8.0. Now I am&nbsp;getting the following error when I try to open it...'Generic file I/O error, the file xxxxx.vi is not a valid labview file.' I have tried to open all the subvi's used in my application and I am able to open all of them. I found that this condition may occur if the hard disk is full. So I tried to open my vi from another PC but I am getting the same error everywhere. What am I supposed&nbsp; to do ?? Please help me out if anyone has a solution to this... &nbsp; Regards, Divya Hi Mike... Thank you for your reply... I get this error when opening the main vi in my application. I can't recall anything which may have corrupted the file. The earlier version of the main vi of which I had saved&nbsp;a copy does not give any problems while opening.&nbsp;I had made many changes in the next version of my application which is lost now... Do you think that the file may be corrupted? Is there no way to retrieve it? I do have a system&nbsp;running LabView 8.2 and I have tried to open this file with that version. But I am still getting the same error. DivyaMessage Edited by divya on 03-22-2007 10:42 PM Hi Divya, Check if the VI's size is &gt; the one U re stating as Ur earlier version, without that so many changes. If it &gt;, it may not ve got corrupted. Problem could be something else. Else, if it shows some drastically low size ( like too less kb ), then it has got goof...

Q: File Exchange --- posted file not there yet. Should I upload file again?
I posted a file to the file exchange yesterday, but it is not showing up yet. How long does it usuallly take for the file to show up? Should I upload it again or wait? James Tursa James Tursa <aclassyguywithaknotac@hotmail.com> wrote in message <f058j3h10e632t855riru5bocpodhvgu3k@4ax.com>... > > I posted a file to the file exchange yesterday, but it is not showing > up yet. How long does it usuallly take for the file to show up? Should > I upload it again or wait? > > James Tursa It can take a few days depending on workload, holidays/weekends, sick days etc....

how to make a file from the strings in a file and redirect the content to these new files
Here is scenari0 223 files copied to this this one big flat file. now how to make this file to 223 files. further explaination: below is excerpt from that one file under xxxxx you will see @TC_ID: :BIC4007 then another @TC_ID: :BIC40008 and has a 223 time with TC_ID: :BICxxx numbers How do I use such a script that makes a file named BIC40008, then another BIC40007 AND copying the content from line @TC_ID: :BIC40007 till one line before the next TC_ID: :BIC40008 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx @TC_ID: :BIC40007 @TC_TITLE: @ACT_IDS: @AUTHOR: @AUTO: @CLASS: @GROUP_ID: @RELEASES: @T...

Segmentation violation in mex file. How can we debug a mex file? I do not have unix.
Here is the mex code I wrote to read in a tab-delimited file. The mex file = got created but it causes my MATLAB to end abruptly and give the following = error. Can anyone help me where I am going wrong? Please let me know if any= further information is required Abnormal termination: Segmentation violation #include "mex.h" #include "matrix.h" #include <stdio.h> #include<string.h> #include<stdlib.h> =20 void mexFunction(int nlhs,mxArray *plhs[],int nrhs,const mxArray *prhs[]) { =20 FILE *ptr_file; const char **field_names; /* pointers to field names */ char *buf[1024]; char *temp[20]; int count; int i, j, k, l; int date_field, mva_field, qc_load_field, air_field, qc_air_field, oil_= field, qc_oil_field, wind_a_field, qc_wind_a_field, wind_b_field, qc_wind_b= _field, wind_c_field, qc_wind_c_field, tamb1_field, qc_tamb1_field; char *NAME; NAME=3DmxArrayToString(prhs[0]); count =3D 0; //open file to count elements =20 ptr_file =3Dfopen(NAME,"r"); if (ptr_file !=3D NULL) { //skip first 3 lines =20 fgets(buf, sizeof(buf), ptr_file); fgets(buf, sizeof(buf), ptr_file); fgets(buf, sizeof(buf), ptr_file); //start counting no. of elements =20 while(fgets(buf, sizeof(buf), ptr_file) !=3D NULL) count++; fclose(ptr_file); } field_names[0] =3D "date"; field_names[1] =3D "mva&q...

Problems converting *.sas file to a tab delimited file, in Unix environment
Hello May I please ask for your kind help? Unfortunately I don't have any knowldege of SAS. I have a program [I am attaching a copy of this program below] that generates *.sas7bdat files. After I generate a file called abc_out.sas7bdat , I go to the directory where abc_out.sas7bdata is located and type the following command: sas dexport TAB "abc_out.sas7bdat" {I found this command on the Internet, I thought it would generate a tab delimited text file called "TAB"} {Please note that I am typing this command at the Telnet prompt, in Unix environment, as far as I un...

Web resources about - Unifying files and no files. - comp.unix.shell

Unifying theories in mathematics - Wikipedia, the free encyclopedia
The process of unification might be seen as helping to define what constitutes mathematics as a discipline. For example, mechanics and mathematical ...

Why Star Wars is a unifying force
Blockbuster movies are still popular despite an overwhelming array of entertainment choices The Age's Economics Editor Peter Martin explains ...

Royal baby a joyful and unifying force
I have been dismayed at the crass and disrespectful comments on radio about the royals.

Why Star Wars is a unifying force (02:36)
Blockbuster movies are still popular despite an overwhelming array of entertainment choices The Age's Economics Editor Peter Martin explains ...

The ties that blind: Tony Abbott never cut it as a unifying national leader
Malcolm Turnbull's prime ministership is a chance to unify Australia by&nbsp;leading from the centre, something Tony Abbott was never able to&nbsp;achieve. ...

Jeffrey Baldwin inquest urges unifying child-protection agencies
Ontario should consider merging its children's aid societies into one co-ordinated agency. That was one of 103 recommendations released Friday ...

Bloc Quebecois members want new leader Mario Beaulieu to adopt a more open, unifying approach - CTV News ...
New Bloc Quebecois Leader Mario Beaulieu was assuring people on Monday that his party is "more united than ever" amid resignations and anger ...

Unifying pension system a monumental breakthrough
Unifying pension system a monumental breakthrough People's Daily Online A State Council meeting presided over by Premier Li Keqiang pledged ...

Banana Republic CMO Talks New 'Mad Men' Collection, Unifying the Brand
Banana Republic's performance has been uneven in recent years as the brand, like siblings Gap and Old Navy, sought to distinguish itself in the ...

In divided S. Africa, anger at new road tolls proves a powerful unifying force - Alaska Dispatch
The ruling ANC pushed through new electronic tolls after years of opposition. It could pay a price in upcoming national elections.

Resources last updated: 3/21/2016 2:39:59 AM