WelcomeWelcome | FAQFAQ | DownloadsDownloads | WikiWiki

Author Topic: (tc) url to local path awk/sh  (Read 201 times)

Offline mocore

  • Sr. Member
  • ****
  • Posts: 419
  • ~.~
(tc) url to local path awk/sh
« on: March 14, 2023, 01:42:22 PM »

 url to local path
with awk to shell

idk if its useful for much tbh  :-\

Code: [Select]



# tc_uri
# $1=$url
# $2=$local_prefix_path
#
# eg tc_uri  "$url"  "$local_path_prefix"
#
# eg tc_uri http://tinycorelinux.net/13.x/x86/tcz/info.lst.gz /tmp
#
function tc_uri(){
 url="${1-'http://tinycorelinux.net/13.x/x86/tcz/provides.db.gz'}";
 shift
 echo "$url" | busybox awk -v path_prefix="$1" -F"/" '

function get_path(u    ,path ){
  colafter=5

  split(u,path,"/")
  path_length=length(path)
  p=""
  pc=0
  for(i in path) { 
   if(  i > colafter && i <= path_length  -1 ) {
    pc=(pc+1);
    if(pc>1){ p=p"/"path[i] }else{ p=path[i] }
   }
  }
  return p ;
 }

function mkpathstr(u){ 
               protocall=$1"//"
               domain=$2
               path=get_path($0)
               remotepath=$0
               file=$NF
               ver=$4
               arch=$5
               localpath=path_prefix"/"ver"/"arch"/"path"/"file
             }

{
  mkpathstr($0)

  # return vars from awk
  print protocall,  remotepath , path , file , ver , arch ,localpath
 
}

' ;  }

 url="$1"
 pathprefix="$2"

# read vars into script
read protocall remotepath path file ver arch localpath  < <( tc_uri "${@}" )

echo -e "vars:\nprotocall : $protocall \nremotepath : $remotepath \npath : $path \nfile : $file  \nver  : $ver \narch : $arch \nlocalpath : $localpath "

# eg ?..
# mkdir -p $(dirname $localpath)
# wget -O$localpath $remotepath





Offline mocore

  • Sr. Member
  • ****
  • Posts: 419
  • ~.~
Re: (tc) url to local path awk/sh
« Reply #1 on: March 18, 2023, 05:50:00 PM »
modified script
 - to use set to pass args from awk
 - remove the (bash) process substitution ::) ,
https://en.wikipedia.org/wiki/Process_substitution
https://stackoverflow.com/questions/30781969/difference-on-bash-and-ash-parentheses

..& found ash "process substitution" alternative using "file descriptors"  ???
https://stackoverflow.com/questions/30781969/difference-on-bash-and-ash-parentheses/69739256#69739256
https://unix.stackexchange.com/questions/309547/what-is-the-portable-posix-way-to-achieve-process-substitution/639752#639752


Code: [Select]

# test
# busybox ash -c '. ./tc-uri.sh ; echo "$# $@ >$path"'


protocall="";
remotepath="";
path="";
filename="";
ver="";
arch="";
localpath="";

# tc_uri
# $1=$url
# $2=$local_prefix_path
#
# eg tc_uri  "$url"  "$local_path_prefix"
#
# eg tc_uri http://tinycorelinux.net/13.x/x86/tcz/info.lst.gz /tmp
#
function tc_uri(){
 url="${1-"http://tinycorelinux.net/13.x/x86/tcz/provides.db.gz"}";
 shift
 echo "$url"  | busybox awk -v path_prefix="$1" -F"/" '

function get_path(u    ,path ){
  colafter=5

  split(u,path,"/")
  path_length=length(path)
  p=""
  pc=0
  for(i in path) {
   if(  i > colafter && i <= path_length  -1 ) {
    pc=(pc+1);
    if(pc>1){ p=p"/"path[i] }else{ p=path[i] }
   }
  }
  return p ;
 }

function mkpathstr(u){
               protocall=$1"//"
               domain=$2
               path=get_path($0)
               remotepath=$0
               file=$NF
               ver=$4
               arch=$5
               localpath=path_prefix"/"ver"/"arch"/"path"/"file
             }

{
  mkpathstr($0)

  # return vars from awk
  print protocall,  remotepath , path , file , ver , arch ,localpath
 
}

' ;  }

# echo $@

set $(tc_uri "${@}" ) ;

# echo $# $@
 

protocall="$1";
remotepath="$2";
path="$3";
filename="$4";
ver="$5";
arch="$6";
localpath="$7";

shift $#



« Last Edit: March 18, 2023, 05:53:39 PM by mocore »