Я считаю, что ваш вопрос связан с этим: Цикл Bash для создания каталога, если числовой идентификатор найден в файле
Вы можете запускать все команды в одной функции awk system()
,просто организуйте их правильно, например:
# create the format text used in sprintf() to run the desired shell commands
cmd_fmt='mkdir -p "%s/%s" && cd "%s/%s" && curl -O -v -k -X GET %s -H "Content- Type:application/x-www-form-urlencoded" -H "Authorization:xxx" && { filename="%s"; unzip "${filename##*/}" && rm -f "${filename##*/}"; }'
# run the awk command
awk -v cmd_fmt="$cmd_fmt" '
# create an associative array (key/value pairs) based on the file1
NR==FNR { for(i=2; i<NF; i+=2) a[substr($i,1,7)] = $NF; next }
# retrieve the first 7-char of each line in file2 as the key to test against the above hash
{ k = substr($0, 1, 7) }
# if find k, then run the system command
k in a { cmd = sprintf(cmd_fmt, a[k], $0, a[k], $0, l, l); print(cmd) }
# save prev line to 'l' which is supposed to be the URL
{ l = $0 }
' RS= file1 RS='\n' file2
измените print
на system
для выполнения команды.
Примечание: вышеуказанные команды unzip
и rm
могут не работать, если имена файлов содержат символы в кодировке URL.
Обновление на основе вашего awk edit
:
Вы также можете просто напечатать необходимую информацию из строки awk
и затем обработать ее в bash, не нужно делать все в awk
(также удалите строку, чтобы определить cmd_fmt
в вашем awk edit
разделе):
awk '
# create an associative array (key/value pairs) based on the file1
NR==FNR { for(i=2; i<NF; i+=2) a[substr($i,1,7)] = $NF; next }
# retrieve the first 7-char of each line in file2 as the key to test against the above hash
{ k = substr($0, 1, 7) }
# if find k, then print
k in a { print a[k] "\t" $0 "\t" l }
# save prev line to 'l' which is supposed to be the URL
{ l = $0 }
' RS= file1 RS='\n' file2 | while IFS=$'\t' read -r base_dir sub_dir link; do
echo "download [$link] to '$base_dir/$sub_dir'"
# bash command lines to make sub-folders and download files
mkdir -p "$base_dir/$sub_dir"
cd "$base_dir/$sub_dir"
if curl -O -v -k -X GET "$link" -H "Content-Type:application/x-www-form-urlencoded" -H "Authorization:xxxx" >/dev/null 2>&1; then
echo " + processing $link"
# remove query_string from the link, since it might contains '/'
filename="${link%\?*}"
# remove path from filename and run `unzip`
unzip "${filename##*/}"
else
echo " + error downloading: $link"
fi
# return to the base directory if it's a relative path
# if all are absolute paths, then just comment out the following line
cd ../..
done
Примечание: Я не проверял строку curl
и не знаю, какие имена файлов могут быть для разных ссылок.filename="${link##*/}"
- удалить все символы до последнего символа '/', в результате чего останутся имя файла и потенциальные строки запроса."${filename%\?*}"
- удалить завершающие строки запроса из filename
.На самом деле имя файла, загружаемое с помощью вашей команды curl
, может отличаться, и вам придется проверять и корректировать его с конца.