smbclient: command not found - linux

I would like to connect to a remote server with the smbclient command and pass some arguments to the script.
Here is my command:
smbclient //$SERVER -c 'cd $PATH;get $FILE /tmp/$FILE' $PASS -U$PSEUDO -W domain
When I launch this command without variables on the command line it works. But when I use it in the script it says:
./test1.sh: line 14: smbclient: command not found
Why is that?
Here is my complete script with for exemple arguments testSRV, testPATH and testFile :
\#! /bin/bash
SERVEUR=$1
PATH=$2
FILE=$3
echo $PATH #Return testPATH
echo $FILE #Return testFILE
\#COMPLETEPATH="cd $testPATH;get $testFILE /tmp/$testFILE"
\#echo $COMPLETEPATH //return
/usr/bin/smbclient //$SERVER -c 'cd $PATH;get $FILE' testpassword -U testuser -W testdomain

It's good,
Thanks you all but I achieved to make my script works.
I tried to replace single quote with double quote and put the full command path

You have two options: ensure /usr/bin is on your PATH (echo $PATH to see if this is the case) or edit the script to call smbclient with the full path (/usr/bin/smbclient)

Related

Using escape characters inside double quotes in ssh command in bash script

I want to run some commands each time when I log in to a remote system. Storing commands in .bashrc on remote is not an option.
What is the proper way to escape the escape chars inside of quotes in bash script for ssh?
How can I write each command in new line?
My script
#!/bin/bash
remote_PS1=$'\[\033[01;32m\]\u#\[\033[03;80m\]\h\[\033[00m\]:\[\033[01;34m\]\!:\w\[\033[00m\]\$ '
ssh -t "$#" 'export SYSTEMD_PAGER="";' \
'export $remote_PS1;' \
'echo -e "set nocompatible" > /home/root/.vimrc;' \
'bash -l;'
didn't work.
Escaping escape characters inside double-quotes and run them on remote server is way too complicated for me :)
Instead, I wrote a remoterc file for remote and a small remotessh script.
In remotessh, first I copy remoterc on remote machine and run bash command with that remoterc file interactively.
remoterc:
#!/bin/bash
SYSTEMD_PAGER=""
PS1="\[\033[01;32m\]\u#\[\033[03;80m\]\h\[\033[00m\]:\[\033[01;34m\]\!:\w\[\033[00m\]\$ "
echo -e "set nocompatible" > /home/root/.vimrc
remotessh:
#!/bin/bash
scp remoterc "$1":/home/root/
ssh "$1" -t "bash --rcfile remoterc -i"
It works :)
You can use Bash's printf %q.
According to help printf:
%q      quote the argument in a way that can be reused as shell input
See the following example:
$ cat foo.sh
ps1='\[\033[1;31m\]\u:\w \[\033[0m\]\$ '
ps1_quoted=$( printf %q "$ps1" )
ssh -t foo#localhost \
'export FOO=bar;' \
"export PS1=$ps1_quoted;" \
'bash --norc'
Result:

Bash: Creating a shell variable in a bash script that I can access from command line

I have very little experience working with bash. With that being said I need to create a bash script that takes your current directory path and saves it to a shell variable. I then need to be able to type "echo $shellvariable" and have that output the directory that I saved to that variable in the bash script. This is what I have so far.
#!/bin/bash
mypath=$(pwd)
cd $1
echo $mypath
exec bash
now when I go to command line and type "echo $mypath" it outputs nothing.
You can just run source <file_with_your_vars>, this will load your variables in yours script or command line session.
> cat source_vars.sh
my_var="value_of_my_var"
> echo $my_var
> source source_vars.sh
> echo $my_var
value_of_my_var
You have to export the variable for it to exist in the newly-execed shell:
#!/bin/bash
export mypath=$(pwd)
cd $1
echo $mypath
exec bash
Hello
'env -i' gives control what vars a shell/programm get...
#!/bin/bash
mypath=$(pwd)
cd $1
echo $mypath
env -i mypath=${mypath} exec bash
...i.e. with minimal environment.

Perl's %ENV doesn't works in one-liner

I write simple bash line that should replace the LOGIN word in some bash script (will replace the word LOGIN to admin word)
But it doesn’t work?
But when I type bash command on my Linux/solaris machine and then run separately the commands then its work
so why the bash one liner not work ( what’s the diff here ? )
bash one liner line
/tmp ROOT > bash -c 'export LOGIN=admin ; /usr/local/bin/perl -i -pe 's/LOGIN/$ENV{LOGIN}/' /tmp/pass_login.bash'
ENV: Undefined variable.
run command separately under bash shell ( works fine )
/tmp ROOT > bash
bash-3.2# export LOGIN=admin
bash-3.2# /usr/local/bin/perl -i -pe 's/LOGIN/$ENV{LOGIN}/' /tmp/pass_login.bash
.
my script
more pass_login.bash
#!/bin/bash
MY_LOG_NAME=LOGIN
Doesn't look to me like you have your quotes/variables escaped properly. Try this instead:
bash -c 'export LOGIN=admin ; /usr/local/bin/perl -i -pe "s/LOGIN/\$ENV{LOGIN}/" /tmp/pass_login.bash'

Error q : Command not found on Vim

I created a bash script :
#!/bin/bash
su root -c vim $1 -c ':%s/^M//g' -c 'wq'
My script has to remove all the ^M (carriage return on Windows) on my file, then save it.
When I execute my script it returns :
/sequenceFiles/Sequence1.seq: wq: command not found
Does someone know why ?
Thanks for your help.
The -c is seen by su, not vim, and the shell complains about the unknown command.
You need to pass the command as one argument to su:
su root -c "vim $1 -c ':%s/^M//g' -c 'wq'"
man su says:
`-c COMMAND'
`--command=COMMAND'
Pass COMMAND, a single command line to run, to the shell with a
`-c' option instead of starting an interactive shell.
Try
su root -c "vim $1 -c ':%s/^M//g' -c 'wq'"
While you can do it with vim, consider simpler:
perl -pi -e 's/\r\n/\n/' file

grep in bash script not working as expected

If I run
grep -i "echo" *
I get the results I want, but if I try the following simple bash script
#search.sh
grep -i "$1" *
echo "####--DONE--####"
and I run it with sh -x search.sh "echo" I get the following error output:
' grep -i echo '*
: No such file or directory
' echo '####--DONE--####
####--DONE--####
How come? I'm on CentOS
Add the sha-bang line at the top of your script
#!/bin/bash
and after making it executable, run the script using
./search.sh "echo"
The "sh -x" should print the files that '*' matches. It looks like it's not matching any files. Are you maybe running it in a directory with no readable files?

Resources