This question already has answers here:
Validate date format in a shell script
(13 answers)
Closed 4 years ago.
I would like to ask if there is a way to validate if the variable passed is in this kind of sample format "06-10" or "10-01". It was really a challenge to me on how to start and echnically the "6-10" stands for "June 10" and "10-01" stands as "October 1", overall it needs to have like of a "Month-Day" valid in number format. I would like to have an if-then statement that would validate if the variables passed are in correct, something like these:
#!/bin/bash
# This script will ONLY accept two parameters in number format
# according to "MM-DD" which is "XX-XX"
# To run this script: ./script.sh xx-xx xx-xx
DATE1=$1
DATE2=$2
if [DATE1 is in correct format] && [DATE2 is in correct format]
then
echo "Correct format"
echo "DATE1 = $DATE1"
echo "DATE2 = $DATE2"
else
echo "Not correct format"
exit 1
fi
The if-statement would look like this:
if [[ $DATE1 =~ ^[0-9]{1,2}-[0-9]{1,2}$ ]] && [[ $DATE2 =~ ^[0-9]{1,2}-[0-9]{1,2}$ ]]
It checks if the date has the string format ##-##.
replacing the regex on the if statement worked by using this:
^(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1])$
Related
This question already has answers here:
How to check if a string contains a substring in Bash
(29 answers)
Closed 6 months ago.
I have a string given below:
string1 = "Hello there, my name is Jack.
Hello there, my name is Jack.
Hello there, my name is Jack."
I'm taking the following input from the string:
read string2
I want to check whether the string2(which is a variable) is present in string1.
I tried running the below command:
output=$(echo $string1 | grep -o "$string2")
echo $output
eg: Let string2="name"
The output is empty when I'm running this command.
Can someone tell me where am I going wrong?
#!/bin/bash
string1="Hello there, my name is Jack"
string2="name"
if [[ $string1 == *"$string2"* ]]; then
echo "$string2 found"
else
echo "$string2 not found"
fi
Alternate method with POSIX-shell grammar:
string1='Hello there, my name is Jack'
string2='name'
case "$string1" in
*"$string2"*) printf '%s found\n' "$string2";;
*) printf '%s not found\n' "$string2";;
esac
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 1 year ago.
Improve this question
I am calling a shell script that has a large number of arguments e.g. ./train-rnn.sh 0 0 0 "63 512". Is it possible to assign each argument to a specific positional parameter ? e.g.
./train-rnn.sh $1=0 $2=0 $4=0 $3="63 512"
Bash has no mechanism for that, but you can cook something up.
The best way would be to parse the command line arguments inside your script. In that case, you might want to improve your user experience by allowing options of the form option=argument instead of having the user (and developer too!) remember the meaning of $1, $2, and so on.
#! /usr/bin/env bash
declare -A opt
for arg; do
if [[ "$arg" =~ ^([^=]+)=(.*) ]]; then
opt["${BASH_REMATCH[1]}"]=${BASH_REMATCH[2]}
else
echo "Error: Arguments must be of the form option=..." >&2
exit 1
fi
done
# "${opt["abc"]}" is the value of option abc=...
# "${opt[#]}" is an unordered (!) list of all values
# "${!opt[#]}" is an unordered (!) list of all options
Example usage:
script.sh abc=... xyz=...
If you really want to stick to the positional parameters, use
#! /usr/bin/env bash
param=()
for arg; do
if [[ "$arg" =~ ^\$([1-9][0-9]*)=(.*) ]]; then
param[BASH_REMATCH[1]]=${BASH_REMATCH[2]}
else
echo "Error: Arguments must be of the form $N=... with N>=1" >&2
exit 1
fi
done
if ! [[ "${#param[#]}" = 0 || " ${!param[*]}" == *" ${#param[#]}" ]]; then
echo "Error: To use $N+1 you have to set $N too" >&2
exit 1
fi
set -- "${param[#]}"
# rest of the script
# "$#" / $1,$2,... are now set accordingly
Example usage:
script.sh $1=... $3=... $2=...
Above approach can also be used as a wrapper in case your script/program cannot be modified. To do so, replace set -- "${param[#]}" by exec program "${param[#]}" and then use wrapper.sh $1=... $3=... $2=....
This question already has answers here:
How can I declare and use Boolean variables in a shell script?
(25 answers)
Closed 5 years ago.
I follow this post
How to declare and use boolean variables in shell script?
and developed a simple shell script
#!/bin/sh
a=false
if [[ $a ]];
then
echo "a is true"
else
echo "a is false"
fi
The output is
a is true
What is wrong?
This doesn't work since [[ only tests if the variable is empty.
You need write:
if [[ $a = true ]];
instead of only
if [[ $a ]];
You need to check if the value equals true, not just whether the variable is set or not. Try the following:
if [[ $a = true ]];
Note that true and false are actually builtin commands in bash, so you can omit the conditional brackets and just do:
if $a;
Update: after reading this excellent answer, I rescind this advice.
The reason if [[ $a ]] doesn't work like you expect is that when then [[ command receives only a single argument (aside from the closing ]]), the return value is success if the argument is non-empty. Clearly the string "false" is not the empty string. See https://www.gnu.org/software/bash/manual/bashref.html#Conditional-Constructs and https://www.gnu.org/software/bash/manual/bashref.html#Bash-Conditional-Expressions
This question already has answers here:
Getting "command not found" error while comparing two strings in Bash
(4 answers)
Closed 6 years ago.
My code below:
echo "====================================="
echo " Test Programme "
echo "====================================="
echo
read -p "Enter Name: " name
if [$name -eq ""]; then
sleep 1
echo "Oh Great! You haven't entered name."
exit
fi
read -p "Enter age: " age
According to that code,I expected "Oh Great! You haven't entered name." to show up when user skips entering the name which WORKS WELL
But, when you enter a proper string for name, it gives this message/ error:
./cool_ham.sh: line 13: [Franco: command not found
I want to know the reason for that.
I have even tried "$name" = "" after #Jack suggested, but still din't work .
Put a space between the square braces of your if condition and its contents.
if [ "$name" = "" ]; then
Additionally note that I use = over -eq to compare strings. -eq is used to compare integer values, while = will compare strings, which can be unintuitive coming from other languages.
I also quoted $name to prevent globbing and word splitting.
This question already has answers here:
I just assigned a variable, but echo $variable shows something else
(7 answers)
Closed 6 years ago.
I want to rewrite a configuration file when asked from a bash script. Here is my code.
function quality {
echo $1 > ~/.livestreamerrc
echo ".livestreamer was modified!"
}
best="stream-types=hls
hls-segment-threads=4
default-stream=best
player=vlc --cache 5000"
read -p "Set quality: " INPUT
if [[ "$INPUT" == "!best" ]]; then
quality $best
fi
This code does the following to .livestreamer file though.
$cat ~/.livestreamerrc
stream-types=hls
Why?
Change it to
quality "$best" # double quotes to avoid word splitting
and then
echo "$1" > ~/.livestreamerrc
Note : Worth checking the [ shellcheck ] documentation.Also, fully uppercase variables like INPUT are reserved for the system.