Bash script - $(ls expr) return double file name with one finishing with '.'


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Bash script - $(ls expr) return double file name with one finishing with '.'
# 1  
Old 10-20-2012
Bash script - $(ls expr) return double file name with one finishing with '.'

Hello.

This small part of my script

Code:
#!/bin/bash
SRCDIR=/root/.kde4/share/config
for MY_FILE in  $(ls $SRCDIR/kate*) ; do
    echo "$MY_FILE"
done

give :
Quote:
/root/.kde4/share/config/katepartpluginsrc
/root/.kde4/share/config/katepartpluginsrc.
/root/.kde4/share/config/katepartscriptrc
/root/.kde4/share/config/katepartscriptrc.
/root/.kde4/share/config/katerc
/root/.kde4/share/config/katerc.
/root/.kde4/share/config/kateschemarc
/root/.kde4/share/config/kateschemarc.
/root/.kde4/share/config/katesyntaxhighlightingrc
/root/.kde4/share/config/katesyntaxhighlightingrc.
So for the moment I have modified my script like this :
Code:
#!/bin/bash
SRCDIR=/root/.kde4/share/config
for MY_FILE in  $(ls $SRCDIR/kate*) ; do
    if [[ "$MY_FILE" == $SRCDIR/kate* ]] ; then
        if [[ "$MY_FILE" == $SRCDIR/kate*. ]] ; then
            echo .
        else
            echo "Keep this : $MY_FILE"
        fi
    fi
done

which give
Quote:
Keep this : /root/.kde4/share/config/katepartpluginsrc
.
Keep this : /root/.kde4/share/config/katepartscriptrc
.
Keep this : /root/.kde4/share/config/katerc
.
Keep this : /root/.kde4/share/config/kateschemarc
.
Keep this : /root/.kde4/share/config/katesyntaxhighlightingrc
.
But there is surely a best method.
# 2  
Old 10-20-2012
You can remove one if ....Smilie

Code:
#!/bin/bash
SRCDIR=/root/.kde4/share/config
for MY_FILE in  $(ls $SRCDIR/kate*) ; do
    if [[ "$MY_FILE" == $SRCDIR/kate*. ]] ; then
            echo .
        else
            echo "Keep this : $MY_FILE"
        fi    
done

# 3  
Old 10-20-2012
Thank you.
But my question is rather how to prevent $(ls expr) from returning file name with '.' at the end.
# 4  
Old 10-20-2012
Quote:
Originally Posted by jcdole
Thank you.
But my question is rather how to prevent $(ls expr) from returning file name with '.' at the end.
ok try this.. No need to use for loop also...Smilie

Code:
ls $SRCDIR/kate* | grep -v "\.$"

This User Gave Thanks to pamu For This Post:
# 5  
Old 10-20-2012
Quote:
Originally Posted by pamu
ok try this.. No need to use for loop also...Smilie

Code:
ls $SRCDIR/kate* | grep -v "\.$"

Great. That do the job.
# 6  
Old 10-21-2012
don't need ls. never use for on ls.

Code:
for file in "$SRCDIR"/kate*; do
    [[ $file = *. ]] && continue
    # ...
done

or use extglob
Code:
shopt -s extglob
for file in "$SRCDIR"/kate!(*.); do
   # ...
done

This User Gave Thanks to neutronscott For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Return: can only `return' from a function or sourced script

Not sure where the problem is. I can run the script without any issue using the following command. . /opt/app/scripts/cdc_migration.sh But it fails with the below error when I try it this way /opt/app/scripts/cdc_migration.sh /opt/app/scripts/cdc_migration.sh: line 65: return: can only... (1 Reply)
Discussion started by: svajhala
1 Replies

2. Shell Programming and Scripting

Need output of script on screen and file with correct return status of the called script.

Hi, I am trying to capture logs of the script in the file as well as on the screen. I have used exec and tee command for this. While using exec command I am getting the correct output in the file but, script output is not getting displayed on the screen as it get executed. Below is my sample... (14 Replies)
Discussion started by: Prathmesh
14 Replies

3. Shell Programming and Scripting

Need help in finishing a bash script for listing subfolder by size in a large folder

Greetings everyone. I have seen that you do wonders here. I have a large folder on a Ubuntu linux. Organization main folder, inside 20 000 subfolders, and inside those subolders many other like 5-6 folders and files. I am interested to create an output to a txt file under the bash... (2 Replies)
Discussion started by: ultimo
2 Replies

4. Shell Programming and Scripting

Getting error in bash script; expr $a + 1: integer expression expected

Hi, I am new to shell/bash script. I am trying to run below script #!/bin/bash a=0 b=10 if then echo "a is equal to be" else echo "a is not equal to be" fi MAX=10 while do echo $a a='expr $a + 1' done (1 Reply)
Discussion started by: Mallikgm
1 Replies

5. Shell Programming and Scripting

Nested double quotes won't work in my bash script?

In a bash script I have: LSCMD="find /project/media/ -mindepth 2 -maxdepth 2 -name \"files*pkg\"" ALL_PACKAGES=$( $LSCMD | sort 2>/dev/null) But I get nothing returned. It's just all blank. If I run the find command in a terminal, I get dozens of hits. I figure it's the way how I'm... (3 Replies)
Discussion started by: superbbrr
3 Replies

6. Shell Programming and Scripting

Script to return first x characters from file

I need a script to read the first 1000 characters of a text file regardless of how many lines it will span. So if my first line has 2000 characters i only want the first 1000 from this line, if my first and second lines have 500 character each then i want all the characters from both lines and... (5 Replies)
Discussion started by: kelseyh
5 Replies

7. Shell Programming and Scripting

How to enter a return key in bash script?

Hi, I'm porting an install script from AIX to Red Hat (2.6.18-164.el5 #1 SMP) I have this script working in both AIX and HP-UX. The script is a wrapper for a Micro Focus Server Express install program. It responds to the install program questions with a here-now list. Responses includes... (14 Replies)
Discussion started by: duker61
14 Replies

8. Shell Programming and Scripting

Return to HTML file from shell script

Hi! I'm writing a simple script which I call on using a simple html button. The script also recives a simple argument. After the script is done I immediately want to return to my html page. But I dont know how to do that! #!/bin/sh echo "Content-type: text/html" echo "" if then echo... (1 Reply)
Discussion started by: crille
1 Replies

9. UNIX for Dummies Questions & Answers

help in finishing 2 commands

the first one: 1. i am trying to build a command that searches a text file and outputs the number of words which consists of two 'b' characters in each word separatly like barby (the b characters must be separated from one another) i tried to use grep filepath then i know that we need... (3 Replies)
Discussion started by: newby2
3 Replies
Login or Register to Ask a Question