Home > OS >  bash awk: how can I use " to reference a variable?
bash awk: how can I use " to reference a variable?

Time:06-22

I try to read a list of file on a remote directory then to get infos on each of them. I'm using awk for this.

My remote directory is on Google drive and I use gdrive, a command line tool, to access it. I store my access-token into a variable $tok.

My first try is to :

gdrive --access-token "$tok" list --bytes | grep 'INC' | awk -F ' ' '{gdrive --access-token "$tok" info $1}'

But I have no output. When I ask for a simple "print", I got the correct results

gdrive --access-token "$tok" list --bytes | grep 'INC' | awk -F ' ' '{print $1}'
1pZHe6YE43zmTuXnsJTMn6da_8CiBrJJH
1G9i7Y1WrRC24bJvw4idAiw55YAVsZhA5
1Vca1pMJbd5106latxTp5CNf6OnjbqkFn
1jt-n5Qw-EKFwyUCX_PNip1juoLSSv8YK
1bP9tNaOhd_Fib1sJdD_v91Vlq8FOlE-i
1NfWdK9ENdSKENexFDoS6hL1AsRqit9IZ

Then, if I perform the command "info" with one value of $1, I got a result :

    gdrive --access-token "$tok" info 1pZHe6YE43zmTuXnsJTMn6da_8CiBrJJH
Id: 1pZHe6YE43zmTuXnsJTMn6da_8CiBrJJH
Name: INC-22-06-01.bck
Path: INC-22-06-01.bck
Mime: application/octet-stream
Size: 1.0 KB
Created: 2022-06-02 16:47:14
Modified: 2022-06-02 16:47:14
Md5sum: 0f3xxxxxx
Shared: False
Parents: 0ABo477gTrFXCUk9PVA
ViewUrl: https://drive.google.com/file/d/1xxxxxxxxx
DownloadUrl: https://drive.google.com/uc?xxxxxxxx

My first conclusion is that my selection works fine and my command to get info works fine too.

So I think I have an issue using the " around the $tok variable in my awk command.

If I try to escape the ", I have an illegal statement error :

gdrive --access-token "$tok" list --bytes | grep 'INC' | awk -F ' ' '{gdrive --access-token \"$tok\" info $1}'
awk: syntax error at source line 1
 context is
    {gdrive --access-token >>>  \ <<< "$tok\" info $1}
awk: illegal statement at source line 1

Can you give me a correct syntax to perform my action ? Thanks in advance

CodePudding user response:

  • The command gdrive is not an awk command and you cannot execute it in the style of awk '{gdrive ..}'.
  • As $tok is a shell variable, it is not automatically passed to awk.

Would you please try instead (not tested):

#!/bin/bash

while read -r f1 _; do
    gdrive --access-token "$tok" info "$f1"
done < <(gdrive --access-token "$tok" list --bytes | grep 'INC')
  • read -r f1 _ reads a line from stdin, split the line on space character, then assigns variable f1 to the 1st column of the line.
  • <(gdrive ... ) feeds the output of the command to the stdin of while loop via the redirect.
  • Related