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.
Related
I was wondering what does the ls -A option do on it's own and why does it work in the code below when checking if a directory is empty or not... ?
I can't find any other answers online and the man pages I don't understand what it means when I read the documentation.
Thanks
#!/bin/bash
FILE=""
DIR="/empty_dir"
# init
# look for empty dir
if [ "$(ls -A $DIR)" ]; then
echo "Take action $DIR is not Empty"
else
echo "$DIR is Empty"
fi
# rest of the logic
Nothing related to bash. It tells ls to show all "hidden" files (those whose names start with .) except for . and .. (special directory entries representing the current directory and the parent directory, respectively).
Compare with ls -a, which shows all hidden files including . and ... The presence of those two entries in the output would make the directory appear to be not empty, even though it really is.
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
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
I have people uploading files to a directory on my Ubuntu Server.
I need to move those files to the final location (another directory) only when I know these files are fully uploaded.
Here's my script so far:
#!/bin/bash
cd /var/uploaded_by_users
for filename in *; do
lsof $filename
if [ -z $? ]; then
# file has been closed, move it
else
echo "*** File is open. Skipping..."
fi
done
cd -
However it's not working as it says some files are open when that's not true. I supposed $? would have 0 if the file was closed and 1 if it wasn't but I think that's wrong.
I'm not linux expert so I'm looking to know how to implement this simple script that will run on a cron job every 1 minute.
[ -z $? ] checks if $? is of zero length or not. Since $? will never be a null string, your check will always fail and result in else part being executed.
You need to test for numeric zero, as below:
lsof "$filename" >/dev/null; lsof_status=$?
if [ "$lsof_status" -eq 0 ]; then
# file is open, skipping
else
# move it
fi
Or more simply (as Benjamin pointed out):
if lsof "$filename" >/dev/null; then
# file is open, skip
else
# move it
fi
Using negation, we can shorten the if statement (as dimo414 pointed out):
if ! lsof "$filename" >/dev/null; then
# move it
fi
You can shorten it even further, using &&:
for filename in *; do
lsof "$filename" >/dev/null && continue # skip if the file is open
# move the file
done
You may not need to worry about when the write is complete, if you are moving the file to a different location in the same file system. As long as the client is using the same file descriptor to write to the file, you can simply create a new hard link for the upload file, then remove the original link. The client's file descriptor won't be affected by one of the links being removed.
cd /var/uploaded_by_users
for f in *; do
ln "$f" /somewhere/else/"$f"
rm "$f"
done
I am trying to write a script to move some file in a common folder.
Basically I have n folders and in each of them there is a file called xmu.dat; I want to copy these files in a different folder changing its names.
This is the code I came up with (I have never written a script before...), but I get some errors:
echo "Folders found:"
for folder in */
do
echo "$folder"
name = ${folder//[\/]/}
cp ./"$folder"/xmu.dat ./OutputFiles/name
done
As fedorqui said, the issue with your code is the presence of whitespaces around the '='.
If you want to check if a file exists, you can use the '-f' option, as:
if [ -f "$file" ]
then
echo "$file found."
else
echo "$file not found."
fi