LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   A bash script that I'm trying to understand (https://www.linuxquestions.org/questions/linux-newbie-8/a-bash-script-that-im-trying-to-understand-4175605593/)

orri23 05-09-2017 06:30 PM

A bash script that I'm trying to understand
 
Okay

chrism01 05-09-2017 06:39 PM

He's trying to ensure var $fn actually has a non-empty value.
I recommend running it with debugging options thus
Code:

set -xv    # this makes the parser show you exactly what its doing as it goes

read fn
while [ "a$fn" != "a" ]
do
basename "$fn"
read fn
done


hydrurga 05-09-2017 06:47 PM

Relevant: https://serverfault.com/questions/75...iable-is-empty

mike acker 05-09-2017 07:34 PM

why not use the -z test?

Example:


Code:

#!/bin/sh
#

TEST_STRING="This has been a test"
echo "Test 1 String value is : " $TEST_STRING

if [ -z "$TEST_STRING" ]
then
      echo "Test String is empty."
else
      echo $TEST_STRING
fi

TEST_STRING=""
echo "Test 2 : Test String is empty "

if [ -z "$TEST_STRING" ]
then
      echo "Test String is empty."
else
      echo $TEST_STRING
fi

exit 0


Result:

~/Documents/Scripting $ ./test_string.sh
Test 1 String value is : This has been a test
This has been a test
Test 2 : Test String is empty
Test String is empty.
~/Documents/Scripting $

i just acquired a copy of Shell Scripting (Jason Cannon) (Amazon) which to me is very helpful. Lots of examples!

rtmistler 05-10-2017 06:47 AM

@orri23, welcome to LQ.

Please do not edit your posts and remove content, especially the question or problem description. This thread is now ineffective because it makes no sense. Consider revising the original question to allow the thread to possibly be helpful to future solution seekers. Please do not repeat this behavior in the future, it doesn't help the site, it doesn't help all to increase their Linux knowledge, which is mainly why we are all here.

Best Regards,

- RT

BW-userx 05-10-2017 08:52 AM

Why not use -n test?

(code example taken from @mike acker, then fixed and modified to match test prams)
Code:

#!/bin/sh
#

TEST_STRING="This has been a test"
echo "Test 1 String value is : " $TEST_STRING

if [ -n "$TEST_STRING" ] ;
then
    echo "$TEST_STRING"
else
      echo "Test String is empty."
fi

TEST_STRING=""
echo "Test 2 : Test String is empty "

if [ -n "$TEST_STRING" ] ;
then
      echo "$TEST_STRING"
else
      echo "Test String is empty."
fi

exit 0

example
Code:

userx%slackwhere ⚡ ~ ⚡> testS="hite"
userx%slackwhere ⚡ ~ ⚡> [[ -n $testS ]] && echo "got string"
got string

7.3. Other Comparison Operators


All times are GMT -5. The time now is 08:55 AM.