All Questions

Filter by
Sorted by
Tagged with
33
votes
6answers
130k views

Checking if an input number is an integer

I'm trying to check if an input is an integer and I've gone over it a hundred times but don't see the error in this. Alas it does not work, it triggers the if statement for all inputs (numbers/letters)...
5
votes
3answers
2k views

Which regular expression methods to validate input could be used in shell scripting?

#!/bin/sh re="\/$" if [ $1 =~ $re ]; then echo "${ATTENTION_PREFIX}$1 DIRECTORY MAY NOT CONTAIN A \"/\" OR LITERAL SLASH!${ATTENTION_POSTFIX}" exit 1 fi Executing sudo file.sh hello/...
5
votes
3answers
8k views

shell test whether multiple lines string contains specified pattern in last line

I want to determine whether a multi-line string ends with a line containing specified pattern. These code failed, it doesn't match. s=`echo hello && echo world && echo OK` [[ "$s" =~ ...
2
votes
1answer
988 views

shell: The =~ operator does not work as expected

Consider the following script: #! /bin/bash line="confusing" if [[ $line =~ [[:lower:]]* ]] then echo "matches!" else echo "does not match." fi While the above produce the expected result, ...
2
votes
2answers
449 views

Pattern matching in a zsh conditional expression

I am having trouble with pattern matching in zsh's hook function precmd. I have the following: precmd(){ local x='test' if [ $x = '*test*' ]; then echo 'hello' fi } which does not ...
2
votes
3answers
730 views

bash: regular expressions in if expression

In my script on my ubuntu machine I declare a string like this: DEBUG_PACKAGE_LIST=$(apt-cache search dbg | awk '{ print $1 }' | grep -e "-dbg") For you to help you understand my problem here is the ...
2
votes
1answer
377 views

Bash: Regex for comparing file path

I want to check if an input string matches whitelisted file paths, then only I want to perform some operation. I am doing something like this path1="mydir/**" path2="mydir/testing" [ [ "$path2" =~ "...
1
vote
4answers
583 views

How to match “4.4” from `uname -r`?

Pseudocode [ `uname -r` =~ ^4\.4.*$ ] && echo "yes" which is, however, unsuccessful in all cases. The uname -r command gives things like 4.6.0-040600-generic as an output.
1
vote
0answers
2k views

Why use a variable in a bash regex match inside [[? [duplicate]

There is not a simple answer to this question at this site yet. This question aims to give a simple and clear answer. It is usually recommended to use a variable ($regex) in this construct: if [[ $...
0
votes
2answers
2k views

RegExp - Optional Capture group in Bash?

Currently working on some RegExp to parse an input file for correct content. I'm using the below RegExp to parse some input: cell-(90|855|80|70)-(DEV|DEVL|SANDP|CAT|(SIT[a-z]|SIT[1-9])|TAT|PROD)(?:-(...
0
votes
2answers
69 views

two variables pattern check at once

Is it possible to check if two variables match regex pattern at once and not one by one? Here is how it works now: # A=1.1 # B=1.2 # [[ $A =~ ^([0-9]+(\.([0-9]+(\.([0-9]+))?))?)$ && $B =~ ^([...