Get running time of script in variable (bash) [duplicate] - bash

This question already has answers here:
get values from 'time' command via bash script [duplicate]
(4 answers)
How to store a substring of the output of "time" function in bash script
(3 answers)
Closed 5 years ago.
I'm looking to capture the execution time of an rsync transfer and then store that time in a variable for a later step in my bash script.
I have tried the following:
ELAPSED=$(time $(rsync -azh source/ dest &> /dev/null)) to no avail. The elapsed time is printed to the screen, but it is not saved in the ELAPSED variable.
I have also tried: ELAPSED=$(/usr/bin/time sh -c "rsync -azh source/ dest &> /dev/null") but this actually takes the sh output and stores that output into the variable, not the time.
Any insights on a better method or corrections to my attempts are appreciated in advance!

Related

How to write bash commands inside SFTP [duplicate]

This question already has answers here:
Using variables inside a bash heredoc
(3 answers)
Closed last year.
I have the below script to download today's file from the server
#!/bin/sh
IFS='
'
SYS_DT=$(date '+%d%h%Y')
SYS_FILE='BOMExtract_'$SYS_DT'.xlsx'
sshpass -p "123" sftp "admin#XXXX" << 'EOF'
cd /u01/admin/Oracle
lcd /u01/usr
get $SYS_FILE
But it is not taking the value of SYS_FILE in getting command. Can anyone please help to write bash inside sftp commands?
I removed the quotes around EOF and that fixed the issue.Thanks Barmar

Golang - how to spawn a process that takes a glob pattern as one of its command-line arguments? [duplicate]

This question already has answers here:
golang failed exec command that works in terminal
(1 answer)
How to pipe several commands in Go?
(8 answers)
Golang exec command chmod returning error
(2 answers)
Closed last year.
From my bash shell, I am able to run this following command successfully - scp <local-folder>/* <user>#<remote-host>:/var/tmp. All files in my <local-folder> are copied onto the remote location.
Now I am trying to run the above same command from my Go program, with exec.Command() but scp complains of "no such file / directory - <local-folder>/*' - the '*' is literally taken as a filename. I want to replicate the same behaviour of scp I get when I run it from my Bash shell. Here is the code snippet I am using:
pushCmd := exec.Command("scp", "<local-folder>/*", "<user>#<remote-host>:/var/tmp")
pushCmdOutput, err = pushCmd.CombinedOutput()
fmt.Fprintln(os.Stderr, string(pushCmdOutput))
Thanks in advance.

Bash executes comands out of sequance sometimes [duplicate]

This question already has answers here:
Why didn't the shell command execute in order as I expect?
(4 answers)
bash script order of execution
(2 answers)
Closed 3 years ago.
I running multiple commands in bash in parallel and need to get output that is delimited so receiving script could separate the values.
I attempted to do this in few ways but it seems that any echo is executed instantly and anything following after.
So I am trying to find a way to separate input from each output with separator preceding output.
I actually use curl request that may take 50-200ms to respond, but here for simplicity I will give example with time command.
Here is rough example:
echo ">" && time &
echo ">" && time &
echo ">" && time &
wait
This produces >>> time time time
I am looking for a way to make it produce >time>time>time
I had some success trying to call other bash scripts with trailing echo command instead of making actual commands and that works most of the time but inevitably things get mixed up because of timing.
I will post updates as I work on it, thank you for the help
Try this:
echo ">$(time)" &
echo ">$(time) &
echo ">$(time)" &
wait
That tells echo that it needs the output of the time command you have before it can do its thing.

rsync remote to local - Unexpected remote arg [duplicate]

This question already has answers here:
How to execute a bash command stored as a string with quotes and asterisk [duplicate]
(5 answers)
Why does shell ignore quoting characters in arguments passed to it through variables? [duplicate]
(3 answers)
Closed 4 years ago.
I'm trying to assemble an rsync command in a bash variable and then execute it.
It looks something like this:
CMD="$RSYNC -a $REMOTE $LOCAL $LINK_DEST"
It gets executed like this
RSYNC_RESULT=$($CMD)
This works fine until I try to add add --rsync-path="sudo /usr/local/bin/rsync" to the mix (so that rsync runs as root on the remote).
RSYNC_PATH='--rsync-path="sudo /usr/local/bin/rsync"'
CMD="$RSYNC -a $RSYNC_PATH $REMOTE $LOCAL $LINK_DEST"
Now I get an error
Unexpected remote arg: user#remote.local:/Users/user/files/
rsync error: syntax or usage error (code 1) at main.c(1343) [sender=3.1.2]
I'm fairly certain it's connected to the quoting in the $RSYNC_PATH var and/or the $($CMD) bit, because I can paste the resulting command into a shell and it runs successfully.
Any ideas what I can do to make this work?

Bash: Set up a new command on a new line without executing [duplicate]

This question already has an answer here:
How to prefill command line input
(1 answer)
Closed 6 years ago.
I'm trying to write a BASH script to output a partially completed command which I can then add parameters to, hit ENTER and then run. I want this to be implemented completely in BASH.
e.g.
~> ./test.sh
~> ls -al <CURSOR POSITION HERE>
The only variable I've found that's close is the PROMPT_COMMAND variable, which when set inside test.sh to 'ls -al', will then immediately execute it once the script has exited.
Is there a way to stop the immediate execution, so I can add, say, *.log?
How about
read -e -p"$PWD> " -i"ls -al " cmd; eval "$cmd"

Resources