Closed Thread
Results 1 to 3 of 3

Thread: Shell script string operator problem

  1. #1
    DarkLordoftheMonkeys's Avatar
    DarkLordoftheMonkeys is offline Programming Professional
    Join Date
    Oct 2009
    Location
    Massachussets
    Posts
    255
    Blog Entries
    56
    Rep Power
    11

    Shell script string operator problem

    Okay, I have an extremely simple shell script here. It uses the string operators ## and % to separate the directory name from the filename. I'm trying to build a larger script that replaces text in files, but I can't get this part to work:

    Code:
    #!/bin/bash
    #script: files
    dire=${1%/*};
    fil=${1##*/};
    echo "Directory: $dire";
    echo "File: $fil";
    unset dir;
    unset fil;
    When I type source files hello/goodbye I get:


    Directory: hello
    File: goodbye


    When I type source files ~/Desktop/hello I get:


    Directory: /Users/chuckboswick/Desktop
    File: hello


    When I type source files ~/Desktop/* I get:


    Directory: /Users/chuckboswick/Desktop
    File: 1226250839986du7.gif


    The problem appears to be that the shell script interpreter is interpreting the string as a file path immediately after I type it. I don't want it to do that. I want it to treat it as just a plain, simple string and not recognize it as a file path. I want to eventually get a list of files by typing for file in $(ls $fil) once I'm inside the directory. How can I protect the string $1 from being immediately interpreted, but still be able to use it to list files later?
    Life's too short to be cool. Be a nerd.

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Location
    Advertising world
    Posts
    Many

     
  3. #2
    Join Date
    Aug 2009
    Location
    ~/
    Posts
    918
    Rep Power
    19

    Re: Shell script string operator problem

    The beauty of shell scripting is 5000 commands to choose from.

    If I understand you correctly...

    Consider the following:
    basename to extract the filename
    dirname to extract the directory path
    and sed or awk to do your text replacement

    Everything including the loop will probably be 15 lines or less.

  4. #3
    asafe's Avatar
    asafe is offline Programmer
    Join Date
    Jul 2009
    Location
    Here
    Posts
    110
    Rep Power
    10

    Re: Shell script string operator problem

    Quote Originally Posted by DarkLordoftheMonkeys View Post
    for file in $(ls $fil)
    Never use ls to show the contents of a directory. Prefer :

    for file in *

Closed Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [HELP] convert Perl script to Bash shell script
    By Egypte in forum Linux Programming and Scripting
    Replies: 2
    Last Post: 04-24-2011, 05:37 PM
  2. A Shell script problem
    By veda87 in forum Linux Programming and Scripting
    Replies: 2
    Last Post: 08-18-2010, 07:51 PM
  3. Shell Script Help!
    By SeanStar in forum Linux Programming and Scripting
    Replies: 6
    Last Post: 08-12-2010, 05:20 PM
  4. Overload the [] operator with a string.
    By manux in forum C and C++
    Replies: 2
    Last Post: 04-03-2010, 05:37 AM
  5. Problem - Grep cammand in shell script
    By prasannasupp in forum Linux Programming and Scripting
    Replies: 2
    Last Post: 09-08-2009, 09:36 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts