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

  • SEARCH
  • Home
  • 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 6852937
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T01:27:05+00:00 2026-05-27T01:27:05+00:00

#!/bin/sh param1=$1 param2=$2 recursive(){ mkdir -p $2 cd $1 for file in `ls $1`;

  • 0
#!/bin/sh

param1=$1
param2=$2

recursive(){
   mkdir -p $2
   cd $1
   for file in `ls $1`; do
      [ $file = "." -o $file = ".." ] && continue
      [ -d $file ] && recursive $1"/"$file $2"/"$file
      [ -f $file ] && ln -s $1"/"$file $2"/"$file
   done
}

recursive $param1 $param2

If I execute this script, that it call self (recursive). Why not scan all directories?

(excuse me: my english is poor)

  • 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-27T01:27:05+00:00Added an answer on May 27, 2026 at 1:27 am
    1. Don’t call ls in backquotes when wildcard will do and is more reliable, that is:

      for file in "$1"/*; do
      
    2. The variable expansions have to be inside the quotes or it will all fall down on any special character including space. You should use "$1", "$2" and "$1/$file" "$2/$file".

    3. Variables are global in shell by default. So the recursive call clobbers the outer call’s file variable. There are two possible workarounds:

      1. Declare the variable local with

        local file
        

        at the beginning of the function. This is “bashishm”, i.e. it’s not defined by POSIX shell standard, so some shells don’t have it.

      2. Wrap the function in round parenthesis instead of curly brackets. That will make the function run in a subshell, which can’t clobber it’s parents’s variables.

    4. Oh, you don’t need the param1 and param2. The positional parameters are scoped.

    5. William Pusell (see the other answer) notices one more thing. Either cd into the argument directory or prefix it to the path, but don’t do both.

    With all the fixes you should get to:

    recursive() (
       mkdir -p "$2"
       for file in "$1"/*; do
          [ "$file" = "." -o "$file" = ".." ] && continue
          [ -d "$file" ] && recursive "$1/$file" "$2/$file"
          [ -f "$file" ] && ln -s "$1/$file" "$2/$file"
       done
    )
    
    recursive "$1" "$2"
    

    I didn’t test it, so there may still be some problem left.

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

Sidebar

Related Questions

I have a sh/bash script that needs to call a batch file with parameters
I've got a simple python script in file 'bin/test': #!/usr/bin/env python import argparse PROGRAM_NAME
I found this call in an app I started managing some time ago: /usr/bin/sftp
this site is extremely cool ( http://www.gsp.com/cgi-bin/man.cgi?section=3&topic=strncpy ) but a bit unformatted, it shows
I have this script #!/bin/sh if [ $# -ne 3 ] ; then echo
#!/bin/bash if test $# == 4; then echo $*; else echo args-error >&2; fi;
#!/usr/bin/python # -*- coding: iso-8859-1 -*- import Tkinter import twitter class simpleapp_tk(Tkinter.Tk): def __init__(self,parent):
#!/usr/bin/python # -*- coding: iso-8859-1 -*- import Tkinter class simpleapp_tk(Tkinter.Tk): def __init__(self,parent): Tkinter.Tk.__init__(self,parent) self.parent=parent
Suppose a shell script (/bin/sh or /bin/bash) contained several commands. How can I cleanly
My name is abbi My first perl script run on linux machine This script

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.