-2

segment of code from shell script

 fname=$(sed 's/(.*//' <<< $p | awk '{ print $NF }')  
 if [[ $fname == *['!'@#\$%^\&*()_+]* ]]
 then
   flag1=0
 fi

where $p is one line from a text file .

p value is >= (short)BigBlock * DISK_SIZE + Size)

when above shell script is executed for p value , its gives garbage result for example this appear on terminal

>= (short)BigBlock  file1 file2 file3 DISK_SIZE + Size)

where file1 file2 file3 are files in the folder

my guess is * is treated as ls command

what could be the possible solution

  • 2
    Please don't use abstracted values for the content of $p, you can't figure out regex issue without a real literal example. Show a real line from the file. – Lizardx Sep 21 '17 at 20:26
  • @Lizardx The OP is showing the value of $p. – Kusalananda Sep 22 '17 at 17:29
  • How do you know that? It's not at all clear from the example. If the string value is x, then how can $p >= x, that makes exactly no sense at all. I don't believe that's what the value is based on the example, but the person never responded, so I'm giving this a downvote as well. I believe that what the person is actually showing is the contents of a word problem assigned by the teacher, which isn't understood. – Lizardx Sep 22 '17 at 18:28
0

To start with, while awaiting the real $p value string: make sure to quote the $p.

fname=$(sed 's/(.*//' <<< "$p" | awk '{ print $NF }') 

Also note, sed 's/(.*//'is going to match to the end of the string.

f='(something) and more stuff';sed 's/(.*//' <<< "$f"

Results in of course, empty string.

f='(something)-and more stuff';sed 's/([^)]*)//' <<< "$f"

Results in: -and more stuff

  • Bash does not expand filename globbing patterns in here-strings. – Kusalananda Sep 21 '17 at 21:07
0

Summary: You probably have echo $p somewhere in your code. You need to double-quote $p.


No, * is not treated as the ls command. It is, however, treated as a filename globbing pattern due to being used unquoted somewhere (not in code shown in the question)

You need to take care both when assigning and using the value of p and use appropriate quoting to protect the string.

Filename globbing patterns are not expanded in here-strings:

$ cat <<< *
*

So there is nothing wrong with the unquoted use of $p in the call to sed. However, it is almost always better to explicitly quote variable expansions. See also the unquoted use of $fname in your test. That should be double quoted for sure.

You say that the script outputs the text

>= (short)BigBlock  file1 file2 file3 DISK_SIZE + Size)

There is nothing that would provoke this string to be outputted in the script shown in the question. This is probably due to you doing

echo $p

somewhere else in the script.

Again, use double quotes around $p:

echo "$p"

This way, you stop the shell from performing file name globbing on its value (expanding *).

In general, use echo only for static strings, and use printf for variable data:

printf '$p is "%s"\n' "$p"

Related to that last point: Why is printf better than echo?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.