Issues trying to execute bash script on windows using cygwin - cygwin

I'm trying to run a bash script on Windows 7, using cygwin. The script takes two lists of file destinations (files are the same sprinkled in different pairs of folders), iterates through them and detects if the files changed.
#!/bin/bash
src=(
"./src/index.js"
"./src/index_2.js"
)
dest=(
"./client/src/index.js"
"./client/src/index_2.js"
)
arraylength=${#src[#]};
for (( i=0; i<${arraylength}; i++ ));
do
DIFF=$(diff -u ${src[$i]} ${dest[$i]})
if [ $? != 0 ]; then
echo "$DIFF"
echo "Files ${src[$i]} and ${dest[$i]} are not equal!"
exit 1
fi
done
echo "All files are equal"
When I run the command like ./shareddiff.sh, the command executes without errors, but displays nothing (no echo message). Even when I manualy change one of the index.js or index_2.js files - it doesn't detect the change.
Any idea what I could be doing wrong?

You are misusing diff in passing the file arguments; you can compare two files or two directories, not two list of files.
SYNOPSIS
diff [OPTION]... FILES
FILES are 'FILE1 FILE2' or 'DIR1 DIR2' or 'DIR FILE' or 'FILE DIR'. If
--from-file or --to-file is given, there are no restrictions on
FILE(s). If a FILE is '-', read standard input. Exit status is 0 if
inputs are the same, 1 if different, 2 if trouble.
try
diff -uR src client/src

Related

using zgrep to find phone numbers in a directory

I need to create a script that will search for US phone numbers in files that are in a directory that has been passed in as a parameter to your script. The script must recognize phone numbers in the following formats: (570)555-1212, 570.555.1212, 570-555-1212, and +1.570.555.1212. My script should also be able attempt to minimize false positives. The script should work on files that are compressed or uncompressed.
The output should be similar to the following.
letter.docx: (312)555-1212 570.389.3000
intro.txt: 570-389-3000
I have no idea where to start other than
#!/usr/bin/bash
zgrep -e 1 -q '[0-9]{3}-[0-9]{3}-[0-9]{4}','[0-9]{3}.[0-9]{3}.[0-9]{4}','+1.[0-9]{3}.[0-9]{3}.[0-9]{4}'
image.dd
if [ $? -eq 0 ] ; then echo matches ; else echo "no match found" ; fi

In output, echo command printed new line when after printed variable

This is my small bash script code and i want to print the number of files created in the directory :
#! /bin/sh
home_dir=/home/var/foo
Count= ls /$home_dir|wc -l
echo -e "$Count files are existed in the directory $home_dir"
exit 0
My expected output is :
9 files are existed in the directory /home/var/foo
but i got the below output:
9
files are existed in the directory /home/var/foo
can you help what went wrong in my above code? Also please suggest if this is the correct way to print the number of files in the directory
This works for me:
Count=$(ls /$xml_dir|wc -l)
To print on the same line
echo -ne "$Count files are existed in the directory $home_dir"
Add argument n to the echo.

why if expression is always true in bash script

I'm very new in shell script and I wrote this code to copy an input file from directory new1 to directory new2 if the file doesn't exist in second directory.
the problem is that the first if expression is always true and the code always print "file copied successfully" even if the file exists in second directory.
here is my code:
while true; do
echo "enter a file name from directory new1 to copy it to directory new2 "
echo "or enter ctrl+c to exit: "
read input
i=0
cd ~/new2
if [ -f ~/new1/$input ]; then
i=1
fi
if [ $i -eq 0 ];then
cp ~/new1/$input ~/new2/
echo "####" $input "copied successfully ####"
else
echo "#### this file exist ####"
fi
done
I will be appreciated if any one tell me how to fix this problem
You are comparing the wrong file. In addition, you probably want to refactor your logic. There is no need to keep a separate variable to remember what you just did.
while true; do
echo "enter a file name from directory new1 to copy it to directory new2 "
echo "or enter ctrl+c to exit: "
read input
#i=0 # no use
#cd ~/new2 # definitely no use
if [ -f ~/new2/"$input" ]; then # fix s/new1/new2/
# diagnostics to stderr; prefix messages with script's name
echo "$0: file ~/new2/$input already exists" >&2
else
cp ~/new1/"$input" ~/new2/
echo "$0: ~/new1/$input copied to ~/new2 successfully" >&2
fi
done
Take care to make your diagnostic messages specific enough to be useful. Too many beginner scripts tell you "file not found" 23 times but you don't know which of the 50 files you tried to access were not found. Similarly, including the name of the script or tool which produces a diagnostic in the diagnostic message helps identify the culprit and facilitate debugging as you start to build scripts which call scripts which call scripts ...
As you learn to use the command line, you will find that scripts which require interactive input are a dog to use because they don't offer command history, tab completion of file names, and other niceties which are trivially available to any tool which accepts a command-line file argument.
cp -u already does what this script attempts to implement, so the script isn't particularly useful per se.
Note also that ~ is a Bash-only feature which does not work with sh. Your script otherwise seems to be compatible with POSIX sh and could actually benefit from some Bash extensions such as [[ if you are going to use Bash features anyway.

Check if D.userid exists in a directory shell

We assume the current directory has a number of directories named D.userid, each of which contains submitted Java files. How to detect if there is D.userid present in a directory? What should be the code. I dont this mine is rite
#!/bin/bash
if [ -d "$D.*" ]
then
else
echo "no .java file(s) submitted"
exit
fi
done
Since you assume there are files, and only want to be informed that there are none, I think this is reasonable:
ls D.* > /dev/null # try to list the files. we don't need output
return=$? # save the return value of ls just in case
if [ $return -ne 0 ]; # compare it to 0 (success)
then
echo "No files."
fi
ls returns 2 if the files are not found, so if you want you can use that directly. Of course, it doesn't check that they are directories, but it's just another way.

How to remote ls on list of files from bash shell script?

I have a below shell script from which I am trying to copy 5 files in parallel. I am running my below shell script on machineA which tries to copy the file from machineB and machineC.
If the file is not there in machineB, then it should be there in machineC for sure.
I am using GNU Parallel here to download five files in parallel. And everything works fine if all the files are there -
#!/bin/bash
export PRIMARY=/data01/primary
export FILERS_LOCATION_1=machineB
export FILERS_LOCATION_2=machineC
export MEMORY_MAPPED_LOCATION=/bexbat/data/be_t1_snapshot
PRIMARY_PARTITION=(550 274 2 546 278 6 558 282 10 554 286 14) # this will have more file numbers
export dir1=/bexbat/data/re_t1_snapshot/20140501
# just iterating the file and doing ls and exit if any of the file is missing
for el in "${PRIMARY_PARTITION[#]}"
do
ssh david#$FILERS_LOCATION_1 ls $dir3/t1_weekly_1680_"$el"_200003_5.data || ssh david#$FILERS_LOCATION_2 ls $dir3/t1_weekly_1680_"$el"_200003_5.data || echo "File number $el missing on both the filers for primary partition." >&2; exit 1
done
echo "All files present. Starting to copy now."
# copy the files now
Problem Statement:-
Before copying any files, I want to see whether all the files are already present in either of the machines (machineB or machineC) or not. If any of the file is missing, then I need to print out which file is missing and exit out of the shell script with non zero status.
Above script is not working as the way I have described. If it sees any of the file is present, then it exits automatically, it's not moving in the for loop to look for other files. And I am not sure why?
Is there anything wrong I am doing?
ssh doesn't preserve quoting, so you need to escape commands locally to be unescaped by the remote shell.
for el in "${PRIMARY_PARTITION[#]}"
do
printf -v cmd '%q ' test -e "$dir3/t1_weekly_1680_${el}_200003_5.data"
ssh "david#$FILERS_LOCATION_1" "$cmd" \
|| ssh "david#$FILERS_LOCATION_2" "$cmd" \
|| { echo "File number $el missing on both the filers for primary partition." >&2;
exit 1; }
done
That ssh line in the loop doesn't do what you expect. The semi-colon has a lower precedence than the other operators, so when you suffix the line with ; exit 1 that will be executed always. You could just use an if statement:
if ! ssh david#$FILERS_LOCATION_1 ls $dir3/t1_weekly_1680_"$el"_200003_5.data && \
! ssh david#$FILERS_LOCATION_2 ls $dir3/t1_weekly_1680_"$el"_200003_5.data;
then
echo "File number $el missing on both the filers for primary partition." >&2
exit 1
fi

Resources