0

I have an issue which can be solved with utility to add quotes to argument and output to standard. I did a web seach for "linux utility to add quotes to argument" and found nothing relevant. Is there amy? (will need to pair it with xargs)

P.S. original issue (see at the end for quick understanding):
Run find on find

Marisha
  • 343
  • Why do you need quoting for that? Just continue to use -print0 and -0 as you were already doing – muru Jan 07 '20 at 05:20
  • @muru, looks like find (at least GNU one I have) does not work as would be expected if action expression is before criteria expressions, and I need to pass criteria (name) by xargs at the end. – Marisha Jan 07 '20 at 05:28
  • 1
    No, you don't. Use xargs with -i/-I – muru Jan 07 '20 at 05:44
  • @muru, yes it works this way. Would you like to type a quick answer to https://unix.stackexchange.com/questions/560739/run-find-on-find and I except it? Should I delete this one after? – Marisha Jan 07 '20 at 06:07
  • You can post an answer showing the command you finally used. Yes, this question doesn't add much, you can delete it. – muru Jan 07 '20 at 06:43

3 Answers3

1

I can't see how that could help with your other problem, but if indeed you need a command that prints its arguments separated by a space character and the whole thing surrounded with " and terminated by a newline character, you could do it with awk:

awk -- '
  BEGIN{
    printf "\""
    for (i = 1; i < ARGC; i++) printf "%s", (i == 1 ? "" : " ") ARGV[i]
    print "\""
  }' arg1 "arg 2" 'arg 3' 'arg "4"'

prints:

"arg1 arg 2 arg 3 arg "4""
0

My original issue got proper solution suggested by @muru, however maybe of any use C utility for quoting I now wrote (my system had quote command but it quoted only 1st argument, ignoring the rest), as usual use gcc quoteit.c -o quoteit && chmod +x quoteit to build provided code in quoteit.c in current folder:

#include <stdio.h> 
#include <stdlib.h>
#include <string.h>
#include <ctype.h>

int main(int argc, char *argv[])  
{ 
    if (argc ==2 && (!strcmp(argv[1],"-h") || !strcmp(argv[1],"--help")))
    {
        printf("The program surrounds set of arguments with double quotes\n");
        printf("Example: quoteit 12 23 outputs \"12 23\"\n");
        return 0;
    } 
    //printf("%d\n", argc); 

    printf("\""); 
    for (int i=1; i< argc; i++) 
    {
        printf("%s", argv[i]); 
        if (u<args-1) printf(" ");
    } 
    printf("\"");
    return 0; 
}
Marisha
  • 343
0

A simple Bash script could do that:

#!/bin/bash
echo "\"$*\""

Already $* generate the full list of arguments. echo simply sourrounds them with escaped quotes.

myquote.sh hi, 'how do' you "do?"
"hi, how do you do?"

Now, related to the intended use of this 'quoter' script to put quotes around filenames obtained with find in its exec clause, it is not necessary, because you can add the quotes directly to the curly braces argument, like in this example, to be correctly received by the grep command:

find /etc -exec grep -Hi "{}" \;

or, in case of piping to xargs, using the -print0 clause to mark the filenames with a NULL delimiter for the xargs command next in the pipe.

Fjor
  • 187
  • 1
    The quotes around {} in your find command do nothing. The argument replaced in {} is not split by the shell in any case. – Kusalananda Nov 03 '20 at 08:15
  • Confirmed by testing, but the man page of find (exec clause section) explicitly states: "[semicolon or curly braces]...might need to be escaped (with a '') or quoted to protect them from expansion by the shell", so there is no guarantee of portability. – Fjor Nov 05 '20 at 08:37
  • In the context of this question, which is tagged with [tag:bash], the {} does not need quoting. Likewise, in any other sh-like shell, {} does not need quoting. If you're using tcsh, or some other non-standard shell, then {} may need quoting. – Kusalananda Nov 05 '20 at 08:45