LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   substring in bash script (https://www.linuxquestions.org/questions/linux-newbie-8/substring-in-bash-script-821370/)

j1alu 07-21-2010 08:35 PM

substring in bash script
 
Code:

#!/usr/bin/env bash

mkdir -p dir_x/fun/crap/
touch dir_x/fun/crap/{argh,uff,ohh}.{sh,txt}

VAR_D="dir_x/fun/crap"

function print_it {
for e in "$VAR_D"/*
do
        for i in "${e##*/}"
        do
            echo "$i"
        done
done
}

print_it


rm -r "$VAR_D"/*

# now the current directory is getting echoed from the:
# for i in "${e##*/}; do echo "$i" ...
print_it   

exit 0

That is not what i want. If the directory is empty i want it to echo nothing.
I think it is clear from the code, and i can't explain it in words.
I am a beginner, bare with me. Thanks for a hint

PS: Do such questions belong here or in the <programming>-section?

lugoteehalt 07-21-2010 08:43 PM

I'd go for the programing section.:)

j1alu 07-21-2010 08:49 PM

Quote:

Originally Posted by lugoteehalt (Post 4041331)
I'd go for the programing section.:)

Thats what i would do too, but i have read so much way more difficult bash-questions here (in newbie's) that i got unsure.

btw: i added a slash
"${e##*/}"/
which seem to work (but, in my eyes, looks more like a dirty workaround).
If someone can confirm that to be a valid solution: i can live with it.

Kenhelm 07-21-2010 10:00 PM

Setting 'nullglob' expands patterns which do not match any files to the null string, instead of using the literal pattern as an argument.
http://www.gnu.org/software/bash/man...t-Builtin.html
Code:

shopt -s nullglob            # Set nullglob
for e in "$VAR_D"/*
do
        for i in "${e##*/}"
        do
            echo "$i"
        done
done
shopt -u nullglob            # Unset nullglob


j1alu 07-21-2010 10:07 PM

Yes, hurray.
Thanks for the link too. I have to read through it.

grail 07-21-2010 10:29 PM

Please mark as SOLVED.

j1alu 07-21-2010 11:23 PM

how?

Well, ok, seems i had to enable java-script to be able to.


All times are GMT -5. The time now is 12:59 AM.