Create a bat file for git-bash script - batch-file

Hi all. I gave a git-bash installed and want some automatisation. I've got a .bat file, which I want to run as
some.bat param | iconv -cp1251 > l.log | tail -f l.log
And I want to run it not in WinCMD but in git-bash shell - tell me plz how to do it?

Git bash on windows uses BASH. You can use bash to create a quick script that can take in parameters and echo them so that you can pipe them to your next utility.
It could look something like this
File: some.sh
#!/bin/bash
#Lots of fun bash scripting here...
echo $1
# $1 here is the first parameter sent to this script.
# $2 is the second... etc. $0 is the script name
then by setting some.sh as executable
$ chmod +x some.sh
You'll then be able to execute it in the git-bash shell
./some.sh param | cat ... etc | etc...
You can read about bash programming
I'd reccomend looking at some bash scripting tutorials such as this one

Related

How to run a command on all .cs files in directory and store file path as a variable to be used as command on windows

I'm trying to run the following command on each file of a directory.
svn blame FILEPATH | gawk '{print $2}' | sort | uniq -c
It works well however it only works on individual files. For whatever reason, it won't run on the directory as a whole. I was hoping to create some form of batch script that would iterate through the directory and would grab the file path and store it as a variable to be used in the command. However, I've never written a batch script nor do I know the first thing about them. I tried this loop but couldn't get it to work
set codedirectory=%C:\Repo\Pineapple% for %codedirectory% %%i in (*.cs) do
but I'm not necessarily sure what to do next. Unfortunately, this all has to be run on windows. Any help would be greatly appreciated. Thanks!
use for and find, similar to example on
https://tldp.org/HOWTO/Bash-Prog-Intro-HOWTO-7.html
for i in $(find . -name "*.cs"); do
svn blame $i | gawk '{print $2}' | sort | uniq -c
done

reading latest from sftp server

I have a requirement to download the latest file from sftp server.I have written the below code in shell script but am not able to download the file.
After retrieving the file am getting the below Error
Invalid command
Please help me how to download the file.
#!/bin/sh
HOST='xx.xx.xx.nxx'
USER='xx'
PASSWD='xx'
sftp $USER#$HOST <<EOF
cd /inbound
file=$(ls -ltr *.xml | tail -1 | awk '{print $NF}')
get $file
EOF
You are trying to run shell commands in sftp, but sftp is not a shell. The command ls happens to exist in sftp, but not $(), tail, or awk. To see this, just type sftp $USER#$HOST to open a sftp session and type help to get all of the available commands.
So what you need to do is to execute the shell commands using ssh to get the filename. So something like this:
file=$(ssh $USER#$HOST "ls -ltr /inbound/*.xml" | tail -1 | awk '{print $NF}')
This execute the command ls -ltr /inbound/*.xml remotely on the server. The output of that is then processed by your shell script locally. Or maybe more efficiently by doing the processing on the server:
file=$(ssh $USER#$HOST "ls -ltr /inbound/*.xml | tail -1 | awk '{print \$NF}'")
Now the shell variable file contains the name of the newest file. Then you can download that file with sftp as
sftp $USER#$HOST:$file .

passing values of text file to array in shell scripting

My script fetches the names of directories in a path and stores in a text file.
#!/bin/bash
MYDIR="/bamboo/artifacts"
DIRS=`ls -d /bamboo/artifacts/* | cut -d'/' -f4 > plan_list.txt`
plan_list.txt:
**************
PLAN1
PLAN2
PLAN3
Now I am trying to pass each of these directory names to a URL to get output like this.
http://bamboo1.test.com:8080/browse/PLAN1
http://bamboo1.test.com:8080/browse/PLAN2
http://bamboo1.test.com:8080/browse/PLAN3
The script to do that doesn't seem to work
bambooServer="http://bamboo1.test.com:8080/browse/"
for DIR in $DIRS
do
echo `$bambooServer+$DIR`
done
Could someone please tell me what I am missing here? Instead of storing the ls command output to a plan_list.txt file i tried passing to array but that didn't work well too.
DIRS=`ls -d /bamboo/artifacts/* | cut -d'/' -f4 > plan_list.txt`
DIRS is just an empty variable since your command is not producing any output and just redirecting output to plan_list.txt.
You can rewrite your script like this:
#!/bin/bash
mydir="/bamboo/artifacts"
cd "$mydir"
bambooServer="http://bamboo1.test.com:8080/browse/"
for dir in */
do
echo "$bambooServer$dir"
done
*/ is the glob pattern to get all the directories in your current path.

How to split lines in a file, and have the output names be based on those lines

I am using CentOS. I have a file that contains information like:
100000,UniqueName1
100000,UniqueName2
100000,UniqueName4
100000,SoloName9
I want to split this out into files, one for each line, each named:
[secondvalue]_file.txt
For an example:
SoloName9_file.txt
Is it possible to split the file in this fashion using a command, or will I need to write a shell script? If the former, what's the command?
Thank you!
Here's one approach. Use the sed command to turn this file into a valid shell script that you can then execute.
sed -e 's/^/echo /g' -e 's/,/ >/g' -e 's/$/_file.txt/g' <your.textfile >your.sh
chmod +x your.sh
./your.sh
Note that trailing whitespace in the file would take some additional work.
Writing it into a shell script file gives you a chance to review it, but you can also execute it as a single line.
sed -e 's/^/echo /g' -e 's/,/ >/g' -e 's/$/_file.txt/g' <your.textfile | sh

Array of IP addresses in sh script

I am having some trouble with a simple shell script on my ubuntu box. Here is the first part of the script:
#!/bin/sh
LCL="192.168.1.1/24"
VPN="10.0.0.0/12"
local_interface="eth0"
virtual_interface="tun0"
servers=(
199.315.117.225
46.31.151.106
46.31.154.82
)
I run the script like this:
sudo sh script.sh
And this is the output:
script.sh: 6: script.sh: Syntax error: "(" unexpected
As far as I can tell, that is a valid array, so I don't understand why that paren is unexpected. I am a newbie so any help is much appreciated.
The problem is that you're running your script using a shell (namely Dash) that doesn't support a feature your script is using (namely Bash-style arrays).
The easiest fix is to change this:
#!/bin/sh
to this:
#!/bin/bash
so that your script is run using Bash instead of Dash.
/bin/bash is superset of /bin/sh... This array syntax works in bash.
https://superuser.com/questions/125728/what-is-the-difference-between-bash-and-sh
http://www.linuxjournal.com/content/bash-arrays
In shell script array should be mentioned by
bash:
declare -a varname
varname=( 192.168.1.1 192.168.1.2 192.168.1.2 )
and you can get all ip by
echo "${varname[#]}"
or
echo "${varname[*]}"
you can try the below code,
#!/bin/sh
LCL="192.168.1.1/24"
VPN="10.0.0.0/12"
local_interface="eth0"
virtual_interface="tun0"
ARRAY="199.315.117.225 46.31.151.106 46.31.154.82"
for i in $ARRAY
do
echo $i
done

Resources