Check if a string contains Asterisk (*) - linux

I want to check if my string contain one or more asterisk.
I have tried this :
if [[ $date_alarm =~ .*\*.* ]]
then
...
fi
It worked when I launch directly the script, but not if this script is called during shutdown (script installed in run level 0 and 6 via update-rc.d)
Any idea, suggestion ?
Thanks

Always quote strings.
To check if the string $date_alarm contains an asterisk, you can do:
if echo x"$date_alarm" | grep '*' > /dev/null; then
...
fi

what happens if you replace
if [[ $date_alarm =~ .*\*.* ]]
with
if [[ "$date_alarm" =~ .*\*.* ]]
you might also want to try:
if [[ "$date_alarm" =~ '\*+' ]]
not sure about that one...
regards

case "$date_alarm" in
*\**)
...
break
;;
*)
# else part
...
;;
esac
The syntax is, well, /bin/sh, but it works.

expr "$date_alarm" : ".*\*.*"

if echo $date_alarm|perl -e '$_=<>;exit(!/\*/)'
then
...
fi

Finally
if echo x"$date_alarm" | grep '*' > /dev/null; then
did the trick
Strange thing =~ .*. doesn't work only in init context during shutdown, but work perfectly if launch in bash context....

No need to redirect stdout like others do. Use the -q option of grep instead:
if echo x"$date_alarm" | grep -q '*' ; then

Related

if file name(just name) comparision shell script

another newbie in Linux shell scripting.
Basically I've a folder with many files in it. But I need to get only the files that ends with ".log"
Below is my version which doesn't work
#!/bin/sh
for i in *;
do
if [ "$i" == "$i".log ]; then
echo $i;
fi
done
Could someone please help me on this ? Thanks a lot !
Any reason you can't you can't just do it like this?
for fname in *.log
do
echo $fname
done
#John3136 has the simplest answer. With bash, you would use the fact that == inside [[ ... ]] is actually a pattern matching operator, not an equality operator:
#!/bin/bash
for f in *; do
if [[ "$f" == *.log ]]; then
echo "$f"
fi
done
See http://www.gnu.org/software/bash/manual/bashref.html#Conditional-Constructs

Bash how check if an argument is a sed patterns?

I'm working on a bash script that should receive many arguments. those arguments could be simple like:
Myscript [-r] [-l|-u] <dir/file names...>
or it could be just a sed patterns given instead of a simple arguments like:
Myscript <sed pattern> <dir/file names...>
So my question is how to check if the arguments given is a sed patterns and not directory path or filename?
I have done something but it doesn't work for a long path (dir1/dir2/dir)
while [[ $# > 0 ]]
do
key="$1"
case $key in
-ru|-ur)
config_recusive
config_upper
shift
;;
-rl|-lr)
config_recusive
config_lower
shift
;;
-r)
config_recusive
shift
;;
-l)
config_lower
shift
;;
-u)
config_upper
shift
;;
*)
check="^.*/.*/.*$"
if [[ $key =~ $check ]] ; then
config_sed_pattern
else
TARGET=$key
fi
shift
;;
esac
done
To be more clear, here is an example of my problem when I'm trying to run the script like that:
./myscript -u tmp/users/regions
its being confused taking the path (tmp/users/regions) as a sed patterns.
hope that I was clear enough.
waiting for your help :)
thanks
i think you could use try run that script on something
echo a | sed "$pattern" >/dev/null 2>/dev/null
and then check the $?
if [ $? != 0 ]; then # means sed failed
#...
but maybe sed will think it will be its argument too.
btw, handle arguments using getopt will make things easier.
i don't think the way you design argument format is good. maybe you think making arguments in different forms could make your program looks dynamic and awesome. but now you are trying to solve a problem have nothing to do with your serious business and wasting your own time and ours. that's not good. it's better to make things stupid and clear.
in your case, maybe you can add another argument to show it's a sed expression that follows.
YourScript -e <sed expr> <others>
and in such a way, you will have idea about what you have done two weeks later.

Scripts works fine in CentOS but not on RHEL5

Below script works fine on CentOS but not on RHEL5:
#!/bin/bash
read -p "enter your value:" ip
if [[ $ip =~ ^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$ ]]
then
echo "valid"
else
echo "invalid"
fi
Run the script and just pass 192.16666 as input, it will say valid. But if you do the same in CentOS, it will say invalid.
Please let me know what is getting wrong.
Thanks
=~ matching was introduced in Bash 3.0-alpha. The =~ semantics changed in 3.2, but it seems that was only to force text matching for right-hand quoted strings, which is irrelevant for this case. Is the version older than that?
Solved it myself by adding single quotes to the regex, as:
#!/bin/bash
read -p "enter your value:" ip
if [[ $ip =~ '^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$' ]]
then
echo "valid"
else
echo "invalid"
fi
Thanks bdw :)

Does not work to execute command in double brackets in bash

In an attempt to stay consistent i have tried to use double brackets [[ ]] in all my if statements. I did however get into a problem when i was going to check the return value for a command i wanted to run. After testing several ways of creating an if statement i found that only without brackets could i execute a command.
The following does not work:
if [[ $command ]] ; then
echo "something"
fi
if [[ $(command) ]] ; then
echo "something"
fi
if [[ ${command} ]] ; then
echo "something"
fi
and the code above makes the if loop true even when the command was not run.
since the code above doesnt work with braces it doesnt work to use this either:
[[ $command ]] || echo "failed"
and it doesnt work in a subshell either.
The following works:
if $command ; then
echo "something"
fi
if $(command) ; then
echo "something"
fi
Why doesnt it work to place a command in an if loop with brackets, and why does the if loops above report true when it didnt even run the command ? I'm using bash version 4.1.9. Ive tried this many times and the if loops are just as simple as the ones i typed above, it just checks if a command was run successfully and exits if it wasnt.
The short answer is:
[ and [[ expect an expression.
if expects a command.
Saying:
[[ $(command) ]]
would essentially execute:
[[ -n <command_output> ]]
which may or may not be what you want. On the other hand, saying:
$command && echo something || echo other
would echo something or other based on the return code of the command (0 and non-zero respectively).
Double braces are a shortcut for test. In your examples, what's happening is that you're testing the shell variable $command for existence.
if [[ $PWD ]]; then
echo PWD is set to a value
fi
if [[ $NOT_A_REAL_VAR ]]; then
echo Nope, its not set
fi
In your second example, you're using command substitution to check that command output something on standard output.
if [[ $(echo hi) ]]; then
echo "echo said hi'
fi
if [[ $(true) ]]; then #true is a program that just quits with successful exit status
echo "This shouldn't execute"
fi
Your third example is the same as your first, pretty much. You use the curly braces if you want to group your variables. for example if you want to put an 's' after something.
WORD=Bike
echo "$WORDS" #won't work because "WORDS" isn't a variable
echo "${WORD}S" # will output "BikeS"
Then in your fifth example, you are running the program that is sitting inside command.
So, if you want to test some strings, use [[ ]] or [ ]. If you just want to test the exit status of a program, then don't use those, just use a bare if.
Check man test for details on the braces.
If you're just checking the return value of the command, drop the double brackets.
if $command
then
echo "Command succeeded"
else
echo "Command failed: $!"
fi
The double brackets are a test command. (Well, not really, but their a takeoff of the single square brackets that were an alias to the test command.) In early Bourne shell, you would see things like:
if test -z "$string"
then
echo "This is an empty string"
fi
The square brackets were syntactic sugar:
if [ -z "$string" ]
then
echo "This is an empty string"
fi
So, if you're not doing an actual test, you can eliminate the double or single square brackets.
If you're using square brackets, you should use the double ones and not the single ones because the double ones are a bit more forgiving and can do a bit more:
if [ -z $string ] # No quotes: This will actually fail if string is zero bytes!
if [[ -z $string ]] # This will work despite the lack of quotes

Bash If Statement - Giving up hope

I have tried and tried to solve this and with my limited knowledge of BASH i cannot, I have searched but cannot find anything relating to my issue.
COMMAND_WAIT=$(curl --data "SERIAL_NUMBER="$SERIALNUMBER"" h**p://SERVER/device_check_in.php)
echo $COMMAND
if [ "$COMMAND_WAIT" == "REBOOT" ]; then
echo "Reboot Scheduled"
else
echo "Nothing Found"
fi
I have included an echo command of "COMMAND_WAIT" and this displays "REBOOT" as expected but the if statement will just not work?
Try echoing
echo ">>$COMMAND_WAIT<<"
and see if you have any padding. That might be the culprit.
I prefer to use [[]] instead of [] since [ is a builtin (a command) while [[ is a keyword (see bash manual for more details).
if [[ "$VAR" == "VALUE" ]]
then
echo "true";
else
echo "false";
fi
If it's trailling whitespaces, there's many way to skin that cat like suggested here

Resources