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

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

Related

How to validate user input on a bash script? [duplicate]

This question already has answers here:
Validate the number of arguments passed in bash from read
(2 answers)
Closed 2 years ago.
I'm creating a bash script. However, I need the script to validate that the user has to enter two arguments. Not 1 and nothing more than 3.
echo -n "Enter two values"
read val1 val2
try this:
#!/bin/bash
if [ $# -eq 2 ]; then
echo "var1: $1 var2: $2"
else
echo "error: need to set two argument"
fi
run:
./scriptName.sh first second

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 to check whether a directory is empty or not in Shell Scripting? [duplicate]

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

Cannot stat mv, file not found [duplicate]

This question already has answers here:
Tilde in path doesn't expand to home directory
(5 answers)
Closed 8 years ago.
could somebody please help me find the problem with this code? getting this error "mv: cannot stat '~Desktop/RecyclingBin/testtest' : No such file or directory. It does exist and it is in the location ~Desktop/RecyclingBin/testtest
fileName=$1
fileLocation='cat ~/Desktop/RecyclingBin/logs/$fileName
if [ -z "$1" ]
then
echo "please enter a valid filename"
else
echo "do you want to restore?"
read ans
if [ "$ans" =="y" ]
then
mv "~/Desktop/RecyclingBin/$fileName" "$fileLocation"
fi
fi
The quotes prevent expansion of ~. Put it outside the quotes:
mv ~/Desktop/RecyclingBin/"$fileName" "$fileLocation"

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