lftp bash script end of file unexpected [duplicate] - bash

This question already has an answer here:
in bash, heredoc inside function returns syntax error
(1 answer)
Closed 2 years ago.
I'm trying to put the result in a variable but it didn't work
Syntax error: end of file unexpected (expecting ")")
#!/bin/bash
test="$(lftp -u ns454575.ip-154-145-259.eu,NGFYv85MTI ftpback-rbx2-588.mybackup.ovh.net << EOF
cd /backup/site
ls
bye
EOF)"

You shouldn't add something after here docs. You can validate this by using shellcheck.
#!/bin/bash
test="$(lftp -u ns454575.ip-154-145-259.eu,NGFYv85MTI ftpback-rbx2-588.mybackup.ovh.net <<-EOF
cd /backup/site
ls
bye
EOF
)"
p.s. you know that test is a command as well? :-)

Related

Export variable in remote .bashrc [duplicate]

This question already has an answer here:
How have both local and remote variable inside an SSH command
(1 answer)
Closed 1 year ago.
Using Linux and bash:
in remote .bashrc file I've export FOO="hello"
locally I run this command but none variable was showed
ssh user1#192.168.1.114 ". /home/user1/.bashrc && echo \$FOO"
How could I get remote FOO variable?
I suggest with bash:
source <(ssh user1#192.168.1.114 cat /home/user1/.bashrc)
declare -p FOO
Output:
declare -x FOO="hello"
I assume /home/user1/.bashrc contains export FOO="hello".

Change directory in script bash [duplicate]

This question already has answers here:
Why can't I change directories using "cd" in a script?
(33 answers)
Find file then cd to that directory in Linux
(12 answers)
Make a Bash alias that takes a parameter?
(24 answers)
Closed 2 years ago.
I want to find a file and goto his directory. I made a bash script :
#!/bin/bash
FILE=$1
FILEPATH=`find . -name "$FILE"`
if [ -f "$FILEPATH" ]
then
cd $(dirname "$FILEPATH")
fi
But this script does not work. I saw on this post that I have to add exec bash or $SHELL but it create a new bash prompt ans display my welcome message.
How can I do ? I just want a script, alias or something to find a file and go to the directory containing that file.
Source your script instead of running it like you do. When you run it like you do, you spawn a new shell that executes the cd, completes succesfully, closes the shell and returns to your current shell, leaving you in your pwd.
Use source myscript.sh or . myscript.sh instead of bash myscript.sh or myscript.sh.

Unable to cat a data file into a script file (bash) on terminal command line [duplicate]

This question already has answers here:
Terminal - command not found
(2 answers)
Closed 3 years ago.
Im new to bash, and I am unable to cat a file and use the pipe command on the terminal using bash.
This is what ive tried on the terminal command line
$ cat data | readlooptest
however i always get this message when i use the pipe |
-bash: readlooptest: command not found
I have a Script named readlooptest, and a data file
script contents of readlooptest
#!/bin/bash
read myLine
sum=0
for i in $myLine
do
sum=`expr $sum + $i`
done
echo "sum is: $sum"
data file contents are
6 4 4 7 7
So once the commands are entered in terminal, the output should be
$ chmod +x readlooptest
$ cat data | readlooptest
sum is : 28
However I get
-bash: readlooptest: command not found
If readlooptest is not installed in one of your $PATH directories, you have to give the path to it to run it.
So for your piped cat, if you are cd in the same directory as readlooptest:
cat data | ./readlooptest
This has nothing to do with piping or cat. The actual problem is that you need to specify where readlooptest is, since it's not in your PATH. If it's in the working directory, simply add ./ to the start:
$ cat data | ./readlooptest

"basename" used in subshell returns "command not found" [duplicate]

This question already has an answer here:
Find "command not found" when executed in bash loop
(1 answer)
Closed 4 years ago.
When running this script:
#!/bin/sh -ex
if [[ $# -ne 1 ]]; then
echo "./import-public-ssh-key.sh <absolute path to public key>"
exit 1;
fi
PATH=$1
KEY=$(basename ${PATH})
I get:
./import-public-ssh-key.sh: line 9: basename: command not found
without the subshell basename works:
$ basename /Users/mles/.ssh/id_rsa.pub
id_rsa.pub
Why is basename not working in the subshell? I'm on a mac if this is relevant.
You reset the PATH. Don't do that. The shell searches all the directories listed in PATH, and you have changed it so that PATH no longer contains the directory that contains basename.

Error with simple shell script [duplicate]

This question already has answers here:
I am getting error "array.sh: 3: array.sh: Syntax error: "(" unexpected"
(3 answers)
Closed 6 years ago.
I wrote a shell script which automatically set up environment
#!/bin/sh
set path=(/dv/project/ $path)
I change the execution bit by
chmod +x init.sh
When I run it as
./init.sh
It prompted me with error
./init.sh: line 3: syntax error near unexpected token `('
./init.sh: line 3: `set path=(/dv/project/ $path)'
What could be the problem here? Thanks!
If using of set isn't required, just try this:
#!/bin/bash
path=(/dv/project/ $path)
As I have noticed, you're trying to extend your $PATH environment variable, right? There is a better way. Try this approach:
# Extend $PATH without duplicates
function _extend_path() {
if ! $( echo "$PATH" | tr ":" "\n" | grep -qx "$1" ) ; then
PATH="$1:$PATH"
fi
}
# Add custom bin to $PATH
[ -d ~/.bin ] && _extend_path "$HOME/.bin"

Resources