This question already has answers here:
How to test if string exists in file with Bash?
(16 answers)
Closed 3 years ago.
Verify if base machine IP is present in file
Trying to resolve it using for loop with if condition
#!/bin/bash
#file with IP List /tmp/ip_list.txt
curr_ip="$(hostname -I)"
for n in `cat /tmp/ip_list.txt`;
do
if [ "$n" == "$curr_ip" ]; then
echo "success"
else
echo "fail"
fi
done
By default it is running else condition.
#!/bin/bash
curr_ip="$(hostname -I)"
output=$(grep -c /tmp/ip_list.txt)
if [ "$output" != "0" ]
then
echo "success"
else
echo "failure"
fi
The -c option in grep gives you a count. If it doesn't equal zero, it found something.
Related
This question already has answers here:
How to check if string contains characters in regex pattern in shell?
(3 answers)
Why does non-equality check of one variable against many values always return true?
(3 answers)
Closed 1 year ago.
I am trying to create a loop that checks a string if it contains the characters "#" or " ". If it does contain this, it echos out 'yes', otherwise with echos 'no'.
#!/bin/bash
string="#### ## # #### #"
for (( i=0; i<${#string}; i++ )); do
str="${string:$i:1}"
if ["$str"!="#"||"$str"!=" "];
then
echo "No"
break
else
continue
fi
echo "yes"
done
Please try following and let me if it works for you, thanks!!
#!/bin/bash
string="##Actually Mosses##"
echo "$string"
for (( i=0; i<${#string}; i++ ));
do
str="${string:$i:1}"
if [[ "$str" == "#" || "$str" == " " ]]
then
echo -n "Yes "
else
echo -n "No "
fi
done
Expected Output: ##Actually Mosses##
Yes Yes No No No No No No No No Yes No No No No No No Yes Yes
Note: Remove echo "$string" and option -n from script as it is not required in your case.
Option -n for echo is used to print on same .
This question already has answers here:
Test for empty string with X"" [duplicate]
(2 answers)
Closed 2 years ago.
Hello I have a simple example which I am confused about:
#!bin/bash
#test_str is a script to test whether a string is empty or not
x=''
if [ -n $x ]
then
echo "'$x' is not empty"
else
echo "'$x' empty"
fi
chmod u+x test_str
./test_str
The output:
'' is not empty
This occurs also if I've not declared a variable (x here).
If I use the flag -z to test for emptiness it works fine:
if [ -z $x ]
then
echo "'$x' is empty"
else
echo "'$x' not empty"
fi
The output:
'' is empty
So how can I use -n correctly?
Thank you!
This is because [ -n $n ] is expanded to [ -n ] so there is nothing to compare and it will return true.
Instead, you should use [ -n "${n}" ] to prevent such errors and to prevent globbing.
This question already has answers here:
When to wrap quotes around a shell variable?
(5 answers)
Closed 3 years ago.
here is my script:
#!/bin/bash
read -p "para:" teatp
if [ -z $teatp ]; then
echo '-z is ture'
else
echo '-z is false'
fi
if [ -n $teatp ]; then
echo '-n is ture'
else
echo '-n is false'
fi
when I input nothing and press enter, the result is
para:
-z is ture
-n is ture
on the other hand, when I input something and press enter, the result is
para:qwer1234
-z is false
-n is ture
which confused me is the first result -n is ture.
I think the -n and -z are antonyms, but why is the result the same?
There must be something I ignore or misunderstand.
I will be appreciate if someone can point out
the #testp should be quote by double quotation marks?
like:
[ -n "$teatp" ]
This question already has answers here:
How do I parse command line arguments in Bash?
(40 answers)
Closed 4 years ago.
I written the following script which gets name of a file and then assemble and link the file. But it doesn't work. What is the problem with it?
EXPECTED_ARGS=2
if [ $# -ne $EXPECTED_ARGS ]
then
echo "[+] Assembling with Nasm."
nasm -f elf32 $1 -o $1.o
echo "[+] Linking ..."
ld $1.o -o $1
echo "[+] Done!"
else
printf "\nInvalid number of arguments, please check the inputs and try again\n"
fi;
When I run it without passing any args, it doesn't shows following error:
printf "\nInvalid number of arguments, please check the inputs and try again\n"
Ok, try like this
define a variable ARGC=$#
and you if statement will look like
if [ $ARGC -ne $MAX_ARGS ]; then
Legend:
-ne = not equal
-gt = greater than
-eq = equal to
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
Task 1:
If the Time is AM, print "It is Morning". If it is PM, print "It is not morning".
Task 2:
Given 2 arguments passed to the script.
Validate that 2 arguments have been submitted. If three arguments have not been provided, print "Must Supply 2 Arguments" and exit the script
Argument 1: Should be a directory (needs validated). If this does not exist, print "Directory: directory, does not exist" and exit the script
Argument 2: Should be a file (needs validated). If this does not exist, print "File: filename, does not exist" and exit the script.
If all arguments are valid, print "Given valid: filename and directory"
This is what I have so far
echo "James DuBois: 555555 - Task 1"
TIME=$(date "+%H")
if [ $TIME -lt 12 ]; then
echo "morning"
else
echo "not morning"
fi
echo "Task 2"
[ -d "$1" ] || exit
[ -d "$2" ] || exit
[ $# == 2 ] || exit
echo "arg1: $1"
echo "arg2: $2"
James, BASH is a wonderful, flexible shell. It has its warts, but if you need to do anything having to do with Linux system administration, etc.., you can do it in bash. Your tasks are to familiarize you with using conditional expressions (tests). There are tests for just about anything you need. That's why I pointed you to the CONDITIONAL EXPRESSIONS part of man bash.
Your second task requires the input of a filename so you can test it. I presume it is intended to be passed as an argument to your script (called positional parameters). Here is one way to approach the test. Note: I have interchanged output routines echo and printf intentionally for your benefit (printf being a bit more robust). Take a look at the following and let me know what questions you have:
#!/bin/bash
# My first script
#
# echo & printf are used at random below -- intentionally
#
[ -z $1 ] && { # validate at least 1 argument given on command line
printf "error: insufficient input. usage: %s filename\n" "${0##*/}"
exit 1
}
printf "\nJames DuBois: 5555555\n\n Task 1\n\n"
TIME=$(date "+%H")
## test for time of date: morning/not morning
if [ $TIME -lt 12 ]; then
printf " morning - time for coffee\n"
else
echo " not morning - time for scotch"
fi
echo -e "\n Task 2\n"
printf "Testing whether '%s' is a valid file.\n\n" "$1"
## test for file using compound commands
[ -f "$1" ] && echo -e " file found: '$1'\n" || printf " file not found: '%s'\n\n" "$1"
echo -e "Second test whether '$1' is a valid file.\n"
## test for file using if; then; else; fi
if [ -f "$1" ]; then
printf " file found: '%s'\n\n" "$1"
else
echo -e " file not found: '$1'\n"
fi
exit 0
Use/Output
$ bash ~/scr/tmp/stack/morningfile.sh
error: insufficient input. usage: morningfile.sh filename
$ bash ~/scr/tmp/stack/morningfile.sh mtrx_simple_dyn.c
James DuBois: 5555555
Task 1
not morning - time for scotch
Task 2
Testing whether 'mtrx_simple_dyn.c' is a valid file.
file found: 'mtrx_simple_dyn.c'
Second test whether 'mtrx_simple_dyn.c' is a valid file.
file found: 'mtrx_simple_dyn.c'
$ bash ~/scr/tmp/stack/morningfile.sh dog.c
James DuBois: 5555555
Task 1
not morning - time for scotch
Task 2
Testing whether 'dog.c' is a valid file.
file not found: 'dog.c'
Second test whether 'dog.c' is a valid file.
file not found: 'dog.c'
This will have some additional advantages while doing the same thing in task 2:
echo "Task 2"
[[ -d $1 && ! -L $1 && -f $2 && ! -L $2 ]] || exit
printf "%s\n" "arg1: $1" "arg2: $2"
It checks for symbolic links with the -L option.
[[ ]] has some advantages over [ ] like dealing with white spaces without the need for quoting variables.
If you want to print messages while checking for directory and file:
[[ ! -d $1 || -L $1 ]] && echo "Directory doesn't exist" && exit
[[ ! -f $2 || -L $2 ]] && echo "File doesn't exist" && exit
printf "%s\n" "arg1: $1" "arg2: $2"