bashscript Heredoc + FTP error - linux

I try to do this
#!/bin/bash
ftp "$HOST"$3"/"$2"/" <<EOD
#toggle Interactive mode
prompt off
lcd $5"/"$4
mget "$4"*
exit
EOD
I get the following error
syntax error: unexpected end of file
When I changed it to or any other possibility
ftp "$HOST"$3"/"$2"/" <<<EOD
#toggle Interactive mode
prompt off
lcd $5"/"$4
mget "$4"*
exit
EOD
I get
./download.sh: line 31: 87621 Segmentation fault: 11 ftp "$HOST"$3"/"$2"/" <<< EOD
./download.sh: line 20: prompt: command not found
./download.sh: line 21: lcd: command not found
./download.sh: line 22: mget: command not found
I am not sure how to fix this. What am I supposed to doooo O_O

On my Mac, the segmentation faults were produced by the comments in the script. Removing the lines with the trailing # would make it work.

many interactive commands don't really deal well with piped input. maybe try http://www.columbia.edu/kermit/ftpscripts.html ?
that said, many others seem to have had success doing what you're doing (e.g. http://www.unix.com/unix-advanced-expert-users/4189-automated-ftp.html ), so maybe you just have a quoting problem? try changing the command (ftp "$HOST"$3"/"$2"/") to just cat to see if the shell is properly passing there here-doc to it?
this, too, may lend insight if you end up needing to supply a password: http://www.stratigery.com/scripting.ftp.html

Related

os.system(cmd) call fails with redirection operator

My Python 3.7.1 script generates a fasta file called
pRNA.sites.fasta
Within the same script, I call following system command:
cmd = "weblogo -A DNA < pRNA.sites.fasta > OUT.eps"
os.system(cmd)
print(cmd) #for debugging
I am getting the following error message and debugging message on the command line.
Error: Please provide a multiple sequence alignment
weblogo -A DNA < pRNA.sites.fasta > OUT.eps
"OUT.eps" file is generated but it's emtpy. On the other hand, if I run the following 'weblogo' command from the command line, It works just find. I get proper OUT.eps file.
$ weblogo -A DNA<pRNA.sites.fasta>OUT.eps
I am guessing my syntax for os.system call is wrong. Can you tell me what is wrong with it? Thanks.
Never mind. It turned out to be that I was not closing my file, "pRNA.sites.fasta" before I make system call that uses this file.

(./runalg: line 25: syntax error near unexpected token 'set' )in Middle bury evaluation

I am testing stereo algorithm in MiddleBury evaluation. I am using cygwin in Windows to run the program.
After compiling tools, I have run the code. According to the guide, I need to write as follows"
./runalg
But it says:
$ ./runalg
-bash: ./runalg: /bin/csh: bad interpreter: No such file or directory
So, I have changed like
bash ./runalg.
In this case, the error was as follows.
$ bash ./runalg
./runalg: line 25: syntax error near unexpected token `set'
./runalg: line 25: `if ($#argv > 3) set suffix = $4'**
Is there anyone, who can advice me how to use this evaluation and why this problem was occurred?
csh and bash/ksh-type shells have a totally different syntax. Except for very simple commands, you won't be able to get compatibility just by changing the interpreter.
If you had bash script and ksh interpreter, a few adaptations could make the script work in most cases but here no way!
ex in csh:
set suffix = $4
would translate to
suffix=$4
(or maybe export suffix=$4 I don't know the exact variables propagations in csh but that's not the point)
The best way is to actually install csh in Cygwin.
According to this forum, the C Shell is not installed by default in Cygwin. Startup the Setup program and select the "Shells" collection of packages and select csh for installation.

Lubuntu: Using .sh script as a keybind, code works, executing script gives errors

This is my first ever post on stackoverflow, hope I don't break any rules. I'm a complete Linux newbie (installed Lubuntu 14.04 64bit last night) so be duly warned.
In short, I'm trying to get my laptop touchpad toggle to work (Fn+F3 on my Inspiron5110). I have a bash script:
#!/bin/bash
if [ $(synclient -l | grep TouchpadOff | awk '{print $3}') == 1 ] ; then
synclient touchpadoff=0;
else
synclient touchpadoff=1;
fi
I got it from http://crunchbang.org/forums/viewtopic.php?id=10996 . If I paste the script code in the terminal and execute it, it works (touchpad goes on/off). However, I want to bind it to a key so in my lubuntu-rc.xml I've added the following:
<!-- disable touchpad -->
<keybind key="XF86TouchpadToggle">
<action name="Execute">
<command>/usr/local/bin/touchpad.sh</command>
</action>
</keybind>
When I press the necessary key combo however I get "Failure to execute child process "/usr/local/bin/touchpad.sh" (No such file or directory)". However I can see in this directory, both in the file manager and when I use ls in the terminal that the file is there:
/usr/local/bin$ ls -l
total 4
-rwxrwxr-x 1 paspaldzhiev paspaldzhiev 145 юни 2 22:54 touchpad.sh
I used chmod +x touchpad.sh to make it executable.
Now, where this gets even more confusing:
If I use bash /usr/local/bin/touchpad.sh I get:
paspaldzhiev#areuexperienced:/usr/local/bin$ bash touchpad.sh
touchpad.sh: line 6: syntax error near unexpected token `fi'
touchpad.sh: line 6: `fi'
Though as I've said above I know for a fact that the code works if I just paste it in the terminal.
Further, if I use ./touchpad.sh I get :
paspaldzhiev#areuexperienced:/usr/local/bin$ ./touchpad.sh
bash: ./touchpad.sh: /bin/bash^M: bad interpreter: No such file or directory
Just to note that I'm not very sure what the difference between bash touchpad.sh and ./touchpad.sh is in terms of execution, it's just that my more Linux-savvy friends told me to try these :D.
In any case, I have no idea how to proceed henceforth, could anyone please shed a light on what I'm doing wrong?
Thank you very much!
The ^M in your last error msg is your big hint ; -). Somehow you have used a windows editor, file transfer or something. Try dos2unix touchpad.sh. It will remove all the CR (^M) chars from end of lines. It should work then. Good luck. – shellter
There is no need for script, since there is no need for if instruction.
Place this piece of code in your lubuntu-rc.xml
<keybind key="XF86TouchpadToggle">
<action name="Execute">
<command>synclient TouchpadOff=$((1-$(synclient | grep TouchpadOff | awk '{print $3}')))</command>
</action>
</keybind>

Shebang causes script to fail

I'm quite bad at bash, and I try to make a script to connect to all my switches with openSSH in order to make some configuration.
I created an array containing all my 25 switches, and then I used a loop to open SSH connection with each of them.
As I'm on Windows and using bash, I've just installed Cygwin.
However, I had to use expect and writing my password in plain text as the switches are quite poor and that is the best way for me (I won't manually put my RSA key on every single switch as it would take me as much time as writing manually the configuration on every switch).
I use the shebang #!/usr/bin/expect -f to make bash recognize expect. When I do this, the expect syntax (spawn, expect, interact) works perfectly, but my array doesn't work.
I get the following error message:
extra characters after close-quote
while executing "arrayname=("172.21.21.20" "172.20.55.55" ... "
When I change the shebang, and use #!/bin/bash, expect is not found anymore :
./stationsnmp.sh: line 20: spawn : command not found couldn't read
./stationsnmp.sh: line 24: send : command not found couldn't read
file "assword": no such file or directory ./stationsnmp.sh: line 27:
send : command not found ./stationsnmp.sh: line 28: interact :
command not found
I'm really not a pro in bash, which explains I can't get this little problem... Some help would be welcome.
EDIT : Below is a part of my code
#!/bin/bash
switch=("172.20.0.229" "172.20.0.232" "172.20.0.233" "172.21.0.15" "172.21.0.16" "172.21.2.1" "172.20.2.250" "172.21.3.1" "172.20.3.250" "172.21.4.1" "172.20.4.250" "172.21.6.1" "172.20.6.250" "172.21.7.1" "172.20.7.250" "172.21.8.1" "172.20.8.250" "172.20.9.250" "172.21.9.1" "172.21.10.1" "172.20.10.250" "172.21.11.1" "172.20.11.250" "172.21.12.1" "172.21.12.250")
nmb=`echo ${#switch[#]}`
set timeout 3
for ((ii=0; ii<=$nmb; ii++))
#for ii in {0..${#switch[#]}}
do
if [ ${switch[$ii]:5:1} -eq 1 ]
then
ipdc=`echo ${switch[ii]} | grep -o -E '[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.'`"10"
spawn ssh admin#switch[$ii]
expect "*assword*"
send "PASS\r"
interact
exit
fi
done
You are mixing bash and expect, those are two entirely different languages. You probably want to have a bash wrapper script with proper option handling (see getopts) which takes a list of IP addresses and execute your expect script for each IP address passed to your bash-wrapper. If your expect script is small you might want to embed it into your shell script as opposed to having it in a separate file.
EDIT:
#!/bin/bash
switches=("172.20.0.229" "172.20.0.232")
for ip in "${switches[#]}"; do
expect "${ip}" <<-'EOT'
set host [lindex $argv 0]
set timeout 3
spawn ssh -l admin $host
expect "*assword*"
send "PASS\r"
interact
exit
EOT
done

backspace issue on linux file

I use EditPlus to write linux bash programs. after writting when i move files to server i get these kind of errors:
sleep: invalid time interval `2\r'
Try `sleep --help' for more information.
/usr/bin/program: line 9: $'\r': command not found
/usr/bin/program: line 10: $'}\r': command not found
/usr/bin/program: line 11: $'\r': command not found
/usr/bin/program: line 12: syntax error near unexpected token `$'{\r''
'usr/bin/program: line 12: `stop() {
I have also used dos2unix to fix this issue but no success. I want to know that is there any way that I can avoid this kind of problems. it waste alot of time. is there any better editor or some way to fix this issue?
EDIT:
What I do to solve this issue is that I vi a file in linux and then copy line by line from windows file to linux file and then it works. these are lines of my code:
line7:udevadm control --reload-rules
sleep 2
echo
echo $(date +"%b %e %T") hostname Program[$$]: Program Started | tee -a /var/log/messages
echo
}
stop() {
You need to change file preferences: go to Preferences -> Files and change "Create new file as " to UNIX. Also, your might want to enable "Check invalid CR/LF, null character when loading" option.
For already produced files
cat OLDFILE | tr -d '\r' > NEWFILE
should help. You can't just redirect the output to the input:
cat FILE | tr -d '\r' > FILE
since this will truncate the input file before it is read.
In contrast to similar programs, this is not eligible for the useless-use-of-cat award, since tr doesn't accept a filename as parameter.
Setting the "line ending sequence" in VSCode / atom from CRLF TO LF and saving worked for me

Resources