LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 12-10-2021, 09:11 AM   #1
dilip_d21
LQ Newbie
 
Registered: Nov 2021
Posts: 14

Rep: Reputation: Disabled
check leading and trailing white space from a variable and fail the script


Hi All ,

I was trying this for some time and can one one please help , consider variable called release has some value called 1.0.11 , i want to check that variable if it has any leading or trailing space and in case if it has any space then the script should fail , how can i achieve this ??

I tried the below code but this does not work

release=1.0.11

```if [ release=${release// } ]; then
echo "release version has trailing or leading whitespaces"
exit 1
else
echo "release version value is fine"
fi```

Any help much appreciated
 
Old 12-10-2021, 09:27 AM   #2
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,842

Rep: Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308
if [ $release=${release// } ]; then
 
Old 12-10-2021, 09:46 AM   #3
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,702

Rep: Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895
Almost...
Code:
if [[ $release != ${release// } ]]; then
  echo "release version has trailing or leading whitespaces"
  exit 1
else
  echo "release version value is fine"
fi
 
Old 12-10-2021, 09:48 AM   #4
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,842

Rep: Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308
you are right. Anyway, shellcheck is our friend.
 
Old 12-10-2021, 10:06 AM   #5
dilip_d21
LQ Newbie
 
Registered: Nov 2021
Posts: 14

Original Poster
Rep: Reputation: Disabled
Thanks guys , i checked the above code but unfortunately i got the wrong result, the release version is a input parameter text box which will be feed manually , their is lot of chances people leave spaces in front or after enter the version but the code is not picking up the space but not sure why and i got the result as

as release version value is fine when actually i gave release= 1.0.100 and release=1.0.100 (here a space after 100 )

but in linux release= 1.0.100 is not accepted

can this be solved in anyways ??

thanks

Last edited by dilip_d21; 12-10-2021 at 10:07 AM.
 
Old 12-10-2021, 11:12 AM   #6
shruggy
Senior Member
 
Registered: Mar 2020
Posts: 3,670

Rep: Reputation: Disabled
Quote:
Originally Posted by dilip_d21 View Post
the release version is a input parameter text box which will be feed manually
By default, the read shell builtin will strip leading and trailing white spaces (unless you change $IFS to a custom value).
Code:
read -r release <<<"$1"
Quote:
Originally Posted by dilip_d21 View Post
but in linux release= 1.0.100 is not accepted
Probably a quoting problem. But saving the value with leading/trailing spaces in it is not needed at all, see above.

Last edited by shruggy; 12-10-2021 at 11:17 AM.
 
Old 12-10-2021, 04:11 PM   #7
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,702

Rep: Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895
To assign variables with spaces they must be quoted which as posted should not be needed since your using a read statement


release=" 10.0.100 "

Last edited by michaelk; 12-10-2021 at 10:18 PM.
 
Old 12-13-2021, 03:40 AM   #8
dilip_d21
LQ Newbie
 
Registered: Nov 2021
Posts: 14

Original Poster
Rep: Reputation: Disabled
Hi shruggy, Thanks for the reply

But my requirement is not to trim the space , my requirement is to check for leading and trailing white spaces and if any space is found then the script should fail.

Is this can be handled by any other way ??

Thanks
 
Old 12-13-2021, 03:53 AM   #9
shruggy
Senior Member
 
Registered: Mar 2020
Posts: 3,670

Rep: Reputation: Disabled
Then, as michaelk wrote above, quote the value.
 
Old 12-13-2021, 05:51 AM   #10
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,702

Rep: Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895
What generates the text box? zenity, dialog?
 
Old 12-13-2021, 06:10 AM   #11
dilip_d21
LQ Newbie
 
Registered: Nov 2021
Posts: 14

Original Poster
Rep: Reputation: Disabled
this code worked for me guys..

echo "$(release)"

case "$(release)" in
*[[:space:]]*)
echo "Release version has trailing or leading whitespaces" >&2
exit 1
;;
esac

Thanks all for the support,cheers
 
Old 12-13-2021, 06:16 AM   #12
shruggy
Senior Member
 
Registered: Mar 2020
Posts: 3,670

Rep: Reputation: Disabled
Quote:
Originally Posted by dilip_d21 View Post
echo "$(release)"
You mean "${release}", right?

Quote:
Originally Posted by dilip_d21 View Post
case "$(release)" in
The case variable is an uncommon example of a variable that may be left unquoted
Code:
case $release in
should work the same.

Quote:
Originally Posted by dilip_d21 View Post
*[[:space:]]*)
would of course catch any internal spaces as well.

Last edited by shruggy; 12-13-2021 at 06:31 AM.
 
Old 12-13-2021, 06:24 AM   #13
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,126

Rep: Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120
This is all so arcane - is insisting on shell the right solution ?.
 
Old 12-13-2021, 06:27 AM   #14
dilip_d21
LQ Newbie
 
Registered: Nov 2021
Posts: 14

Original Poster
Rep: Reputation: Disabled
I mean only $(release) , this is a variable from azure which has the value(parameter defined across which can be used across the stage ), thanks
 
Old 12-13-2021, 06:41 AM   #15
shruggy
Senior Member
 
Registered: Mar 2020
Posts: 3,670

Rep: Reputation: Disabled
Microsoft Azure is a very unusual platform for Linux. You should have mentioned it from the start.

Last edited by shruggy; 12-13-2021 at 06:49 AM.
 
  


Reply

Tags
linux, ubuntu



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
LXer: Add leading zeroes that aren't really leading LXer Syndicated Linux News 0 09-13-2019 12:31 PM
LXer: Leading and trailing whitespace LXer Syndicated Linux News 0 06-28-2019 03:04 AM
[SOLVED] if [[ -n "$1" ]]; then FAIL FAIL FAIL rbees Programming 7 03-25-2015 02:39 PM
trim heading and trailing space George2 Programming 1 10-21-2007 03:07 AM
Trailing whitespaces ( space char) and Ncurses terminal refreshes bretzeltux Programming 0 03-02-2004 02:47 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 07:57 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration