+ Reply to Thread
Results 1 to 3 of 3

Thread: Shell script string operator problem

  1. #1
    Programming Professional DarkLordoftheMonkeys has a spectacular aura about DarkLordoftheMonkeys has a spectacular aura about DarkLordoftheMonkeys's Avatar
    Join Date
    Oct 2009
    Location
    Massachussets
    Age
    20
    Posts
    214
    Blog Entries
    33

    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. #2
    Guru debtboy is just really nice debtboy is just really nice debtboy is just really nice debtboy is just really nice debtboy is just really nice debtboy's Avatar
    Join Date
    Aug 2009
    Location
    I'm in the... Black Lodge
    Posts
    908

    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.
    The owls are not what they seem...

  3. #3
    Learning Programmer asafe is on a distinguished road asafe's Avatar
    Join Date
    Jul 2009
    Location
    Here
    Posts
    83

    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 *

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

     

Similar Threads

  1. Detect Character Set
    By dargueta in forum General Programming
    Replies: 14
    Last Post: 09-10-2009, 04:55 PM
  2. Replies: 1
    Last Post: 02-20-2009, 11:53 AM
  3. C++: Just some stuff....
    By void_3e01 in forum Classes and Code Snippets
    Replies: 17
    Last Post: 10-14-2008, 11:54 AM
  4. OOP DB Access Wrapper
    By digioz in forum C# Programming
    Replies: 6
    Last Post: 09-07-2008, 12:58 PM
  5. SecurityAudit
    By vinay in forum Visual Basic Programming
    Replies: 27
    Last Post: 01-07-2008, 12:14 PM

Bookmarks

Bookmarks

     
        Algorithms and Data Structures

        Java tutorials

        Algorithms Forum

Posting Permissions

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