Exit not working inside loop and if condition - loops

I have a list file containing:
PATH1=/opt/apps/skum_edw/Source_Bad/
PATH2=/opt/apps/skum_edw/Source_Backup/
PATH3=/hfd
PATH4=/opt/apps/skum_edw/Target_Backup/
Also I have a script to check whether dir is present or not:
cat Path.lis | cut -d'=' -f2 | while read path
do
[ -d $path ]
then
echo $path is present
else
echo $path is not present
exit 1
fi
done
echo That is the end of script
The problem is that the exit 1 is not working. And I am getting last line also as output. How to do it? And what is the reason of this?

There's no IF, while there is an FI.

Related

How do you assign the output of a multiple piped command to a variable in windows CMD batch?

I need to loop through a set of files in a directory, matching a wildcard pattern and extract a portion of of the file name in between two other patterns. Unix (i.e. bash) equivalent command would be something like this:
ls -1 "$wildcard1"*"$wildcard2" | while read filename; do
string2find=`echo $filename | cut -d $delim1 -f2 | cut -d $delim2 -f1`
echo $string2find
#do something with string2find variable here
done
In batch file, I tried this
for %%f in (WILD1*WILD2) do (
echo %%f | cut -d_ -f2 | cut -d"." -f1> temp.out <--here, need something between an "_" and a "."
type temp.out <-- see the right string in the file
set var= <clear the variable
set /p var=<temp.out
echo %var% <-- ECHO is off message I am getting here
del temp.out
REM need to do something with var here
)
All the examples I was able to find is devoid of piped commands.

How to make this script work but search for empty files in any directory instead of current directory?

#!/bin/bash
n=0
for f in *; do
[[ -f "$f" ]] && { echo "$f"; ((n++)); }
done
echo :Number of empty files: $n"
currently it checks the current directory for empty files, I would like it to search for empty files in any directory. Any ideas?
Recursively searches for empty files in current directory and below:
find . -empty -type f
Recursively lists empty files in specified directory and below and reports total
findempty
#!/bin/bash
echo :Number of empty files: `find $1 -empty -type f | tee /dev/tty | wc -l`
Example Usage
findempty /tmp
Example Output
/tmp/source/fb/b
/tmp/source/fb/a
/tmp/source/fb/c
/tmp/source/fa/b
/tmp/source/fa/a
/tmp/source/fa/c
/tmp/source/fc/b
/tmp/source/fc/a
/tmp/source/fc/c
/tmp/dest/source/fb/b
/tmp/dest/source/fa/b
/tmp/dest/source/fc/b
:Number of empty files: 12

create and loop through arrays of folders in shell

Please feel free to rename the question to something more appropriate.
How would I mimic the below zsh using bash instead?
mkdir folder1
mkdir folder2
mkdir folder3
# zsh
folders=(folder*) | print $folders
#folder1 folder2 folder3
# bash
folders=(folder*/) | echo $folders
#folder1
As you can see, this only outputs the first element.
Any pointers would be appreciated, thanks.
Try changing it to:
folders=(folder*); echo "${folders[#]}"
folders[#] gives all the elements in the array
${} expands the output from above command to bash.
If lets say, you have multiple .txt file in some Directory and you want to get/display those folders . you can try something like this:
declare -a folder_arr
i=0
for dir in *.txt; do
folder_arr[i]=$dir
i=$((i+1))
done
for j in $(seq 0 $((i-1)))
do
echo ${folder_arr[$j]}
done
I excuted the above file and was able to get the expected reult.
/temps$ ./dirrr.sh
z.txt

Using wgets to connect to sites in a txt file using shell scripting

I am trying to create a script that uses wgets to access every url from a list in a file. However when doing this instead of accessing website.com it will try to connect to website.com/r/n. I know this has to do with text formatting in the text editor but I'm unsure of how to get my program to ignore this. This is the code I have:
#!/bin/bash
for i in `cat $1`
do
wget --spider $i
if wget --spider $i 2>&1 | grep --quiet "200 OK" ; then
echo $i >> connected.txt
else
echo $i >> unsuccesful.txt
fi
rm wget-log
done
Add this to the top of your script
dos2unix "$1"
IHTH

How do I sort a directory full of pictures into sub-directories based on the image name?

I want to take all of the files in /media/mdrive/dump/:
1COD-234355.jpg
MAK-LXT218.jpg
ZIR-CON145.jpg
And create and sort them into the following directories:
/media/mdrive/dump/1/1COD-234355.jpg
/media/mdrive/dump/M/MAK-LXT218.jpg
/media/mdrive/dump/Z/ZIR-CON145.jpg
How would I do that?
This script takes a directory as the first argument and performs what you need:
#!/bin/bash
DIR="$1"
if [ -z "$DIR" ]; then
echo >&2 "Syntax: $0 <directory>"
exit 1
fi
if [ ! -d "$DIR" ]; then
echo >&2 "\"$DIR\" is not a directory"
exit 1
fi
cd "$DIR"
for file in *.jpg *.JPG; do
first=${file::1}
mkdir -p $first && mv $file $first/;
done
head -c xx will return the first xx characters of its input (here, the filename). mkdir -p will skip directory creation if it already exists.
to make two directories you could try something like
dir "/media/mdrive/dump/1/" :: CD would also work here
mkdir folder 1
mkdir folder 2
from here I think you can continue with your IF statements and so forth.
all you need to do is set the dir commands with the Direct path takes the guess work out.
then to check each just do:
start explorer.exe "the folder's path here"
it should open the folder to view the files

Resources