Dealing with white spaces in bash scripts


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Dealing with white spaces in bash scripts
# 8  
Old 03-17-2013
Your shell man page should document the steps taken to parse a command, although it may be dense and terse. Experimentation usually helps fill in the blanks. If not, you can always ask us.

Should it be of interest, here's a portable solution that should handle any filename:
Code:
find /dump -group 200 \( -type f -o -type d -o -type l \) -exec sh -c '
    for f; do
        chgrp -vh $(ls -lnd "$f" | awk "NR==1 {print \$3; exit}") "$f"
    done
' sh {} +

Most importantly, note that awk has been restricted to only the first line of output. If there is indeed a possibility of a filename having newlines, then uid is only the third field of the first line; the third field of subsequent lines will be some part of the multiline filename.

Also, I changed -gid to -group, which is more portable. However, it works slightly differently. At first it tries to lookup a group name. If the name doesn't exist, and if the name provided is numerical, then it will look up a group id.

Regards,
Alister
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Dealing with filename spaces in Perl

The following command to replace text in place in multiple files in a directory is tripping up on filename spaces (Windows environment). I really don't know Perl. find '\\server\directory' | xargs perl -pi -e 's/textA/textB/g'Mike (2 Replies)
Discussion started by: Michael Stora
2 Replies

2. Shell Programming and Scripting

Bash - read white spaces

Hello! I have one problem with my bash script - I would like to be able to read white space characters from stdin (for example single " ") - can I acomplish that somehow? I need to read only one character at the time, so I use read -s -n 1 var but it doesn't work for whitespaces apparently. ... (3 Replies)
Discussion started by: xqwzts
3 Replies

3. Shell Programming and Scripting

Leading white spaces

Hi, I am having problem in deleting the leading spaces:- cat x.csv baseball,NULL,8798765,Most played baseball,NULL,8928192,Most played baseball,NULL,5678945,Most played cricket,NOTNULL,125782,Usually played cricket,NOTNULL,678921,Usually played $ nawk 'BEGIN{FS=","}!a... (2 Replies)
Discussion started by: scripter12
2 Replies

4. Shell Programming and Scripting

Dealing with files with spaces in the name

Hello, I'm a computer science major and I'm having problems dealing with file names with spaces in them. Particularly I'm saving a file name in a variable and then using the variable in a compare function i.e. a='te xt.txt' b='file2.txt' cmp $a $b If anyone could help me with this particular... (10 Replies)
Discussion started by: jakethegreycat
10 Replies

5. Shell Programming and Scripting

white spaces in bash autocompletion

Hello dear community! I've recently written a BASH function for auto completion of options. It works like following: if a user types a command and then an argument to this command which starts with "^-" and then presses TAB, then 'user_command --help (or -h)' is invoked and possible options are... (0 Replies)
Discussion started by: sidorenko
0 Replies

6. 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

7. Shell Programming and Scripting

Two or more white spaces in string

Hi, Can anybody suggest me how to combine two strings with two or more white spaces and assign it to a variable? E.g. first=HAI second=HELLO third="$first $second" # appending strings with more than one white spaces echo $third this would print HAI HELLO Output appears... (2 Replies)
Discussion started by: harish_oty
2 Replies

8. Shell Programming and Scripting

trimming white spaces

I have a variable that calls in a string from txt file. Problem is the string comes with an abundance of white spaces trailing it. Is there any easy way to trim the tailing white spaces off at the end? Thanks in advance. (9 Replies)
Discussion started by: briskbaby
9 Replies

9. Shell Programming and Scripting

delete white spaces

hi all... i have the next question: i have a flat file with a lot of records (lines). Each record has 10 fields, which are separated by pipe (|). My problem is what sometimes, in the first record, there are white spaces (no values, nothing) in the beginning of the record, like this: ws ws... (2 Replies)
Discussion started by: DebianJ
2 Replies

10. UNIX for Dummies Questions & Answers

deleting white spaces

How would I delete white spaces in a specified file? Also, I'd like to know what command I would use to take something off a regular expression, and put it onto another. ie. . . . expression1 <take_off> . . . expression2 (put here) . . . Any help would be great, thanks! (10 Replies)
Discussion started by: cary530
10 Replies
Login or Register to Ask a Question
UNIXSERVER(1)						      General Commands Manual						     UNIXSERVER(1)

NAME
unixserver - UNIX domain socket server SYNOPSIS
unixserver [options] socket program DESCRIPTION
Unixserver is UNIX domain socket server that conform to UCSPI, the UNIX Client-Server Program Interface. OPTIONS
-q Quiet. Do not print any messages. -Q (default) Print error messages. -v Verbose. Print error and status messages. -d Do not delete the socket file on exit. -D (default) Delete the socket file on exit. -u UID Change user id to UID after creating socket. -g GID Change group id to GID after creating socket. -U Same as '-u $UID -g $GID'. -o UID Make the socket owned by UID. -r GID Make the socket group owned by GID. -O Same as '-o $SOCKET_UID -r $SOCKET_GID'. -p PERM Set the permissions on the created socket (in octal). Note: this option, if set, overrides the mask below. (defaults to 0666 minus umask) -m MASK Set umask to MASK (in octal) before creating socket. (defaults to 0, previous value is restored afterwards) -c N Do not handle more than N simultaneous connections. (default 10) -b N Allow a backlog of N connections. -B BANNER Write BANNER to the client immediately after connecting. SEE ALSO
unixclient(1), unixcat(1) http://cr.yp.to/proto/ucspi.txt AUTHOR
ucspi-unix package was written by Bruce Guenter <bruceg@em.ca> This manual page was originally written by Tomas Kuliavas <tokul@bigfoot.com> for the Debian GNU/Linux system, but may be used elsewhere under the GPL. UNIXSERVER(1)