Skip to main content
Add more specific explenation for the problem
Source Link
Doron Behar
  • 783
  • 8
  • 27

I'm trying to run a small gawk script which will run certain shell commands using system() on certain. I want to get the file list with find and run the script like that:

find <arguments> -printf "%f\n" | script.awk

The problem is that these files have special characters such as spaces, quotes and parentheses in them. For example:

$ ls
'Aujourd'\''hui C'\''est Toi (Orchestral).flac'

I tried all sorts of quotes like the following:

system("<command> \""$0"\"")

And also this:

system("<command> \'"$0"\'")

And I get all sorts of errors, some of these errors are from the sh -c and some are from gawk...

Is there a way to pass the record $0 or any other variables from the gawk script to a shell command with out encountering all of theses problems?

Note: I know it could have been easier to just rename the files temporarily but I find it more challenging to avoid it.

EDIT:

None of the suggested answers solved my problem so here is a more specific description of the problem: I'm trying to run a command which uses 2 awk variables. Examples:

If I run:

system("metaflac --show-tag=ARTIST \""FILE"\"")

I don't get an error because I quote FILE and because it doesn't contain double quotes it goes OK.

But if I run:

system("metaflac --set-tag=ARTIST="AWK_VAR_WITH_ARTIST_VALUE" "\"FILE\")

Even when I quote it, I get an error by metaflac that the flac file was not found. When reading the error message, I see the file name was split into separate words.

I'm trying to run a small gawk script which will run certain shell commands using system() on certain I want to get the file list with find and run the script like that:

find <arguments> -printf "%f\n" | script.awk

The problem is that these files have special characters such as spaces, quotes and parentheses in them. For example:

$ ls
'Aujourd'\''hui C'\''est Toi (Orchestral).flac'

I tried all sorts of quotes like the following:

system("<command> \""$0"\"")

And also this:

system("<command> \'"$0"\'")

And I get all sorts of errors, some of these errors are from the sh -c and some are from gawk...

Is there a way to pass the record $0 or other variables from the gawk script to a shell command with out encountering all of theses problems?

Note: I know it could have been easier to just rename the files temporarily but I find it more challenging to avoid it.

I'm trying to run a small gawk script which will run certain shell commands using system(). I want to get the file list with find and run the script like that:

find <arguments> -printf "%f\n" | script.awk

The problem is that these files have special characters such as spaces, quotes and parentheses in them. For example:

$ ls
'Aujourd'\''hui C'\''est Toi (Orchestral).flac'

I tried all sorts of quotes like the following:

system("<command> \""$0"\"")

And this:

system("<command> \'"$0"\'")

I get all sorts of errors, some of these errors are from sh -c and some are from gawk...

Is there a way to pass the record $0 or any other variables from the gawk script to a shell command with out encountering all of theses problems?

Note: I know it could have been easier to just rename the files temporarily but I find it more challenging to avoid it.

EDIT:

None of the suggested answers solved my problem so here is a more specific description of the problem: I'm trying to run a command which uses 2 awk variables. Examples:

If I run:

system("metaflac --show-tag=ARTIST \""FILE"\"")

I don't get an error because I quote FILE and because it doesn't contain double quotes it goes OK.

But if I run:

system("metaflac --set-tag=ARTIST="AWK_VAR_WITH_ARTIST_VALUE" "\"FILE\")

Even when I quote it, I get an error by metaflac that the flac file was not found. When reading the error message, I see the file name was split into separate words.

Source Link
Doron Behar
  • 783
  • 8
  • 27

Escaping gawk variables for shell commands

I'm trying to run a small gawk script which will run certain shell commands using system() on certain I want to get the file list with find and run the script like that:

find <arguments> -printf "%f\n" | script.awk

The problem is that these files have special characters such as spaces, quotes and parentheses in them. For example:

$ ls
'Aujourd'\''hui C'\''est Toi (Orchestral).flac'

I tried all sorts of quotes like the following:

system("<command> \""$0"\"")

And also this:

system("<command> \'"$0"\'")

And I get all sorts of errors, some of these errors are from the sh -c and some are from gawk...

Is there a way to pass the record $0 or other variables from the gawk script to a shell command with out encountering all of theses problems?

Note: I know it could have been easier to just rename the files temporarily but I find it more challenging to avoid it.