LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 07-21-2010, 09:35 PM   #1
j1alu
Member
 
Registered: Apr 2009
Distribution: debian gnu/linux
Posts: 798

Rep: Reputation: Disabled
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?

Last edited by j1alu; 07-21-2010 at 09:39 PM.
 
Old 07-21-2010, 09:43 PM   #2
lugoteehalt
Senior Member
 
Registered: Sep 2003
Location: UK
Distribution: Debian
Posts: 1,215
Blog Entries: 2

Rep: Reputation: 49
I'd go for the programing section.
 
Old 07-21-2010, 09:49 PM   #3
j1alu
Member
 
Registered: Apr 2009
Distribution: debian gnu/linux
Posts: 798

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by lugoteehalt View Post
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.
 
Old 07-21-2010, 11:00 PM   #4
Kenhelm
Member
 
Registered: Mar 2008
Location: N. W. England
Distribution: Mandriva
Posts: 333

Rep: Reputation: 141Reputation: 141
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
 
1 members found this post helpful.
Old 07-21-2010, 11:07 PM   #5
j1alu
Member
 
Registered: Apr 2009
Distribution: debian gnu/linux
Posts: 798

Original Poster
Rep: Reputation: Disabled
Yes, hurray.
Thanks for the link too. I have to read through it.
 
Old 07-21-2010, 11:29 PM   #6
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,250

Rep: Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684
Please mark as SOLVED.
 
Old 07-22-2010, 12:23 AM   #7
j1alu
Member
 
Registered: Apr 2009
Distribution: debian gnu/linux
Posts: 798

Original Poster
Rep: Reputation: Disabled
how?

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

Last edited by j1alu; 07-22-2010 at 12:26 AM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
bash substring manipulation problem fitteschleiker Programming 4 04-07-2010 01:13 PM
Bash - Substring Extraction and Substitution on same variable jax8 Programming 5 04-26-2009 07:20 AM
using awk substring function on a file in a bash script matt007 Programming 3 06-17-2008 09:17 PM
bash: test for substring inside string ali_bush Linux - General 1 03-08-2007 05:34 PM
get a substring with a bash command xeon123 Linux - General 4 01-26-2007 04:50 AM


All times are GMT -5. The time now is 04:37 AM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration