-3

Context

I had a SO question successfully answered at https://stackoverflow.com/a/59244265/80353

I have successfully used the command that was given.

cap()(cd /tmp;rm -f *.vtt;youtube-dl --skip-download --write-auto-sub "$1";\
sed '1,/^$/d' *.vtt|sed 's/<[^>]*>//g'|awk -F. 'NR%8==1{printf"%s ",$1}NR%8==3'\
|tee -a "$2")

What does this command do?

  1. This command will download captions for a youtube video as a .vtt file from $1 parameter
  2. then print out the simplified version of the .vtt file into another file that's stated as parameter $2

This works as advertised.

How to call the command

In the terminal I will run the above command once and then run cap $youtube_url $full_path_to_output_file

What changes I would like

Currently, the $2 parameter must be a full path. Also currently, if the $2 parameter doesn't exist, an actual file will be created. What I would like is this behavior remains even for relative path. So hopefully for relative path, this behavior of creating a new empty file still works.

Update

I see that comments are such that there's nothing wrong with the command.

However, I did try running

cap $youtube_url $relative_path_to_a_text_file and it definitely did not work for me in macOS

Perhaps I am missing something else?

Update 2

This is a video of me running the awk sed command . First I did it with just a relative path. No output file shows up in the current working directory. The second shows me typing the full path and it works.

https://www.loom.com/share/1c179506fa5b48b4a3d62c81a9d2a411

I hope this clarifies the question i am raising and the commenters would kindly update their comments based on this video.

Kim Stacks
  • 10,202
  • 35
  • 151
  • 282
  • 2
    In the code shown, the second argument does not have to be a full path. It simply has to designate a file that can be created or appended to. You could use `elephant` or `../pachyderms/elephant` or `mammoths/frozen/elephant` and the `$2` would work fine as long as the directories `../pachyderms` or `mammoths/frozen` exist (the first one uses the current directory — that normally exists). – Jonathan Leffler Dec 10 '19 at 03:30
  • 2
    Rather difficult to fix things that are not broken... – David C. Rankin Dec 10 '19 at 03:42
  • @JonathanLeffler I have done a loom to clarify my question. Hopefully that shows why my impression that the awk, sed doesn't work for relative path – Kim Stacks Dec 19 '19 at 09:36

1 Answers1

1

EDIT: Adding a solution after OP's comment which do checks inside OP's function itself, warning not tested it though.

cap()(
user_path=$(echo "$path_details" | awk 'match($0,/.*\//){print substr($0,RSTART,RLENGTH)}')
path_details="$2"
PWD=`pwd`
cd "$PWD"
user_path=$(echo "$path_details" | awk 'match($0,/.*\//){print substr($0,RSTART,RLENGTH)}')
if [[ -d "$user_path" ]]
then
    echo "Present path $user_path."
    ##Call your program here....##
    cd /tmp;rm -f *.vtt;youtube-dl --skip-download --write-auto-sub "$1";\
    sed '1,/^$/d' *.vtt|sed 's/<[^>]*>//g'|awk -F. 'NR%8==1{printf"%s ",$1}NR%8==3'\
    |tee -a "$2"
else
    echo "NOT present path $user_path."
    ##Can exit from here. if needed.##
fi
)


I believe OP wants to check directory of relative path passed as 2nd argument, is present or not, if this is the case then one could try following.

cat file.ksh
path_details="$2"
PWD=`pwd`
##Why I am going to your path is, in case you are running this from cron, so in that case you can mention complete path here, rather than pwd as mentioned above.
cd "$PWD"
user_path=$(echo "$path_details" | awk 'match($0,/.*\//){print substr($0,RSTART,RLENGTH)}')

if [[ -d "$user_path" ]]
then
    echo "Present path $user_path."
    ##Call your program here....##
else
    echo "NOT present path $user_path."
    ##Can exit from here. if needed.##
fi


Explanation: Adding detailed explanation for above code.

cat file.ksh       ##For OP reference to show content I am using cat script_name here.
path_details="$2"  ##Creating variable path_details whose value is $2(2nd argument passed to script) 
PWD=`pwd`          ##Creating variable PWD whose value is pwd(current working directory).
##Why I am going to your path is, in case you are running this from cron, so in that case you can mention complete path here, rather than pwd as mentioned above.
cd "$PWD"          ##Going to current directory, why I did is you can set PWD above variable value as per your need and navigate to that path, this will help in case of script is running from Cron.
user_path=$(echo "$path_details" | awk 'match($0,/.*\//){print substr($0,RSTART,RLENGTH)}')  ##Now getting path details from passed 2nd argument for script.

if [[ -d "$user_path" ]]                        ##Checking if user_path(path value is existing on system)
then
    echo "Present path $user_path."
    ##Call your program here....##              ##If path existing then call your program.
else                                            ##If path NOT existing then exit from program or print message up to you :)
    echo "NOT present path $user_path."
    ##Can exit from here. if needed.##
fi                                              ##Closing if condition here.
RavinderSingh13
  • 130,504
  • 14
  • 57
  • 93
  • Sorry, i just saw this. how do I run this? Is this a bash script? previously i simply have a awk sed command, so how do I alter that to become what you just gave me? – Kim Stacks Dec 12 '19 at 05:40
  • @KimStacks, yes its a bash script, wherever I have written `call your program` you should call your code there and test it once and lemme know? – RavinderSingh13 Dec 12 '19 at 05:42
  • 1
    can u explain to me what the first line of your bashscript is doing? really appreciate your close attention – Kim Stacks Dec 12 '19 at 05:53
  • @KimStacks, Sure, I have added detailed information in my answer now, please check and lemme know in case of any queries. – RavinderSingh13 Dec 12 '19 at 06:15
  • Sorry I haven't had time to test as I was busy with work. Appreicate the detailed explanation but I'm still a bit lost as to how I use this script. – Kim Stacks Dec 19 '19 at 09:25
  • @KimStacks, Lets say you are running script `./file.ksh 1 /path/test/etc` then it is checking 2nd argument, then code is checking either than passed path is present or not on system, if present then your further program will be called is not then it will exit. This is what you require right? If I am not wrong here. – RavinderSingh13 Dec 19 '19 at 09:28
  • 1
    you're very helpful. more helpful than most. But this seems a bit overkill to use bashript just to call the awk-sed command. also, my first argument to the awk-sed command is a parameter that can be different each time. Does that get passed into the bashscript? Is there really no way for the original awk-sed command be modified? Did you see my loom video? I hope the loom video clarifies what my situation is. – Kim Stacks Dec 19 '19 at 09:39
  • @KimStacks, Actually I thought of a neat way, lemme quickly put something in awk itself, be here, will be back in few mins :) Sorry not sure about what is that loom video? – RavinderSingh13 Dec 19 '19 at 09:41
  • @KimStacks, but how you are calling this function then? Is it not a bash function, I believe this is a bash function right? – RavinderSingh13 Dec 19 '19 at 09:42
  • If u look at my question, I have embedded a video via a loom link. The loom video will show you how I am unable to run the command when I use relative path but perfectly when it's full path – Kim Stacks Dec 19 '19 at 09:43
  • @KimStacks, No sorry I haven't seen it, will check at home :) how about my previous comment's question to you :) – RavinderSingh13 Dec 19 '19 at 09:43
  • The `cap()` function is not in a bashscript. I simply run it in the terminal before I run `cap $youtube_url $path_to_output_file` – Kim Stacks Dec 19 '19 at 09:45
  • @KimStacks, Could you please check my EDIT solution once and lemme know if this helps you? Sorry couldn't test it, little difficult to test, lemme know. – RavinderSingh13 Dec 19 '19 at 10:00
  • It just tells me it's not present. I don't want to be negative but I have to be honest. This is not what I am looking for. I want it to *just work* even if I supply a relative path. Does that make sense? – Kim Stacks Dec 19 '19 at 10:25
  • @KimStacks, yeah I got that it should work ideally I didn't check since can't test your command lemme see it and get back to you on same. – RavinderSingh13 Dec 19 '19 at 10:28