MATLAB Bash script - bash

Let's say I have this MATLAB function
function a = testfun(filename)
% do something with file contents and create some variable a
disp(a);
I would like to have a shell script that runs on Cygwin as follows:
./testscript.sh < inputforfunction.txt > outputoffunction.txt
The input file would contain the data I need.
After running this command, the output file will contain the result of running testfun(filename).
Up till now, I can write the output to the file outputoffunction.txt.
The problem is I want to read the file name "inputforfunction.txt".
I am able to read in the file contents but not the file name, any hints please?
Thanks!

Why not pass the file as an argument to the bash script?
./testscript.sh inputforfunction.txt > outputoffunction.txt
In the script you can access $1 - it will evaluate to 'inputforfunction.txt'.

read file_name
./testscript.sh < $file_name > outputoffunction.txt

Related

How to create and write a properties file using shell script

I have a shell file and in that I need to create and write a content as below into my abc.properties file.
version=123456-> This is the one thing which is required in my properties file.
I tried with the below commands and its not writing the contents into the file.
sh "touch abc.properties"
sh "lscm show lastmod . -f yyyyMMddHHmmssSSS >> ${lscm_home}/abc.properties"
Can someone provide some inputs on how can i make the second command to write into the required file ? Thanks
You can try like below
echo "Version=`lscm show lastmod . -f yyyyMMddHHmmssSSS`" >> FILE_NAME

How to write list of executing program in a text file by bash

I trying write a program with bash file that create a text file then write the List of running programs file in the text file. I created a text file with bash that name is test.txt, how can I write List of running programs in the text file?
I put my code here:
#!/bin/bash
cat test.txt
To get a list of running programs, use ps. Run man ps for details.
To redirect the output of a command to a file, use >.
ps > test.txt
See REDIRECTION in man bash for details.

What does the redirection operator "<" do in shell scripts?

I came across the below shell command:
$prog.sh < file_name.json
I know it reads from a file, but how and where does prog.sh load the file?
Every program has three open file handles at startup, one of which is standard input. Normally, the file handles are inherited from the parent process. The < operator tells the shell that, instead of passing its standard input to prog.sh, to open file_name.json instead and give that file handle to prog.sh as its standard input.
$prog.sh < file_name.json
As you rightly guess. The < is meant for redirecting the input from a file so that your script will read from the file which will be the (temporary) stdin(fd0).
it read from a file, but how and where prog.sh will load the file
It depends on how you plan to go about it. Any command in the script that expects an input from the stdin will now read from the file. The new line character in the text file (usually) stands for the ↵ in the stdin.

Automating user inputs to an INTERACTIVE Ruby console

There is a Ruby file called ABC (for eg). Now this file is called from a Batch file called Batch.bat.
So I would run the Batch file like this:
Batch.bat ip username password.
Now the process goes to Ruby console and provides output as a file system:
0 /
1 10.160.165.86/
Now User must provide inputs as:
10.160.165.86/Computers/Data
so it move to that location and run one more command :
[Eg: O/P]: 10.160.165.86/Computers/Data > some_command
Is there a way to automate the user input process using Batch script or using Process Class in Java?
as another batch script :
echo my_url | Batch.bat
This will pipe the result of echo into the input of your batch file.
Replace my_url by the input you want.
If what you want to input depends on what is outputted by the script, you can also capture its output using a similar redirection, process it, and echo the processed value to it.

Shell Script - Removing portions of strings. Can't find Files

I have a pretty simple shell script. It reads something like this.
#!/bin/bash
echo upload trigram
bteq < load_temp_trigram
echo load bigram
bteq < load_temp_bigram
echo load word
bteq < load_temp_word
echo load phrase
bteq < load_temp_phrase
I get the following errors, and then it the script executes the following command
: command not found
upload trigram
: No such file or directoryigram
: command not found
load bigram
: No such file or directorygram
: command not found
load word
: No such file or directoryord
: command not found
load phrase
I'm calling the script with bash script.sh or sh script.sh.
So, it looks like it isn't recognizing the echo command, even though it seems to work. And it is cutting off pieces of the strings/files - which is probably why it can't find them. I'm at a loss as to what's going on here. Any help would be appreciated.
1)add an -x to shell like this
#!/bin/bash -x
it will reduce amount of questions in general.
2) in case you want the text output use quotes
like this
echo "upload trigram"
3) use absolute paths to the executable
like this
/usr/bin/bteq
the path to your bteq executable could be found by executing
which bteq
(if you are lucky and have a which installed)
same goes for sql batch files
/path/to/td_bins/bteq < /path/to/batch/load_temp_word
if you in the folder with batch files, relative path to it will be ./

Resources