Buffer for reading script in bash - bash

I have a problem with running script under bash. When the script is started and while it is executing some long operation and I change that script, after long operation is finished bash is reading rest of the script and fails with silly errors like below:
test.sh: line 1093: unexpected EOF while looking for matching `"'
test.sh: line 1098: syntax error: unexpected end of file
If I run the same script (without further changes) there are no error whatsoever.
I suspect there is a buffered reading done by bash. Is there something I can do to make bash reading script in whole?

Bash reads the script line by line. If you change the length of a line that's before the current position, bash will start reading the next line from the middle of a line and most probably fail.
Don't change the source of a running script, make a copy.

Probable duplicate of the question asked:
Edit shell script while it's running
In particular, I like the answer at the comment referenced in the link (involves use of the source command and seperate .sh files).

I found a solution: to add {} like
#!/bin/bash
{
code goes here
}
This way everything inside {} will be read into memory.

Related

Bash adding unknown extra characters to command in script

I am currently trying to create a script that executes a program 100 times, with different parameters, typically pretty simple, but it's adding strange characters into the output filename that is passed into the command call for the program, the script i have written goes as follows
#!/bin/bash
for i in {1..100}
do ./generaterandomizedlist 10 input/input_10_$i.txt
done
I've taken a small screenshot of the output file name here
https://imgur.com/I855Hof
(extra characters are not recognized by chrome so simply pasting the name doesn't work)
It doesn't do this when i manually call the command issued in the script, any ideas?
Your script has some stray CRs in it. Use dos2unix or tr to fix it.

Bash script OS detect and if and wget

Bash script OS detect and if and wget problem. I tried os bit detect itself and it work normaly.
Edit: I found that was space after every "fi"
Ful bash http://pastebin.com/ENVYmXsU
But now i get unexpected end of file.
Edit: I copied same text from pastebin i copied to pastebin and now it works. How is that possible ? Pastebin add extra empty lines This is orig file http://www57.zippyshare.com/v/15049778/file.html
I keep getting
[root#localhost ~]# sh ioncube.sh
: command not found
ioncube.sh: line 28: syntax error near unexpected token `fi'
'oncube.sh: line 28: `fi
The code:
if [ `getconf LONG_BIT` = "64" ]
then
# 64-bit stuff here
wget http://downloads3.ioncube.com/loader_downloads/ioncube_loaders_lin_x86-64.tar.gz
else
# 32-bit stuff here
wget http://downloads3.ioncube.com/loader_downloads/ioncube_loaders_lin_x86.tar.gz
fi
I think we would need to see line 28 of the script; as that's where the error occurred; you have only posted 14 lines, and which comprise only a single control statement and two commands. FWIW, that code looks' like it should run, although you may wish to put the URI's in "quotes", incase they contain unusual characters.
You must have had Windows newlines in the file. It's a common problem when using Windows editors for *nix shell scripts. – l0b0

rtorrent execute shell script

I can't figure out how to get output from shell script back to rtorrent after command has been executed.
Is it possible to return back output from exeternal command back to rtorrent session?
I use rtorrent scripting interface to auto execute shell command after torrent is finished
event line in .rtorrent.rc looks like this:
system.method.set_key = event.download.finished,mycustomcommand,"execute=~/myshellscript.sh"
myshellscript.sh file looks like this
#!/bin/sh
echo "Torrent finished!"
Is there a way to do this?
I'm not sure what you're searching for, but I found this on rtorrent's wiki site:
execute_capture_nothrow={command,arg1,arg2,...}
This will execute the external command with arguments arg1,arg2,.... It will return the
stdout output of the command.
system.method.set_key = event.download.finished,mycustomcommand,print="$execute_capture=/path/to/script"
should work, at least
print="$execute_capture=/path/to/script"
works when you do it inside rtorrent. If you want to store the output then intstead of print use d.custom1.set= if that helps.
You forgot to add parameters to the rtorrent.rc itself and also the bash script is incomplete according to me.
.rtorrent.rc line should have
method.set_key = event.download.finished,whatever,"execute2={/path/myscript.sh,$d.name=,$d.base_path=,$d.hash=}"
bash script
#!/bin/bash
TORRENT_NAME=1
TORRENT_PATH=2
TORRENT_HASH=3
touch "$1" Finished download!
exit
this will create touch file telling you particular file has finished downloading.

Bash Script File Descriptor echo

echo: write error: Bad file descriptor
Throughout my code (through several bash scripts) I encounter this error. It happens when I'm trying to write or append to a (one) file.
LOGRUN_SOM_MUT_ANA=/Volumes/.../logRUN_SOMATIC_MUT_ANA
I use the absolute path for this variable and I use the same file for each script that is called. The file has a bunch of lines just like this. I use the import '.' on each script to get it.
echo "debug level set for $DEBUG_LEVEL" >> ${LOGRUN_SOM_MUT_ANA}
Worth noting:
It typically happens AFTER the FIRST time I write to it.
I read about files 'closing' themselves and yielding this error
I am using the above line in one script, and then calling another script.
I'd be happy to clarify anything.
For others encountering the same stupid error under cygwin in a script that works under a real Linux: no idea why, but it can happen:
1) after a syntax error in the script
2) because cygwin bash wants you to replace ./myScript.sh with . ./myScript.sh (where dot is the bash-style include directive, aka source)
I figured it out, the thumb drive I'm using is encrypted. It outputs to /tmp/ so it's a permission thing. That's the problem!

How can I call a Matlab function that takes text input from the command line?

I'm working on a medical robotics project that captures a series of images and then does some processing on them in MATLAB. Since a number of other things have to be done outside MATLAB, I'm using another language for the overall control, and using console commands to trigger other portions.
I have a single .m file with a single function that takes the filepath to the directory the images are in and does all the MATLAB processing. How can I call this from the command line? I've seen matlab -r "function(input)" discussed in some other answers here, but it doesn't work for me (I get a syntax error at the open paren). More specifically, I get: matlab: eval: line 1690: syntax error near unexpected token '('.
I've seen a few people saying this has to be done by calling a shell script (which I have no idea how to write), but a number of other people saying it's doable without that, can anyone clarify?
Additionally, assuming I've merely botched the matlab -r syntax, how does MATLAB know where to find the .m file? Does it need to be in whatever directory I'm running the command from?
I am guessing you are trying:
matlab -r test('hi')
and getting...
bash: syntax error near unexpected token `('
or something similar?
Answer: You need to use " " around the function(input), ie:
matlab -r "test('hi')"
This runs test.m in my current directory with the input string 'hi'.
To do this in a shell script named runmatlabcommand.sh, in say bash, you can just open a file and write:
#!/bin/bash
matlab -r "test('hi')"
and then execute this script from the command line by typing ./runmatlabcommand.sh. Make sure the script has execute permissions before you try to run it ;)

Resources