write to terminal via bash script - bash

I have to run some hundreds of simulations and scan the output file for a certain variable. In order to run the program, I need to write
$SIMPLESIM/simplesim-3.0/sim-outorder -config ../../config/tmp.cfg bzip2_base.i386-m32-gcc42-nn dryer.jpg
to the terminal, where tmp.cfg is the config file I will be modifying for each simulation. Running this outputs a file in which I named via tmp.cfg. This obviously works when I literally type it into terminal, however, in bash script, running this command gives me the error
simplesim-3.0/sim-outorder no such file or directory
I believe it has to do with the $ symbol? Thanks for any help.

Before calling any command its path must be defined in $PATH variable or you have to manually give the complete path to invoke it.
So define SIMPLESIM in script to the path, like SIMPLESIM=/usr/bin , this /usr/bin is for reference only.To know the path do echo $SIMPLESIM in terminal and see the path
and call the command $SIMPLESIM/simplesim-3.0/sim-outorder -config ../../config/tmp.cfg bzip2_base.i386-m32-gcc42-nn dryer.jpg

Related

Instead of giving command for batch mode, give .scm file path?

It is possible to supply batch commands directly with the -b flag, but if the commands become very long, this is no longer an option. Is there a way to give the path to an .scm script that was written to a file, without having to move the file into the scripts directory?
No as far as I know. What you give in the -b flag is a Scheme statement, which implies your function has already been loaded by the script executor process. You can of course add more directories that are searched for scripts using Edit>Preferences>Folders>Scripts.
If you write your script in Python the problem is a bit different since you can alter the Python path before loading the script code but the command line remains a bit long.

bash commands to remote hosts - errors with writing local output files

I'm trying to run several sets of commands in parallel on a few remote hosts.
I've created a script that constructs these commands, and then writes the output in a local file, something along the lines of:
ssh <me>#<ip1> "command" 2> ./path/to/file/newFile1.txt & ssh <me>#<ip2>
"command" 2> ./path/to/file/newFile2.txt & ssh <me>#<ip2> "command" 2>
./path/to/file/newFile3.txt; ...(same repeats itself, with new commands and new
file names)...
My issue is that, when my script runs these commands, I am getting the following errors:
bash: ./path/to/file/newFile1.txt: No such file or directory
bash: ./path/to/file/newFile2.txt: No such file or directory
bash: ./path/to/file/newFile3.txt: No such file or directory
...
These files do NOT exist but will be written. That being said, the directory paths are valid.
The strange thing is that, if I copy and paste the whole big command, then it works without any issue. I'd rather have it automated tho ;).
Any ideas?
Edit - more information:
My filesystem is the following:
- home
- User
- Desktop
- Servers
- Outputs
- ...
I am running the bash script from home/User/Desktop/Servers.
The script creates the commands that need to be run on the remote servers. First thing first, the script creates the directories where the files will be stored.
outputFolder="./Outputs"
...
mkdir -p ${outputFolder}/f{fileNumb}
...
The script then continues to create the commands that will be called on remotes hosts, and their respective outputs will be placed in the created directories.
The directories are there. Running the commands gives me the errors, however printing and then copying the commands into the same location works for some reason. I have also tried to give the full path to directory, still same issue.
Hope I've been a bit clearer.
If this is the exact error message you get:
bash: ./path/to/file/newFile1.txt: No such file or directory
Then you'll note that there's an extra space between the colon and the dot, so it's actually trying to open a file called " ./path/to/file/newFile1.txt" (without the quotes).
However, to accomplish that, you'd need to use quotes around the filename in the redirection, as in
something ... 2> " ./path/to/file/newFile1.txt"
Or the first character would have to something else than a regular space. A non-breaking space perhaps, possible something that some editor might create if you hit alt-space or such.
I don't believe you've shown enough to correctly answer the question.
This doesn't look like a problem with ssh, but the way you are calling the (ssh) commands.
You say that you are writing the commands into a file... presumably you are then running that file as a script. Could you show the code you use to do that. I believe that's your problem.
I suspect you have made a false assumption about the way the working directory changes when you run a script. It doesn't. You are listing relative paths, so its important to know what they are relative to. That is the most likely reason for it working when you copy and paste it... You are executing from a different working directory.
I am new to bash scripting and was building my script based on another one I had seen. I was "running" the command by simply calling the variable where the command was stored:
$cmd
Solved by using:
eval $cmd
instead. My bad, should have given the full script from the start.

Ruby: How to open .exe file(that open CMD) and run command init

I am trying using ruby script to a task.
I have an .exe file that i want to run.
when opening this file it open in CMD and i can pass commands to it.
That file located in C:\temp\test.exe
I need to go to the directory and then open the file and then insert to it command like:
"getobject" task = "aa"
this program will give me the result to the CMD.
i will need to copy the result to text but i think i can handle it late.
I tried to search it online cant found anything.
Thanks
If you want to open an executable, usually you can use the `command` syntax in Ruby. So call:
`C:\temp\test.exe`
That should run the executable from the Ruby script. Then you can interact with that executable as if you ran it from a CMD instead of a Ruby file.
In order to run and capture the output of a command you'll need to use a system command. There are many system commands that you can use, but my preference is Open3:
require 'open3'
output, status = Open3.capture2("C:\temp\test.exe")
In the event that you want to pass command line arguments to capture2 you'll want to write that like this: Open3.capture2("C:\temp\test.exe", "arg1", "arg2"). That is the safest way to pass arguments.
I think what you are looking for is input/ output redirection
check redirection
Not tested
system 'C:\temp\test.exe < "\"getobject\" task = \"aa\""'

Logging terminal while running an install script of sorts

I have written an install script in shell that does some configuration of various things such as xserver, network, etc and then installs a few RPM's which is no problem. But I want to be able to log everything that goes to the terminal screen as well. Is this possible to do from within the script so if the end user runs ./Install.sh it will do everything (including the logging).
I have tried using "script" but doesn't work from within the Install.sh script itself.
Thanks
Just use:
script logfilename install.sh
when the install.sh finished the run, the script end too - so, you get everything logged into logfilename.
Probably you can make it two-stage, so, you should rename your current install.sh into install-stage2.sh and your install.sh will contain:
script ./install.log ./install-stage2.sh
UPDATE from my man script
NAME
script -- make typescript of terminal session
SYNOPSIS
script [-akq] [-t time] [file [command ...]]
DESCRIPTION
The script utility makes a typescript of everything printed on your terminal. It is useful for students who need
a hardcopy record of an interactive session as proof of an assignment, as the typescript file can be printed out
later with lpr(1).
If the argument file is given, script saves all dialogue in file. If no file name is given, the typescript is
saved in the file typescript.
If the argument command is given, script will run the specified command with an optional argument vector instead
of an interactive shell.

not able to run .sh file using java

I have a .sh file which i am trying to run using runtime api.
It runs perfectly if .sh file is under main project folder, but I want it to be accessed from outside the project folder.
example: /home/test/test.sh.
When i try to run this I get /home/test/test.sh not found error
Can anyone tell me how to execute .sh file using runtime accessing the file from local system?
is the .sh file chmoded to be executable?
if its not you could either:
chmod +x /home/test/test.sh
or
when you call the script pass it through sh so:
sh /home/test/test.sh
Runtime.exec() is not a command line
One pitfall to cover with Runtime.exec() is mistakenly assuming that exec() accepts any String that your command line (or shell) accepts. Runtime.exec() is much more limited and not cross-platform. This pitfall is caused by users attempting to use the exec() method to accept a single String as a command line would. The confusion may be due to the fact that command is the parameter name for the exec() method. Thus, the programmer incorrectly associates the parameter command with anything that he or she can type on a command line, instead of associating it with a single program and its arguments.

Resources