Results 1 to 8 of 8

Thread: UNIX Shell - expansion of wildcards in variables

  1. #1
    Taz
    Taz is offline
    Senior Member Taz's Avatar
    Join Date
    Jan 2005
    Location
    London
    Posts
    2,152
    Thanks
    57
    Thanked
    29 times in 27 posts
    • Taz's system
      • Motherboard:
      • Gigabyte Z270 HD3P
      • CPU:
      • Intel Core i5 7600K
      • Memory:
      • Corsair CMK16GX4M2B3200C16R Vengeance LPX 16 GB
      • Storage:
      • Samsung 960 EVO M.2-2280 500GB (PCIe) + 1TB Sandisk Ultra II SSD (SATA)
      • Graphics card(s):
      • Asus NVIDIA GeForce GTX 1070 OC
      • PSU:
      • Corsair CS550M 550W Hybrid
      • Case:
      • NZXT Source 340
      • Operating System:
      • Windows 10 Pro
      • Monitor(s):
      • 34" Asus Designo Curve MX34VQ UWQHD Monitor
      • Internet:
      • Virgin Media M350

    UNIX Shell - expansion of wildcards in variables

    I'm trying to work around a shell functionality that expands wildcards in variables. Here's an explanation:

    1. set a variable that contains asterisks and newline characters with something like:

    abc="123***************\n456*********************\n789*******************\n"

    2. Use the variable in a for loop and iterate each line held in the variable. So, I set the IFS to be a newline character and use a for loop and echo the variable into it:

    IFS='
    '

    for line in `echo -e "$abc"`
    do
    # line also contains each file in the current directory as well as each line in the variable abc!
    done

    The problem is that the echo statement in the for loop exposes the asterisks in the variable abc which then causes the shell to list the contents of the current directory! Now, this is normal shell (ksh) behaviour but I need to be able to loop through my variable line by line and use the exact contents of each line without the shell expanding the asterisks and messing up my for loop.

    Any ideas?

  2. #2
    mush-mushroom b0redom's Avatar
    Join Date
    Oct 2005
    Location
    Middlesex
    Posts
    3,510
    Thanks
    201
    Thanked
    388 times in 294 posts
    • b0redom's system
      • Motherboard:
      • Some iMac thingy
      • CPU:
      • 3.4Ghz Quad Core i7
      • Memory:
      • 24GB
      • Storage:
      • 3TB Fusion Drive
      • Graphics card(s):
      • nViidia GTX 680MX
      • PSU:
      • Some iMac thingy
      • Case:
      • Late 2012 pointlessly thin iMac enclosure
      • Operating System:
      • OSX 10.8 / Win 7 Pro
      • Monitor(s):
      • Dell 2713H
      • Internet:
      • Be+

    Re: UNIX Shell - expansion of wildcards in variables

    Escape the asterisks.

    Or use PERL and split on newline.

  3. #3
    Taz
    Taz is offline
    Senior Member Taz's Avatar
    Join Date
    Jan 2005
    Location
    London
    Posts
    2,152
    Thanks
    57
    Thanked
    29 times in 27 posts
    • Taz's system
      • Motherboard:
      • Gigabyte Z270 HD3P
      • CPU:
      • Intel Core i5 7600K
      • Memory:
      • Corsair CMK16GX4M2B3200C16R Vengeance LPX 16 GB
      • Storage:
      • Samsung 960 EVO M.2-2280 500GB (PCIe) + 1TB Sandisk Ultra II SSD (SATA)
      • Graphics card(s):
      • Asus NVIDIA GeForce GTX 1070 OC
      • PSU:
      • Corsair CS550M 550W Hybrid
      • Case:
      • NZXT Source 340
      • Operating System:
      • Windows 10 Pro
      • Monitor(s):
      • 34" Asus Designo Curve MX34VQ UWQHD Monitor
      • Internet:
      • Virgin Media M350

    Re: UNIX Shell - expansion of wildcards in variables

    My problem is that I cannot escape the asterisks. I run a command that produces multi-line output and then I assign this output to a variable. Unfortunately the output contains lines with multiple asterisks.

  4. #4
    Senior Member manwithnoname's Avatar
    Join Date
    Dec 2005
    Posts
    1,050
    Thanks
    17
    Thanked
    26 times in 25 posts

    Re: UNIX Shell - expansion of wildcards in variables

    Are you sure echo statement does the variable expansion, it might be done inside the do loop. See what out put you get from
    echo $line

    edit: did see you were using ksh

    running under ubuntu 9.10, is this the output you are looking for?

    Code:
    ubuntu@ubuntu:~$ cat test
    #!/usr/bin/ksh
    
    abc="123***************\n456*********************\n789*******************\n"
    
    IFS='
    '
    
    for line in `echo -e $abc`
    do
     echo $line
    done
    
    
    ubuntu@ubuntu:~$ ./test
    123***************
    456*********************
    789*******************
    ubuntu@ubuntu:~$
    Last edited by manwithnoname; 27-03-2010 at 11:52 AM.

  5. #5
    Taz
    Taz is offline
    Senior Member Taz's Avatar
    Join Date
    Jan 2005
    Location
    London
    Posts
    2,152
    Thanks
    57
    Thanked
    29 times in 27 posts
    • Taz's system
      • Motherboard:
      • Gigabyte Z270 HD3P
      • CPU:
      • Intel Core i5 7600K
      • Memory:
      • Corsair CMK16GX4M2B3200C16R Vengeance LPX 16 GB
      • Storage:
      • Samsung 960 EVO M.2-2280 500GB (PCIe) + 1TB Sandisk Ultra II SSD (SATA)
      • Graphics card(s):
      • Asus NVIDIA GeForce GTX 1070 OC
      • PSU:
      • Corsair CS550M 550W Hybrid
      • Case:
      • NZXT Source 340
      • Operating System:
      • Windows 10 Pro
      • Monitor(s):
      • 34" Asus Designo Curve MX34VQ UWQHD Monitor
      • Internet:
      • Virgin Media M350

    Re: UNIX Shell - expansion of wildcards in variables

    ^ Yes, that is the output I am after. What i'm getting is the files in the current directory mixed in with the contents of the variable. I'll try removing the double-quotes when i'm back at work tomorrow. Something is expanding the asterisks!

  6. #6
    Taz
    Taz is offline
    Senior Member Taz's Avatar
    Join Date
    Jan 2005
    Location
    London
    Posts
    2,152
    Thanks
    57
    Thanked
    29 times in 27 posts
    • Taz's system
      • Motherboard:
      • Gigabyte Z270 HD3P
      • CPU:
      • Intel Core i5 7600K
      • Memory:
      • Corsair CMK16GX4M2B3200C16R Vengeance LPX 16 GB
      • Storage:
      • Samsung 960 EVO M.2-2280 500GB (PCIe) + 1TB Sandisk Ultra II SSD (SATA)
      • Graphics card(s):
      • Asus NVIDIA GeForce GTX 1070 OC
      • PSU:
      • Corsair CS550M 550W Hybrid
      • Case:
      • NZXT Source 340
      • Operating System:
      • Windows 10 Pro
      • Monitor(s):
      • 34" Asus Designo Curve MX34VQ UWQHD Monitor
      • Internet:
      • Virgin Media M350

    Re: UNIX Shell - expansion of wildcards in variables

    Now this gets interesting. The script that manwithnoname posted works fine. However, if you have asterisks at the beginning of a line then my terminal session actually hangs!

    Code:
    #!/usr/bin/ksh
    
    abc="***************************************************abc\n\n123***************\n456*********************\n789*******************\n"
    
    IFS='
    '
    
    for line in `echo -e $abc`
    do
     echo $line
    done
    The only difference is that I added the following text to the beginning of the variable 'abc':

    Code:
    ***************************************************abc\n\n
    Can anyone explain why this would cause my shell session to hang? Removing that extra text causes the script to run okay. I think it's something to do with having the asterisks at the beginning of a line and the newline character. It's almost as if the script expands the asterisks to the files in the current directory and the newline character causes the shell to try and execute those files.

  7. #7
    Taz
    Taz is offline
    Senior Member Taz's Avatar
    Join Date
    Jan 2005
    Location
    London
    Posts
    2,152
    Thanks
    57
    Thanked
    29 times in 27 posts
    • Taz's system
      • Motherboard:
      • Gigabyte Z270 HD3P
      • CPU:
      • Intel Core i5 7600K
      • Memory:
      • Corsair CMK16GX4M2B3200C16R Vengeance LPX 16 GB
      • Storage:
      • Samsung 960 EVO M.2-2280 500GB (PCIe) + 1TB Sandisk Ultra II SSD (SATA)
      • Graphics card(s):
      • Asus NVIDIA GeForce GTX 1070 OC
      • PSU:
      • Corsair CS550M 550W Hybrid
      • Case:
      • NZXT Source 340
      • Operating System:
      • Windows 10 Pro
      • Monitor(s):
      • 34" Asus Designo Curve MX34VQ UWQHD Monitor
      • Internet:
      • Virgin Media M350

    Re: UNIX Shell - expansion of wildcards in variables

    As a workaround, I am stripping out asterisks by piping the output to sed as follows:

    Code:
    #!/bin/sh
    
    abc=`echo "***************************************************abc\n\n123***************\n456*********************\n789*******************\n" | sed 's/\*//g'`
    
    IFS='
    '
    
    for line in `echo -e $abc`
    do
     echo $line
    done
    I'm not interested in the asterisks. Ultimately I will be running an external command and capturing the out of that command (which contains lines with asterisks).

  8. #8
    Senior Member manwithnoname's Avatar
    Join Date
    Dec 2005
    Posts
    1,050
    Thanks
    17
    Thanked
    26 times in 25 posts

    Re: UNIX Shell - expansion of wildcards in variables

    How are you creating what "abc" is, could you put it in a file (called mydatafile) and read line by line using something like:

    while read line
    do
    echo $line
    done < mydatafile

    if you cannot use a file, you can could use pipe to connect a script that generates the "abc" data (called generateabcdata) and writes on the stdout, to the script below (called processabcdata) which should read from the stdin. Run the 2 commands: generateabcdata | processabcdata

    #!/bin/sh
    while read line
    do
    echo $line
    done

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Replies: 14
    Last Post: 17-05-2008, 12:24 PM

Posting Permissions

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