LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-27-2023, 01:56 PM   #1
blason
Member
 
Registered: Feb 2016
Posts: 122

Rep: Reputation: Disabled
Stuck in loop and unable to find answer - Can someone please help?


Hi Team,

I need to write a code where I have certain urls entered in file. This I am taking as a loop and wanted to download those files and keep it separate with name. Once downloaded I can then concatenate those. Below are my codes but dang I am not able to store them in different file.
PHP Code:
cat $URL_FILE | while read line; do
 
curl_cli -/tmp/file --user $_USER:$_PASS ---cacert $DIR/conf/ca-bundle.crt --retry 10 --retry-delay 60 $line dos2unix grep -vE '^$'
       
done 
So in this example my $URL_FILE contains

http://download.example.com/file1.txt
http://download.example.com/file2.txt
http://download.example.com/file3.txt
http://download.example.com/file4.txt

And in that loop I want to keep output those files in separate file; so that at the end of loop I can then

cat file1.txt file2.txt file3.txt file4.txt | dos2unix | sort -n | uniq > /tmp/finalfile

However I am not able to achieve it; can someone please help?

TIA
Blason R
 
Old 03-27-2023, 02:44 PM   #2
business_kid
LQ Guru
 
Registered: Jan 2006
Location: Ireland
Distribution: Slackware, Slarm64 & Android
Posts: 16,297

Rep: Reputation: 2322Reputation: 2322Reputation: 2322Reputation: 2322Reputation: 2322Reputation: 2322Reputation: 2322Reputation: 2322Reputation: 2322Reputation: 2322Reputation: 2322
Quote:
Originally Posted by blason
However I am not able to achieve it;
Hi, not a coder, but that bit gets no replies, because coders need to see exactly what went wrong so they can solve it.
Do better, look at the errors you get, and try stuff.
 
Old 03-27-2023, 03:12 PM   #3
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,704

Rep: Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896
Code:
-o /tmp/file
With just a quick look your writing to file but then wanting to pipe the output?
Something like:
Code:
while read -r line; do
  base=$(basename $line)
  curl ... | dos2unix | grep -vE '^$' > /tmp/$base
done < "$URL_FILE"
cat /tmp/*.txt | sort -n | uniq > /tmp/finalfile
 
Old 03-27-2023, 05:00 PM   #4
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,781

Rep: Reputation: 2082Reputation: 2082Reputation: 2082Reputation: 2082Reputation: 2082Reputation: 2082Reputation: 2082Reputation: 2082Reputation: 2082Reputation: 2082Reputation: 2082
https://manned.org/curl.1
Code:
       -O, --remote-name
              Write output to a local file named like the remote file we get.
              (Only the file part of the remote file is used, the path is cut
              off.)
Alternatively, drop the -o entirely and put the pipe after the loop.
 
Old 03-27-2023, 05:30 PM   #5
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,704

Rep: Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896
To elaborate on ntubski's post you do not need to save them separately.
Code:
while read -r line
do
  curl ... $line
done < "$URL_FILE" | dos2unix | grep -vE '^$' | sort -n | uniq > "/tmp/finalfile"
 
1 members found this post helpful.
Old 03-29-2023, 08:30 AM   #6
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,794

Rep: Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201
And
Code:
grep -vE '^$'
can become
Code:
grep .
Consider having each $variable in quotes: "$line"
 
Old 04-01-2023, 10:40 PM   #7
blason
Member
 
Registered: Feb 2016
Posts: 122

Original Poster
Rep: Reputation: Disabled
Thanks folks that resolved my issue,
 
  


Reply

Tags
bash, script



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
can't someone help me i'am trying to install idevicerestore and when i do the sudo make i get this error can someone help me please shawn7226791 Linux - Software 4 09-18-2019 07:25 AM
I can not find answer of this question. Please post helpful answer. pritammt Linux - Newbie 8 05-30-2014 05:43 AM
ways of the path, could someone please answer Azfer Linux - Software 7 04-16-2004 05:06 AM
Can someone please answer plastickid Linux - Networking 3 12-15-2002 05:50 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:11 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration