execute cygwin sort command from batch file - cygwin

I am trying to run the sort command from a batch file to take the lines from new.txt and output them into unique.txt
C:\cygwin64\bin\bash -c "sort -u new.txt > unique.txt"
This is not working
However, if I place new.txt into the home/Administrator directory and run the command in the terminal it works just fine.

wrote a script:
#!/bin/bash
sort -u /home/Administrator/new.txt > unique.txt
batch file:
set PATH=C:\cygwin64\bin;%PATH%
c:\cygwin64\bin\bash.exe /usr/bin/u.sh
all good :)

Or you can do it directly from cmd without using bash:
C:\cygwin64\bin\sort -u new.txt > unique.txt

Related

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'

How to run matlab code in linux as script file?

I am looking into running matlab script in Linux similar to bash/python scripts. I.e., a matlab script that can be run as an application.
You can get a similar effect without your custom mash script by adding the following header to the files you want to be executable:
#/usr/bin/bash
/path/to/matlab -r "$(sed -n -e '4,$p' < "$0")"
exit $?
If you want matlab to terminate after executing the script, as in your example, you could replace the second line with
sed -n -e '4,$p' < "$0" | /path/to/matlab
The idea here is to execute a bash command that simply clips off the header of the script, and passes the rest along to matlab.
Here is the implementation I came up with -
Create /usr/bin/mash script file containing the following lines -
#!/bin/bash
grep -ve '^(#!\|^\s*$)' ${#: -1} | ${#: 1:$#-1}
exit $?
Make mash script executable -
$ chmod +x /usr/bin/mash
Write matlab script file called test.msh
#!/usr/bin/mash /usr/local/MATLAB/R2012a/bin/matlab -nodisplay
format long
a = 2*pi % matlab commands ...
Make test.msh script executable -
$ chmod +x mash
Run test.msh
$ ./test.msh
...
>> >> a =
6.283185307179586

Wget file and send it to Bash

I want to make a Bash script which has to use Wget and run its output with Bash like this:
wget -q -O - http://pastebin.com/raw.php?i=VURksJnn | bash
The pastebin file is a test script, but this commands shows me:
"Unknown command" (maybe due to new lines) and "Unexpected end of file", and I don't know why.
Am I missing something?
Your script has DOS line-endings.
If you convert the line endings to Unix line endings, it runs fine:
$ tr -d '\r' < raw.php\?i\=VURksJnn > script
$ cat script | bash
Test script
You're not root
End test
$
To start with, you save the downloaded wget'ed file locally, and run as bash filename. Because the following works for me:
cat - | bash

Run text file as commands in Bash

If I have a text file with a separate command on each line how would I make terminal run each line as a command? I just don't want to have to copy and paste 1 line at a time. It doesn't HAVE to be a text file... It can be any kind of file that will work.
example.txt:
sudo command 1
sudo command 2
sudo command 3
you can make a shell script with those commands, and then chmod +x <scriptname.sh>, and then just run it by
./scriptname.sh
Its very simple to write a bash script
Mockup sh file:
#!/bin/sh
sudo command1
sudo command2
.
.
.
sudo commandn
you can also just run it with a shell, for example:
bash example.txt
sh example.txt
Execute
. example.txt
That does exactly what you ask for, without setting an executable flag on the file or running an extra bash instance.
For a detailed explanation see e.g. https://unix.stackexchange.com/questions/43882/what-is-the-difference-between-sourcing-or-source-and-executing-a-file-i
You can use something like this:
for i in `cat foo.txt`
do
sudo $i
done
Though if the commands have arguments (i.e. there is whitespace in the lines) you may have to monkey around with that a bit to protect the whitepace so that the whole string is seen by sudo as a command. But it gives you an idea on how to start.
cat /path/* | bash
OR
cat commands.txt | bash

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