how can I write if condition in bash? [duplicate] - bash

This question already has answers here:
How to check if a file is empty in Bash?
(11 answers)
Closed 4 months ago.
in bash regarding "if" condition. How can I write in a script that will check if a text file is empty continue the script else < perform some command>?

You probably want to this:
if [ -s "$file" ]
then
echo 'file exist and non-empty'
# perform some command then exit?
fi
The [ is a short-hand for the command test. See the man bash in the "SHELL BUILTIN COMMANDS" for details. man test is what I end up using most of the time, though.
If it is important that it is a text file, then preferably check the file has the expected extension (here .txt):
if [ "${file: -4}" = ".txt" ]
then
echo file is a text file (extension)
fi
"${file: -4} means extract the last 4 letters from the variable file, and we then compare that with ".txt".
Failing that you use the file utility to inspect the content of the file:
if [ "`file -b "$file"`" = "ASCII text" ]
then
echo file is a text file (content)
fi

Related

How to read environment variable from the text file? [duplicate]

This question already has answers here:
Forcing bash to expand variables in a string loaded from a file
(13 answers)
Closed 1 year ago.
Hi all, I'm facing an issue that I cant read the environment variable from the text file.
Here is the content of the text file:
Blockquote
#INCLUDE
$ward/ancd/qwe
.........
.........
And the bash script
while IFS= read -r line
do
echo "$line" # It shows $ward/ancd/qwe instead of tchung/folder/main/ancd/qwe
done < "$input"
Blockquote
It should directly shows "tchung/folder/main/ancd/qwe" when echo, but it outputs $ward/ancd/qwe.
The $ward is an environment variable and it able to shows the file path in bash when echo directly. But when comes to reading text file it cant really recognize the environment variable.
Blockquote
The current solution that i can think off is replace the matched $ENVAR in the $line with the value.
repo="\$ward"
if echo "$line" | grep -q "$repo"
then
# echo "match"
line="${line/$repo/$ward}"
#echo "Print the match line : $line"
fi
Is there any other more flexible way that can recognize the environment variables during reading file without replacing the substring one by one?
Perhaps you need to evaluate the content of $line within an echo:
while IFS= read -r line
do
echo $(eval "echo $line")
done
Use envsubst:
envsubst "$input"

cygwin bash: unexpected EOF [duplicate]

This question already has an answer here:
Why is a shell script giving syntax errors when the same code works elsewhere? [duplicate]
(1 answer)
Closed 4 years ago.
What is the problem with the following bash code ?
#!/bin/bash
if [ -z "$1" ]; then
echo "Usage: $0 {file}"
fi
Generates an error of "Unexpected EOF".
Cygwin 2.10.0(0.325/5/3) in Windows 10.
You have DOS line endings in your file, which means the bash parser sees
if [ -z "$1" ]; then\r
echo "Usage: $0 {file}"\r
fi\r
Rather than a complete if statement, it sees the beginning of one, one whose condition consists of (so far) the commands [ -z "$1" ], then\r, echo "Usage: $0 {file}"\r, and fi\r. The parser is still looking for the then keyword to terminate the condition list, but finds the end of the file instead.
Save your script as a POSIX text file using \n as the line endings, not \r\n.

Bash Script : How to pass a wildcard before my filename in "if" exist file? [duplicate]

This question already has answers here:
Check if a file exists with a wildcard in a shell script [duplicate]
(21 answers)
Closed 6 years ago.
I wish to be able to check if file exist.
if [ -f "/var/run/screen/user/*.$InstanceName" ]; then
echo -e "screen instance exist"
fi
but the wilcard / joker don't work
How I can pass it ?
Your wildcard doesn't work because it's quoted. Unquoting it however might break the [ command as it only expects one filename argument, and if two or more files wore globbed it would break.
In bash you can use compgen that will generate a list of files matching the globbing pattern, it will also set proper exit status if no globs are found, it is a hack? I don't know, but it could look like it:
if compgen -G "/var/run/screen/user/*/$InstanceName" > /dev/null; then
printf "screen instance exist\n"
fi

Bourne shell scripting reading file

This simple program supposed to read the file lines, but instead it outputs "cat" every time. What is the problem?
#!/bin/sh
while read line
do
echo $line
done <file
Edit:
file is supposed to be the users input file when calling the program from the terminal. Like:
./programname file
this is suppose to be the users input file when calling the program
from the terminal. Like: ./programname file
In this case you should be doing
#!/bin/sh
if [ -f "$1" ] # checking if file exist
then
while read line
do
echo "$line"
done <"$1" # double quotes important to prevent word splitting
else
echo "Sorry file $1 doesn't exist"
fi
Here $1 represents the first parameter that you pass to the script.
Interesting reads:
What is [ word splitting ] ?
Shell script [ parameters ]

search for files with similar names by if condition in shell script [duplicate]

This question already has answers here:
Check if a file exists with a wildcard in a shell script [duplicate]
(21 answers)
Closed 6 years ago.
I am trying to use the if condition in shell script to check whether some files (starting with similar names) exist or not in the working directory.
My problem is I have more than one file viz. "A1.txt", "A2.txt", "A3.txt" etc in my working directory.
Now my question is, is there a way to check for files with the part of the string of the file name. So if the script finds at least one file with name starting with "A" then it prints that file found of there is no file with file name starting with "A" it does not print so.
My attempt is the following :
if [ -f "A*.txt" ];
then
echo "Files exist."
else
echo "Files do not exist"
fi
But it does not work and returns "too many arguments". Is there a way out?
In BASH you can use:
( shopt -s nullglob; set -- A*.txt; (( $# > 0 )) ) &&
echo "Files exist." || echo "Files don't exist"
When it find a matching file it will exit with 0 and prints:
Files exist.
when it doesn't find any matching file/directory with the given glob pattern it will exit with 1 and print:
Files don't exist
Explanation:
shopt -s nullglob causes shell to fail silently if no matching file is there for the glob
set -- assigns given arguments to the positional parameters $1,$2,$3 etc.
$# is number of positional arguments. It will be set to greater than zero if any matching file is found
((...)) is used for arithmetic evaluation in bash
(..) creates a sub-shell for the above commands so that current shell's env is not affected
this could be one way:
#!/bin/bash
check=`ls A*.txt 2> /dev/null`
if [ ! -z "${check}" ];
then
echo "Files exist."
else
echo "Files do not exist"
fi
then you can inprove the check variable with a regex if you need i.e. to get only the file with number you can do something like check=ls A[1-9]*.txt
Regards
Claudio

Resources