Problem with shell script while spaces encountered in directory names


 
Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users Problem with shell script while spaces encountered in directory names
# 8  
Old 07-20-2016
Quote:
Originally Posted by Ivo Breeden
First: `man find` shows there are options for "UNUSUAL FILENAMES" (e.g. filenames with spaces).
Code:
      -print0

This working depends on the find-command you have supporting it. Actually -print0 is a GNU-extension and, for instance, the find on AIX doesn't know it.

To the rest of your sentiments, though, i can wholeheartedly agree.

I hope this helps.

bakunin
# 9  
Old 07-21-2016
Quote:
Originally Posted by Ivo Breeden
[..]Second: using a "for loop" may be dangerous, there can be more results than a Unix line can handle. [..]
Hi Ivo, this is not correct, there are no line length limitations to a for loop, which is part of the shell syntax, so the limitations of passing parameters to a subprocess do not apply.

The problem with the construct in post #1 is that the result of the command substitution (`...`) is vulnerable to interpretation by the shell, e.g. field splitting, so for example files with spaces will be split in two or more arguments and these arguments typically do not exist and so this will fail, hence the (No such file or directory) messages..

Last edited by Scrutinizer; 07-21-2016 at 08:29 AM..
# 10  
Old 07-21-2016
Quote:
Originally Posted by Scrutinizer
there are no line length limitations to a for loop, which is part of the shell syntax, so the limitations of passing parameters to a subprocess do not apply
Yes - and No: the maximum number of passed arguments will not apply but the maximum line length will. Suppose you have a line like:

Code:
for x in * [; do ....]

and you execute that in a directory with really many files, it will first be expanded by the shell to:

Code:
for x in fileA fileB fileC [...]

and if the really many filenames make this line too long for the shells input parser to fathom you will be in deep kimchi. I give you that: some shells don't care for system constants like "LINE_MAX", but as far as i know a POSIXly correct utility has only to be able to digest lines up to that length. Above that all bets are off. So, if you want to stay on the safe side you better do not require something that is optional in the standards.

From /usr/include/sys/limits.h on a rather up-to-date AIX 7.1-system:

Code:
/* The system supports the following utility limit minimum values */

#define _POSIX2_BC_BASE_MAX     99   /* max ibase and obase values for bc     */
#define _POSIX2_BC_DIM_MAX      2048 /* max num elements in array for bc      */
#define _POSIX2_BC_SCALE_MAX    99   /* max scale value allowed by bc         */
#define _POSIX2_BC_STRING_MAX   1000 /* max len of string constant by bc      */
#define _POSIX2_EQUIV_CLASS_MAX 2    /* this define is obsolete               */
#define _POSIX2_COLL_WEIGHTS_MAX 2   /* max num weight for LC_COLLATE order   */
#define _POSIX2_EXPR_NEST_MAX   32   /* max num expression nested for expr    */
#define _POSIX2_LINE_MAX        2048 /* max len of utility input line         */
#define _POSIX2_RE_DUP_MAX      255  /* max num repeated reg for interval not */

I hope this helps.

bakunin
# 11  
Old 07-21-2016
Quote:
Originally Posted by bakunin
Yes - and No: the maximum number of passed arguments will not apply but the maximum line length will. Suppose you have a line like:

Code:
for x in * [; do ....]

and you execute that in a directory with really many files, it will first be expanded by the shell to:

Code:
for x in fileA fileB fileC [...]

and if the really many filenames make this line too long for the shells input parser to fathom you will be in deep kimchi.
Yes, the shell expands the wild card, but it does not somehow pass the expanded result as an input line to itself, it does not pass anything. It would only do that if it would pass it to another process, but it does not do that here.
Quote:
I give you that: some shells don't care for system constants like "LINE_MAX", but as far as i know a POSIXly correct utility has only to be able to digest lines up to that length. Above that all bets are off. So, if you want to stay on the safe side you better do not require something that is optional in the standards.

From /usr/include/sys/limits.h on a rather up-to-date AIX 7.1-system:

Code:
[..]
#define _POSIX2_LINE_MAX        2048 /* max len of utility input line         */
[..]

[..]
A for loop is not a utility! It is part of the shell syntax, it is not even a builtin utility. So LINE_MAX does not play a role either!

--
Also check this post, by alister on this subject:
https://www.unix.com/302890451-post14.html

Last edited by Scrutinizer; 07-21-2016 at 01:40 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Selecting/using part of directory names in script

I'm making a shell script to: -copy directories to a new location -perform conversions on the files within the copied directories -move the newly created files to a new directory Please see my super basic script and notes below... and thank you thank you thank you in advance !! ... (1 Reply)
Discussion started by: kayzee
1 Replies

2. Shell Programming and Scripting

Shell Script to Enter a Directory (Folder Name Contains Spaces)

I am writing a shell script to cd to a folder. folder name is "October Scripts" I am currently storing the name of folder in a shell variable as so path="October\ Scripts/". If I do cd $path I receive the error bash: cd: October\: No such file or directory What am I doing wrong? Kindly... (3 Replies)
Discussion started by: anand2308
3 Replies

3. Shell Programming and Scripting

making find/sed to include directory names with spaces

how can i make find/sed to include directory names with spaces the command is like this for i in `find wp-content/themes -type f -print0 | xargs -0 grep -l -iE 'e'`;do sed -i -e 's/word1/word2/gI' "$i";done but it skips one directory names with spaces sed: can't read ./Nova: No such... (5 Replies)
Discussion started by: vanessafan99
5 Replies

4. Shell Programming and Scripting

problem switching to directory using shell script

Hi, I have an issue with switching to other directory through shell script. I have used cd /Music" but that doesn't help me. Then I have also tried using alias proj 'Music' alias then I get the error permission denied but i set the chmod 777 Music. I am using cShell for my... (3 Replies)
Discussion started by: baluk
3 Replies

5. Shell Programming and Scripting

Problem with blank spaces in shell script

Hi, I did the following script: DIR=`pwd`;for i in `find . -name GESTION -type d`;do cd $i/..;setfacl -R -m g:directores:rwx, GESTION;echo $DIR'/'$i;cd $DIR;done This code do the following actions: 1. Starting inside a folder, it's searching for any folder called "GESTION" 2. Every time... (3 Replies)
Discussion started by: argie01
3 Replies

6. UNIX for Dummies Questions & Answers

Problem with spaces in directory path

Hi Gurus, I have a requirement. cat /usdd/Sample/"NDDF Plus DB"/"NDDF Descriptive and Pricing"/"NDDF BASICS 3.0"/"Pricing"/1.txt | sed 's/*|*/|/g' | sed 's/^*//'| sed 's/^*//; s/*$//' > temp.txt In unix prompt the above command is reading the file 1.txt and I am... (1 Reply)
Discussion started by: prabhutkl
1 Replies

7. Shell Programming and Scripting

Dealing with spaces in file names in a shell script

Hi, What's the best way to find all files under a directory - including ones with space - in order to apply a command to each of them. For instance I want get a list of files under a directory and generate a checksum for each file. Here's the csh script: #!/bin/csh set files = `find $1... (5 Replies)
Discussion started by: same1290
5 Replies

8. Solaris

Encountered a strange problem with doing a restore

Hi Unix Guru's, I have encountered a strange problem with doing a restore on a local sunu machine. We have 2 live system (v880, v450) and 1 test system v440. All the machines are sun4u and using sun os 8.0 We have 2 backup device. One Bcakup device connected with v880 and another backup... (0 Replies)
Discussion started by: cpandian
0 Replies

9. Shell Programming and Scripting

Directory names that contain spaces and other suggestions?

The script below was written to select files and convert a particular string to something other and replace that file. However, I came across some issues with filenames that contain spaces, any suggestions to get around this? Any other suggestions that may apply to this code would also be... (5 Replies)
Discussion started by: Shakey21
5 Replies
Login or Register to Ask a Question