bash can't capture output from aria2c to variable and stdout - bash

I am trying to use aria2c to download a file. The command looks like this:
aria2c --http-user=$USER --http-passwd=$usepw -x 16 -s 100 $urlPath
The command works perfectly from the script when run this way. What I'm trying to do is capture the output from the command to a variable and still display it on the screen in real-time.
I have successfully captured the output to a variable by using:
VAR=$(aria2c --http-user=$USER --http-passwd=$usepw -x 16 -s 100 $urlPath)
With this scenario though, there's a long delay on the screen where there's no update while the download is happening. I have an echo command after this line in the script and $VAR has all of the aria2c download data captured.
I have tried using different combinations of 2>&1, and | tee /dev/tty at the end of the command, but nothing shows in the display in realtime.
Example:
VAR=$(aria2c --http-user=$USER --http-passwd=$usepw -x 16 -s 100 $urlPath 2>&1)
VAR=$(aria2c --http-user=$USER --http-passwd=$usepw -x 16 -s 100 $urlPath 2>&1 | tee /dev/tty )
VAR=$(aria2c --http-user=$USER --http-passwd=$usepw -x 16 -s 100 $urlPath | tee /dev/tty )
VAR=$((aria2c --http-user=$USER --http-passwd=$usepw -x 16 -s 100 $urlPath) 2>&1)
VAR=$((aria2c --http-user=$USER --http-passwd=$usepw -x 16 -s 100 $urlPath) 2>&1 | tee /dev/tty )
VAR=$((aria2c --http-user=$USER --http-passwd=$usepw -x 16 -s 100 $urlPath) 2>&1 ) | tee /dev/tty )
I've been able to use the "2>&1 | tee" combination before with other commands but for some reason I can't seem to capture aria2c to both simultaneously. Anyone had any luck doing this from a bash script?

Since aria2c seems to output to stdout, consider teeing that to stderr:
var=$(aria2c --http-user=$USER --http-passwd=$usepw -x 16 -s 100 $urlPath | tee /dev/fd/2)
The stdout ends up in var while tee duplicates it to stderr, which displays to your screen.

Related

how to use trickle to limit upload bandwith from .sh file?

I want to limit the upload bandwidth limit of the linux version of 115.com webapp. This webapp actually is run by "sh /usr/local/115/115.sh". If I do
trickle -s -u 5 sh /usr/local/115/115.sh, then the upload limit is not in effect.
The inside of /usr/local/115/115.sh is
#!/bin/sh
export LD_LIBRARY_PATH=/usr/local/115/lib:$LD_LIBRARY_PATH export PATH=/usr/local/115:$PATH
/bin/bash -c "exec -a $0 /usr/local/115/115 > /dev/null 2>&1" $0
I feel I need to put the trickle command inside the 115.sh. How exactly should I do it? Thanks
I tried
trickle -s -u 5 /bin/bash -c "exec -a $0 /usr/local/115/115 > /dev/null 2>&1" $0
/bin/bash -c "exec -a trickle -s -u 5 $0 /usr/local/115/115 > /dev/null 2>&1" $0
and
/bin/bash -c "exec -a $0 trickle -s -u 5 /usr/local/115/115 > /dev/null 2>&1" $0
but still the speed limit is not effective.

how to terminate a process via key stroke

I have this function on my bash script:
sudo tshark -i eth0 -T fields -e ip.src -e dns.qry.name -Y "dns.qry.name~." -q 1>>log.txt 2>/dev/null &
while true
do
cat log.txt
done
it is capturing ips and domain names in live mode and save them into log file.
how can configure this live mode to be terminated by pressing a key?
Using tee to watch log and send the command to background, then read input to terminate script
tshark -i eth0 -T fields -e ip.src -e dns.qry.name -Y "ip" -q 2>/dev/null | tee log.txt &
read -n1 c && echo "Got key $c"
exit
Note: running the command in a console will terminate it :-p

Bash Upload file over Netcat

Im trying to write a bash script that will curl a file and send it to my server over netcat then sleep (10) and send another file and sleep for 1hour then repeat all the process.
the first file is uploaded successfully but the second file : NO, i don't know what wrong with my code.
Ant help will be appreciated.
#!/bin/bash
file="curl -L mydomain.net/file.txt -o file.php"
file2="curl -L mydomain.net/file2.txt -o file2.php"
while true
do
if cat <(echo "${file}") | nc -u 120.0.0.1 4444 -w 1
echo -e "\e[92m[*][INFO] file1 uploaded"
sleep 10
then
cat <(echo "${file2}") | nc -u 120.0.0.1 4444 -w 1
echo -e "\e[91m[*][INFO] file2 uploaded"
sleep 3600
fi
done

Run command if screen exists

I have a shell script that is supposed to kill a detached screen session if it exists. The script is
if ! screen -list | grep -q "ScreenName"; then
screen -S ScreenName -X quit
fi
Unfortunately it seems that the screen -S ... -X quit command always runs bc when I run the script and the screen session doesn't exist it still outputs "No screen sessions found." When I remove the ! and the screen session is running, it doesn't kill the running session.
What could the problem be? Thanks
To get rid of the "No screen sessions found" message, redirect the stderr of screen:
screen -list 2>/dev/null
Next, keep in mind that -X doesn't work if the session is password protected.
Then, use -r to let screen know you only want to kill detached sessions, and there's no more need for grep and if.
Putting it all together:
screen -r -S "ScreenName" -X quit 2>/dev/null
grep has a confusing convention for return codes. It returns 0 when a string is found and returns 1 when there is no match. You should omit the ! in the condition.
#Sir Athos answer is pretty good, if you don't want to ignore valid errors, or just want additional ways to do things in the future, you can use this as a reference:
screen -ls 2>&1 | grep '(Detached)' | grep -o 'ScreenName' | xargs -I{} -n 1 -r screen -r -S {} -X quit
screen -ls 2>&1 List sessions, all output to stdout
grep '(Detached)' Filter for detached sessions
grep -o 'ScreenName' Filter for ScreenName and only output ScreenName
xargs -I{} -n 1 -r screen -r -S {} -X quit Run output through xargs -n 1 one at a time, -r don't run if there is no output, -I{} use {} as the replacement location for your argument since it's not at the end, and run your command
Code Sample:
evan> screen -ls
There are screens on:
15491.pts-2.x (08/29/2013 10:43:53 AM) (Detached)
31676.pts-41.x (08/28/2013 10:55:00 AM) (Attached)
2 Sockets in /var/run/screen/S-evan.
evan> screen -ls 2>&1 | grep '(Detached)' | grep -o '15491.pts-2.x' | xargs -I{} -n 1 -r screen -r -S {} -X quit
evan> screen -ls
There is a screen on:
31676.pts-41.x (08/28/2013 10:55:00 AM) (Attached)
1 Socket in /var/run/screen/S-evan.
evan> screen -ls 2>&1 | grep '(Detached)' | grep -o '15491.pts-2.x' | xargs -I{} -n 1 -r screen -r -S {} -X quit
evan>

Bash piping issue

I need to execute the following grep query as an argument for konsole (the kde terminal)
grep -R -i -n -A 2 -B 2 --color=always -R "searchtext" * | less -R
works for the current terminal.
konsole --workdir `pwd` -e grep -R -i -n -A 2 -B 2 --color=always -R "searchtext" * | less -R
works, but the konsole window displays the grep query without less pipe.
Ideally I want konsole to spawn as seperate process with konsole &
and send the grep command with less as an argument for konsole -e
You need to run the pipe in a shell.
konsole --workdir pwd -e bash -c 'grep -R -i -n -A 2 -B 2 --color=always -R "searchtext" * | less -R'

Resources