Thread: How do I retain values across the bash subshell?

  1. #1
    Banned
    Join Date
    May 2007
    Location
    Berkeley, CA
    Posts
    329

    How do I retain values across the bash subshell?

    When I run the following script

    $ more p.sh
    Code:
    #!/bin/bash
    
    exec 3<&0;
    i=0;
    find . -type f </dev/fd/3 | while read fil; do
        i=`expr $i + 1`;
        grep tty $fil;
    done;
    exec 0<&3;
    echo found $i lines;
    I get the corresponding matches
    $ ./p.sh
    Binary file ./reterm matches
    grep tty $fil;
    real ttyname is: /dev/tty
    int msgsok, myttyfd;
    if (isatty(fileno(stdin)))
    myttyfd = fileno(stdin);
    else if (isatty(fileno(stdout)))
    myttyfd = fileno(stdout);
    else if (isatty(fileno(stderr)))
    myttyfd = fileno(stderr);
    fprintf(stderr, "can't find tty\n");
    ttyname(myttyfd);
    if((fd = open("/dev/tty", O_RDWR)) < 0) {
    Binary file ./ttytest matches
    /*if((fd = open("/dev/tty", O_RDONLY )) < 0) {
    perror("Can't open /dev/tty: ");
    printf("fd 0: %s\n", isatty(0) ? "tty" : "not a tty");
    printf("fd 1: %s\n", isatty(1) ? "tty" : "not a tty");
    printf("fd 2: %s\n", isatty(2) ? "tty" : "not a tty");
    printf("fd 0: %s\n", isatty(0) ? "tty" : "not a tty");
    printf("fd 1: %s\n", isatty(1) ? "tty" : "not a tty");
    printf("fd 2: %s\n", isatty(2) ? "tty" : "not a tty");*/
    Binary file ./lockme matches
    Binary file ./pass matches
    Binary file ./me matches
    if ((fd = open("/dev/tty", O_RDONLY)) < 0) {
    name = ttyname(fd);
    printf("real ttyname is: %s\n", name);
    void changeme(char *tty)
    strcpy(tty, nylons);
    Binary file ./tty matches
    if((fd = open("/dev/tty", O_RDWR)) < 0){
    perror("Can't open tty\n");
    grep: ./am: Permission denied
    found 0 lines
    $

    But the number of lines shows 0. I don't want change over to ksh shell. So now, what is my error is reasoning and how do I fix it?

  2. #2
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Don't start a subshell. By my reasoning, this:
    Code:
    for f in `find . -maxdepth 1 -type f`;
    is equivalent to your find | while combo.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  3. #3
    Banned
    Join Date
    May 2007
    Location
    Berkeley, CA
    Posts
    329
    Yes. That works. Thanks.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. putting values into an array
    By zdream8 in forum C Programming
    Replies: 15
    Last Post: 05-21-2008, 11:18 PM
  2. Replies: 1
    Last Post: 12-30-2007, 10:08 AM
  3. Need help with project
    By chrisa777 in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2006, 05:01 PM
  4. Sending values to a control
    By Zyk0tiK in forum C Programming
    Replies: 6
    Last Post: 12-02-2005, 06:29 PM
  5. Replies: 1
    Last Post: 02-03-2005, 03:33 AM