1

Totally new in bash programming and got a problem like this:

N empty files should be created. The file sum N and also the file name should be given from users via command line. no loop and getopts should be used.

I've tried something like this which i found from google but it doesn't works. I got confused between linux and windows bash scripts. Hope someone can help me with the problem. Thank you!

#!/bin/bash
echo $N
:start
if [ $N > 0 ] 
  then
    touch xyzfile_$N
    $N = $N - 1
  pause
  goto start

else
  then 
    echo "no data will be created"
fi

The command line and the result should be (for example if N=7) look like this:

command line:

./createfiles -n filename 7

expected result:

filename_1,filename_2,filename_3...filename_7
cchi
  • 71
  • 6
  • 1
    @anubhava thank you for the information, I'm really new here on stackvoverflow. Now i know it. Thanks! – cchi Sep 19 '19 at 13:57
  • You're most welcome. Accepting a working answer also gives couple of points to you :) – anubhava Sep 19 '19 at 13:58

2 Answers2

0

You can do this using a recursive shell function:

fn() {
    # add some sanity checks to check parameters
    touch "$1_${2}"
    (($2 > 1)) && fn "$1" $(($2 - 1))
}

This part is a recursive call:

(($2 > 1)) && fn "$1" $(($2 - 1))

Where it basically calls itself by decrementing 1 from 2nd argument as long as $2 is greater than 1.

Then just call it as:

fn filename 7

It will create 7 files as:

filename_1  filename_2  filename_3  filename_4  filename_5  filename_6  filename_7
anubhava
  • 761,203
  • 64
  • 569
  • 643
  • A recursive construct is a loop that stacks the state of all iterations until it reaches the last one. I think the homework of the poster would mean something much simpler such as `touch filename_{1..5}` – Léa Gris Sep 16 '19 at 09:04
  • @LéaGris: You may know that `{1..5}` cannot work with a variable. Also recursion is not an explicit loop IMO. – anubhava Sep 16 '19 at 09:06
  • `createfile() { touch "$1_$(seq 1 "$2")"; }; createfile filename 7` – Léa Gris Sep 16 '19 at 09:12
  • Hmm it requires an external utility `seq` which doesn't exist on many systems like OSX by default. – anubhava Sep 16 '19 at 09:15
  • @anubhava don't know why that your code doesn't work, i solved the problem only with a for-loop. and now i try to solve it with `getopts` – cchi Sep 18 '19 at 12:33
  • @cchi: Click on this link: https://ideone.com/kvYJS1 to see a working demo. – anubhava Sep 18 '19 at 14:00
  • @anubhava your code demo looks good. but it create just the names not the real file, and the filename and sum should be as given arguments via command line. I removed the "echo" before "touch", changed the "filename" and "7" in the last line to $1 and $2, and now everything is perfect. Your recursion works super good. Thanks very much! – cchi Sep 19 '19 at 06:56
  • @cchi: Do you not see an `echo` before `touch` inside function body. I have purposely kept an `echo` before `touch` command in demo code so that you can see that function is working perfectly fine. You cannot create files on ideone demo otherwise. On your system just copy paste code from my answer and it will work fine. – anubhava Sep 19 '19 at 07:34
  • 1
    @anubhava ah yes, I forgot that the files cannot be created on demo. sorry. yes your code works perfectly like what you said. – cchi Sep 19 '19 at 07:42
0

I ended up the question with a for-loop like this:

createfile() {
    name="$1"
    num="$2"
    for((i=1;i<=num;i++))
        do
        touch "$1_${i}"
  done
}
createfile $1 ${2}

It works great actually. And now I tried to solve the problem with getopts and it seems a function call doesn't work in the case option

cchi
  • 71
  • 6