How to check whether a directory is empty or not in Shell Scripting? [duplicate] - linux

This question already has answers here:
Checking from shell script if a directory contains files
(30 answers)
How do I check if a folder has contents? [duplicate]
(3 answers)
Closed 6 years ago.
I have a directory. It is empty. If i perform ls -lrt , it shows total 0
How do I specify an If condition to perform something, only if the directory is empty.
I mean to ask how to capture that 0 value.

From here. This should help you run your statements within the if else loop. I saved the DIR in the variable
#!/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

Remove the -A option :
$ mkdir /tmp/aaa
$ ls /tmp/aaa
$ a=\`ls /tmp/aaa`
$ [[ -z $a ]]
$ echo $?
0

Related

Shell script which prints error message when package not found [duplicate]

This question already has answers here:
How do I suppress shell script error messages?
(6 answers)
Detect if executable file is on user's PATH [duplicate]
(7 answers)
Closed 1 year ago.
I'm writing a shell script, and I need to check for some dependencies being installed before executing anything. I found I can use which <package> to see if it is installed or not. The problem is that when that dependency is not found, it throws the following error into console's output:
which: no abc in (/home/pace/.emacs.d/bin:/usr/local/bin:/home/pace/.emacs.d/bin:/usr/local/bin:/home/pace/.local/bin:/usr/local/sbin:/usr/local/bin:/usr/bin:...)
I want to avoid having such output, as I already have error messages shown when something fails. How can I avoid which from writing anything?
function is_installed() {
if [[ ! $(which $1) ]]
then
echo "[ERROR]: $1 $2"
exit 1
fi
}
Well, there might be better ways to do what you're trying to do (I'm not certain of the "best" way), but you can redirect stderr and stdout to hide the results from the output:
function is_installed() {
if [[ ! $(which $1 > /dev/null 2>&1 ) ]]
then
echo "[ERROR]: $1 $2"
exit 1
fi
}
(recent versions of bash support using >& /dev/null too to do both at once, but the above is slightly more portable)
EDIT -- try this instead
function is_installed() {
which $1 > /dev/null 2>&1
if [ $? = 1 ] ; then
echo "[ERROR]: $1 $2"
exit 1
fi
}

I am trying to write a shell script to read username from a file, but it is not working. I am posting script I am writing and output [duplicate]

This question already has answers here:
How do I set a variable to the output of a command in Bash?
(15 answers)
Closed 3 years ago.
#!/bin/bash
if [ -f "$1" ]
then
for users in 'cat $1'
do
useradd $users
done
else
echo "input is not a file"
fi
You just have to get the input for the do loop right:
#!/bin/bash
if [ -f "$1" ]
then
for user in $(cat "$1")
do
useradd "$user"
done
else
echo "input is not a file"
fi
Remarks: this works for reading out a file word-by-word and I tried to keep your structure.
For reading out files line by line this is an elegant way: https://stackoverflow.com/a/4642213/2819581

How can I modify script to print information about all files in directory [duplicate]

This question already has answers here:
How to loop over files in directory and change path and add suffix to filename
(6 answers)
Closed 3 years ago.
How can I finish the script?
Linux version 3.10.0-957.21.3.el7.x86_64 .(Red Hat 4.8.5-36)
#!/bin/bash
echo "Enter the file name"
read x
if [ -f $x ]
then
echo "This is a regular file"
else
echo "This is a directory"
fi
Need modify script which will output all files and directory in /etc/ directory and indicate which one is what (e.g.:
dir1 is a directory
fileA is a file
dir2 is a directory
2nd part of the job I did. need help with
Use a for loop instead of getting the filenames from the user.
#!/bin/bash
for file in /etc/*; do
if [ -f "$file" ]
then
echo "$file is a regular file"
elif [ -d "$file" ]
then
echo "$file is a directory"
else
echo "$file is something else"
fi
done
Don't forget to quote variables, in case the value contains a space. And there are other possibilities than just files and directories.

how I could get a echo output if file is empty in linux bash? [duplicate]

This question already has answers here:
How to check if a file is empty in Bash?
(11 answers)
Closed 5 years ago.
I have a simple command for identify when file is empty but not works
MN=$(echo "$(cat empty)") ;
if [ MN == "" ]; then echo "This file is empty"; else echo "This file has been edited. You'll need to do it manually."; fi
whats I doing wrong
Please help me
test -s tests whether a file exists and is nonempty.
if test -e empty && ! test -s empty; then
echo "This file exists but is empty"
fi

Check if a file exists in a linux script [duplicate]

This question already has answers here:
How do I tell if a file does not exist in Bash?
(20 answers)
Closed 4 years ago.
i made a linux script which receives as first argument a path to a directory. I don't know the path. And i want to check if "file.txt" exists at that certain path . For example :
if [ -e $1/file.txt ];then
echo HAHA
fi
if [[ -e "$1/file.txt" ]]; then
echo "It exists"
fi

Resources