GET Dynamic Date file using SFTP? [duplicate] - bash

This question already has answers here:
Using variables inside a bash heredoc
(3 answers)
Closed 3 years ago.
I am creating a bash file which will download file daily but file name is dynamic it generates file on the basis of date something like '20190819.log' My code is as follows :
export SSHPASS=$SFTP_PASS
DATE=`$DATE -d "-1 day" +"%Y%m%d"`
sshpass -e sftp -oBatchMode=no -b - $SFTP_USER#$SFTP_HOST<<-'EOF'
mget $DATE.log
EOF
But in line mget $DATE.log it is treating $DATE as string.
Need help...Thanks in advance.

When you put quotes around the here-doc end delimiter (<<-'EOF' in this case), you are asking the shell to not expand parameters in the here-doc. If you want shell variables to be expanded, lose the quotes (<<-EOF).
But in this particular case, you might find that scp is easier to use.

Related

Bash while loop behave differently in file vs direct execution [duplicate]

This question already has answers here:
Loop through an array of strings in Bash?
(21 answers)
Closed 2 years ago.
The following is the sample while loop.
res=()
lines=$'first\nsecond\nthird'
while read line; do
res+=("$line")
done <<< "$lines"
echo $res
When i run this directly in terminal im getting the following out put.
first second third
But when run the same script by saving it to a file. Then im getting the following out put.
first
Why is it behaving differently?
Note: I tested with and without shebang in file. result is same.
If you have a string with embedded newlines and want to turn it into an array split on them, use the mapfile builtin:
$ mapfile -t res <<<$'first\nsecond\nthird'
$ printf "%s\n" "${res[#]}"
first
second
third

In shell script, how do I use the contents of a file as a parameter [duplicate]

This question already has answers here:
Need to assign the contents of a text file to a variable in a bash script
(4 answers)
Closed 4 years ago.
Suppose in dir.txt I have the following content:
test-dir
I try to use that as a parameter as follows:
echo dir.txt | cp * $1
I want the above to be the equivalent of:
cp * test-dir
What am I doing wrong?
You are giving the string "dir.txt" to a program that does not accept any input by stdin.
You are looking for the following syntax:
cp * "$(<dir.txt)"
$() runs the command inside parenthesis and substitutes its results in its position in the command line. The < is a shorthand to read a file (a cat would also work). The quotes are to avoid problems with spaces.
You can get content of file to variable:
file1=$(cat dir.txt)
echo $file1
Results:
test-dir

How should I sort one word using shell? [duplicate]

This question already has answers here:
How to sort characters in a string?
(5 answers)
Closed 4 years ago.
I can use every language to realize it, but I want to use shell.
Use shell command or shell script.
For example, I have one word: sport.
I want to get another word: oprst.
Now I want to use shell to bring about it, how to do? Thanks!
Use the following Shell Script:-
#!/bin/sh
sortedWord=`echo $1 | grep -o . | sort |tr -d "\n"`;
echo $sortedWord;

Bash "while read loop" does not properly recognize variables [duplicate]

This question already has answers here:
bash variable interpolation separate variables by a hyphen or underscore
(3 answers)
Error in string Concatenation in Shell Scripting
(3 answers)
Closed 5 years ago.
I am using MacBook pro terminal to execute a shell script. It loops through a text file and create filenames based on each line in the file.
#!/bin/bash
year=2010
list=list_test.txt
mydir=thisdir
i=1 # counter
while read line
do
echo $i $line
file1=`echo $mydir/file_$year_$line_test.tif`
file2=`echo $mydir/file_$year_$line_test.tif`
echo $file1 $file2
i=$(($i+1))
done < $list
However, the output is peculiar:
1 17019
thisdir/file_.tif thisdir/file_.tif
2 17029
thisdir/file_.tif thisdir/file_.tif
3 17039
thisdir/file_.tif thisdir/file_.tif
Within the loop, bash does not recognize some variables, like "year" which is a global, and "line" which is read from the text file. The text file is as below:
17019
17029
17039
Another script with exactly the same manner works very well. This is mysterious to me now.
Any help or comments are extremely appreciated! Thanks very much!
_ is a valid character for an identifier, but you want to use it as a literal character in the file name. You need to use the full form of parameter expansion, ${x} instead of $x.
(Also, the command substitution isn't necessary.)
file1=$mydir/file_${year}_${line}_test.tif
file2=$mydir/file_${year}_${line}_test.tif

Bash Script : How to pass a wildcard before my filename in "if" exist file? [duplicate]

This question already has answers here:
Check if a file exists with a wildcard in a shell script [duplicate]
(21 answers)
Closed 6 years ago.
I wish to be able to check if file exist.
if [ -f "/var/run/screen/user/*.$InstanceName" ]; then
echo -e "screen instance exist"
fi
but the wilcard / joker don't work
How I can pass it ?
Your wildcard doesn't work because it's quoted. Unquoting it however might break the [ command as it only expects one filename argument, and if two or more files wore globbed it would break.
In bash you can use compgen that will generate a list of files matching the globbing pattern, it will also set proper exit status if no globs are found, it is a hack? I don't know, but it could look like it:
if compgen -G "/var/run/screen/user/*/$InstanceName" > /dev/null; then
printf "screen instance exist\n"
fi

Resources