Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1043127
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T15:37:05+00:00 2026-05-16T15:37:05+00:00

Two questions: how can I write a shell variable from this script into its

  • 0

Two questions: how can I write a shell variable from this script into its child script?

Are there any easier ways to do this?

If you can’t follow what I’m doing, I’m:

1) starting with a list of directories whose names will be stored as values taken by $i

2) cd’ing to every value of $i and ls’ing its contents

3) echoing its contents into a new script with the name of the directory via cat

4) using echo and cat to write a new script that contains the ls’d values of $i and sends them all to a blogging email address called $i@tumblr.com

#/bin/sh
read -d '' commands <<EOF

#list of directories goes here
dir1
dir2
dir3
etc...    

EOF

for i in $commands
do

cd $SPECIALPATH/$i
echo ("#/bin/sh \n read -d '' directives <<EOF \n") | cat >> $i.sh
ls | cat >> $i.sh
echo ("EOF \n for q in $directives \n do \n uuencode $q $q | sendmail $i \n done \n") | cat >> $i.sh
# NB -- I am asking the script to write the shell variable $i into the new
# script, called $i.sh, as the email address specified, in the middle of an
# echo statement... I am well aware that it doesn't work as is
chmod +x $i.sh
./$i.sh    

done
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T15:37:06+00:00Added an answer on May 16, 2026 at 3:37 pm

    You are abusing felines a lot – you should simply redirect, rather than pipe to cat which appends.

    You can avoid the intermediary $i.sh file by bundling all the output that goes to the file with a single I/O redirection that pipes direct into a shell – no need for the intermediate file to clean up (you didn’t show that happening) or the chmod operation.

    I would have done this using braces:

    {
    echo "..."
    ls
    echo "..."
    } | sh
    

    However, when I looked at the script in that form, I realized that wasn’t necessary. I’ve left the initial part of your script unchanged, but the loop is vastly simpler like this:

    #/bin/sh
    read -d '' commands <<EOF
    
    #list of directories goes here
    dir1
    dir2
    dir3
    etc...    
    
    EOF
    
    for i in $commands
    do
        (
        cd $SPECIALPATH/$i
        ls |
        while read q
        do uuencode $q $q | sendmail $i
        done
        )
    done
    

    I’m assuming the sendmail command works – it isn’t the way I’d try sending email. I’d probably use mailx or something similar, and I’d avoid using uuencode too (I’d use a base-64 encoding, left to my own devices):

        do uuencode $q $q | mailx -s "File $q" $i@tumblr.com
    

    The script also uses parentheses around the cd command. It means that the cd command and what follows is run in a sub-shell, so the parent script does not change directory. In this case, with an absolute pathname for $SPECIALDIR, it would not matter much. But as a general rule, it often makes life easier if you isolate directory changes like that.

    I’d probably simplify it still further for general reuse (though I’d need to add something to ensure that SPECIALPATH is set appropriately):

    #/bin/sh
    
    for i in "$@"
    do
        (
        cd $SPECIALPATH/$i
        ls |
        while read q
        do uuencode $q $q | sendmail $i
        done
        )
    done
    

    I can then invoke it with:

    script-name $(<list-of-dirs)
    

    That means that without editing the script, it can be reused for any list of directories.


    Intermediate step 1:

    for i in $commands
    do
        (
        cd $SPECIALPATH/$i
        {
        echo "read -d '' directives <<EOF"
        ls 
        echo "EOF"
        echo "for q in $directives"
        echo "do"
        echo "    uuencode $q $q | sendmail $i"
        echo "done"
        } |
        sh
        )
    done
    

    Personally, I find it easier to read the generated script if the code that generates makes the generated script clear – using multiple echo commands. This includes indenting the code.

    Intermediate Step 2:

    for i in $commands
    do
        (
        cd $SPECIALPATH/$i
        {
        ls |
        echo "while read q"
        echo "do"
        echo "    uuencode $q $q | sendmail $i"
        echo "done"
        } |
        sh
        )
    done
    

    I don’t need to read the data into a variable in order to step through each item in the list once – simply read each line in turn. The while read mechanism is often useful for splitting up a line into multiple variables too: while read var1 var2 var3 junk will read the first field into $var1, the second into $var2, the third into $var3, and if there’s anything left over, it goes into $junk. If you’ve generated the data accurately, there won’t be any junk; but sometimes you have to deal with other people’s data.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 537k
  • Answers 537k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer It's not necessary to create a new controller action, but… May 17, 2026 at 1:49 am
  • Editorial Team
    Editorial Team added an answer The !Threads command displays only managed threads (it will discard… May 17, 2026 at 1:49 am
  • Editorial Team
    Editorial Team added an answer I used a LEFT OUTER JOIN below for ClosedBy, as… May 17, 2026 at 1:49 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

I'm trying to write a shell script that, when run, will set some environment
UPDATE: this is a repost of How to make shell scripts robust to source
I would like to execute a MySQL command in a shell script/cron job that
Is there a simple way to run a Python script on Windows/Linux/OS X? On
I would like to write a program that sets an environment variable in an
I've been thinking about this far too long and haven't gotten any idea, maybe
There is a general rule of OO design that you should model is-a relationships
Now my program generates two data files. a.txt and b.txt Take a.txt as an
at the moment I'm writing a simple script in ksh who should take some
I have two SWF files which I shall call container and slave . The

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.