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
Related
This question already has answers here:
While loop stops reading after the first line in Bash
(5 answers)
Closed 2 years ago.
I made a bash script for my personal usage which sets up selenium webdriver with appropriate options . Here is its raw link - https://del.dog/raw/edivamubos
If i execute this script using curl after writing it to a file first like..
curl https://del.dog/raw/edivamubos -o test.sh && \
chmod u+x test.sh && \
bash test.sh
The script works perfectly as its intended to work
But usually i like to execute scripts directly using curl , so when i do..
curl https://del.dog/raw/edivamubos | bash
The script works very weirdly , it keeps repeating line 22,23 and 29 infinitely on loop. i couldnt beleive it as first so i tested this 3,4 times and can confirm it.
Now
what is the reason for same script acting differently in both cases ?
How do i fix it ( ie make it work correctly even after executing it directly without writing to a file )
Edit -
If someone want they can quickly test this in google colab ( in case someone intending to test but don't want to install any packages locally ) . I am mentioning this thing because you won't be able to reproduce this properly in any bash IDE.
When you pipe the script to bash, this command (line 24):
read -p "Enter your input : " input
reads the next line (i.e. line 25, case $input in) because bash's stdin is connected to curl's stdout, and read reads from the same descriptor as bash.
To avoid that, the developer can change the script so that all input is read from /dev/tty (i.e. the controlling terminal). E.g.:
read -p 'prompt' input </dev/tty
Or the user can use one of the below, so that read reads from the terminal, not the descriptor it was read from.
bash -c "$(curl link)"
bash <(curl link)
This question already has answers here:
Why does shell ignore quoting characters in arguments passed to it through variables? [duplicate]
(3 answers)
Closed 3 years ago.
I have a shell script with 3 Parameters:
#!/bin/bash
VHOST=$1
EXCHANGE=$2
DELAY=$3
RABBIT_CMD="docker exec rabbit rabbitmqadmin -u rabbit -p rabbit --vhost=$VHOST"
COMPLEX_CMD="$RABBIT_CMD declare queue name=${EXCHANGE}-delay"
COMPLEX_CMD="$COMPLEX_CMD arguments="
COMPLEX_CMD="${COMPLEX_CMD}'{\"x-message-ttl\":$DELAY,\"x-dead-letter-exchange\":\"$EXCHANGE\", \"x-dead-letter-routing-key\":\"worker\"}'"
echo $COMPLEX_CMD
$COMPLEX_CMD
Now I call this script
./script.sh rdb blah 5000
The second last line echo $COMPLEX_CMD outputs the following line:
docker exec rabbit rabbitmqadmin -u rabbit -p rabbit --vhost=rdb declare queue name=blah-delay arguments='{"x-message-ttl":5000,"x-dead-letter-exchange":"blah","x-dead-letter-routing-key":"worker"}'
When I copy-paste this into my bash and execute it, it works without any problems. But when I want to execute this in the script (last line $COMPLEX_CMD), I get the following error:
ERROR: Could not parse JSON:
'{"x-message-ttl":5000,"x-dead-letter-exchange":"blah","x-dead-letter-routing-key":"worker"}'
How do I have to escape my strings within the JSON in the right manner?
Found the solution: Just put eval before the $COMPLEX_CMD, so change the last line to
eval $COMPLEX_CMD
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?
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"
This question already has answers here:
Looping through lines in a file in bash, without using stdin
(3 answers)
Execute a command on remote hosts via ssh from inside a bash script
(4 answers)
Closed 8 years ago.
I have a list of IP addresses I need to ssh into, making unique changes to each one. I tried doing
while read -r line; do
ssh -n $line; done < file
but I need to manually run commands and checks on each device. The second I login it kicks me out to the next one. Are there any read line or ssh options that can allow me to do this?
Tell read to use a different FD, then you can remove the -n from ssh.
while read -u 3 ...
do
...
done 3< file