Jump to content



Photo

How to create a command sequence for this?


  • Please log in to reply
12 replies to this topic

#1 zoheb

zoheb

    Neowinian

  • Joined: 02-January 10
  • Location: haLLuNicaTeD pAradISe

Posted 17 October 2012 - 11:46

A. Write a command sequence, to create a directory structure like follows:
1. level 1 - 4 directories with random alphanumeric name of 8 characters each directory
2. level 2 - each level 1 directory will have 3 directories - yes, no and yesorno
3. level 3 - each level 3 directory will have 9 directories with directory named in series 1-9 e.g. [1, 2, 3...9]
4. level 4 - each level 4 directory will have 3 directories - yes, no and yesorno
5. all directories at each level will have a text file named abc.txt with random alpha numeric text of 200 characters


Can someone shed a light in a right direction that will help me to achieve this?

Thanks !!


#2 AstareGod

AstareGod

    Neowinian

  • Joined: 11-June 04
  • Location: Biloxi, MS
  • OS: Windows 8
  • Phone: HTC Windows Phone 8X

Posted 17 October 2012 - 12:40

I have a background in C#/VB.NET, so that will influence my answer:

You'll need a few nested For-Next loops to create the exact number of directories you need. You'll also need a pseudo-random number generator to generate the random alphanumeric characters, and to make the "yes, no, or yesorno" directories. The 1-9 directories are easy.

If you're using .NET, use a TextWriter to write the text files.

Have fun!

#3 simplezz

simplezz

    Neowinian Senior

  • Tech Issues Solved: 1
  • Joined: 01-February 12

Posted 17 October 2012 - 18:35

It could be done in pretty much any language, scripting or otherwise, but assuming you want it in bash:

#!/bin/bash

function random_chars ()
{
	tmp=$( < /dev/urandom tr -dc A-Za-z1-9 | head -c $1 )
	eval $2=$tmp
}

function create_abc ()
{
	local rand=''
	local file=$1'/abc.txt'

	random_chars 200 rand
	echo $rand > $file
	echo $file
}

function create_1to9 ()
{
	for i in 1 2 3 4 5 6 7 8 9
	do

		wd=$1'/'$i
		mkdir $wd
		echo $wd

		create_abc $wd
		create_yesno $wd 0

	done
}

function create_answer ()
{
	local answer=$1'/'$2

	mkdir $answer
	echo $answer

	create_abc $answer  

	if [ $3 == 1 ]
	then
		create_1to9 $answer
	fi
}

function create_yesno ()
{
	create_answer $1 'yes' $2
	create_answer $1 'no'  $2
	create_answer $1 'yesorno' $2
}

function create_dirs ()
{
	local ret=''

	for i in 1 2 3 4
	do
		random_chars 8 ret

		dir=$1'/'$ret
		mkdir $dir
		echo $dir
		
		create_abc $dir
		create_yesno $dir 1	  
		
	done

}

function main ()
{
	create_dirs $1
}

main $1


Copy/echo it into a file, chmod +x it.
Then to run:
./script targetdir

targetdir is where you want it to create the directory structure. I just used '.' ( current directory ) for testing.

My bash script knowledge is pretty awful, so I'm sure someone else could do better.

#4 Kami-

Kami-

    ♫ d(-_-)b ♫

  • Tech Issues Solved: 2
  • Joined: 28-July 08
  • Location: SandBox

Posted 17 October 2012 - 20:34

I have a background in C#/VB.NET, so that will influence my answer:

You'll need a few nested For-Next loops to create the exact number of directories you need. You'll also need a pseudo-random number generator to generate the random alphanumeric characters, and to make the "yes, no, or yesorno" directories. The 1-9 directories are easy.

If you're using .NET, use a TextWriter to write the text files.

Have fun!

That's about as efficient as using ice cream for a fire guard ;)

#5 simplezz

simplezz

    Neowinian Senior

  • Tech Issues Solved: 1
  • Joined: 01-February 12

Posted 17 October 2012 - 21:07

There was a slight bug in random_chars () that caused it to output the abc.txt file incorrectly. Change it to:

Edit: Actually, the tmp variable is superfluous. Just remove it entirely.

function random_chars ()
{
	eval $2=$( < /dev/urandom tr -dc A-Za-z1-9 | head -c $1 )
}


#6 OP zoheb

zoheb

    Neowinian

  • Joined: 02-January 10
  • Location: haLLuNicaTeD pAradISe

Posted 23 October 2012 - 17:10

Thanks for your responses.. . .

I came up with following command sequence :::::::::::::

bash$ for i in $(seq 4); do mkdir `< /dev/urandom tr -dc A-Z0-9 | head -c8`; done

bash$ for i in `ls`; do `mkdir -p $i/{yes,no,yesorno}/{1..9} `; done

bash$ find . -type d -exec touch {}/abc.txt  \;

Now I am stuck at creating "yes,no,yesorno" directory in directories with name 1 - 9

This is what I tried ::

bash$ for i in $(find . -type d -name [1-9]); do `mkdir -p /{yes,no,yesorno}`;done

but its prompting with error "permission denied"

What am I missing?

#7 Haggis

Haggis

    Neowinian Senior

  • Tech Issues Solved: 8
  • Joined: 13-June 07
  • Location: Near Stirling, Scotland
  • OS: Debian 7
  • Phone: Samsung Galaxy S3 LTE (i9305)

Posted 23 October 2012 - 19:54

change the permission of the folder

#8 Clearskies

Clearskies

    Neowinian

  • Joined: 10-November 11
  • Location: Ottawa, Canada
  • OS: W8 / Xubuntu
  • Phone: Samsung Galaxy SII X

Posted 23 October 2012 - 20:57

Thanks for your responses.. . .

I came up with following command sequence :::::::::::::

bash$ for i in $(seq 4); do mkdir `< /dev/urandom tr -dc A-Z0-9 | head -c8`; done

bash$ for i in `ls`; do `mkdir -p $i/{yes,no,yesorno}/{1..9} `; done

bash$ find . -type d -exec touch {}/abc.txt  \;

Now I am stuck at creating "yes,no,yesorno" directory in directories with name 1 - 9

This is what I tried ::

bash$ for i in $(find . -type d -name [1-9]); do `mkdir -p /{yes,no,yesorno}`;done

but its prompting with error "permission denied"

What am I missing?


What's happening is you are trying to create the folders /yes, /no, and /yesorno, which are in the root directory of your filesystem. What you want is to modify that line to be

for i in $(find . -type d -name [1-9]); do `mkdir -p $i/{yes,no,yesorno}`; done

Basically, you're missing that $i which would make the {yes,no,yesorno} a subdirectory of your random folders as opposed to a subdirectory of the root directory.

#9 OP zoheb

zoheb

    Neowinian

  • Joined: 02-January 10
  • Location: haLLuNicaTeD pAradISe

Posted 28 October 2012 - 04:27

What's happening is you are trying to create the folders /yes, /no, and /yesorno, which are in the root directory of your filesystem. What you want is to modify that line to be

for i in $(find . -type d -name [1-9]); do `mkdir -p $i/{yes,no,yesorno}`; done

Basically, you're missing that $i which would make the {yes,no,yesorno} a subdirectory of your random folders as opposed to a subdirectory of the root directory.



Thats what I needed. .. . .Thanks.. .



But now there are some other complications I am facing,,, :(

I need to redirect the output to output.txt and error to error.txt

I came up with this:






for i in $(seq 4); do mkdir `< /dev/urandom tr -dc A-Z0-9 | head -c8`<&- 2>$HOME/error.txt; done

for i in `ls` ; do `mkdir -p $i/{yes,no,yesorno}/{1..9}` <&- 1>$HOME/output.txt 2>>$HOME/error.txt; done

for i in $(find . -type d -name [1-9]); do `mkdir -p $i/{yes,no,yesorno} <&- 1>$HOME/output.txt 2>>$HOME/error.txt `;done

for i in $(find . -type d); do `< /dev/urandom tr -dc A-Z0-9 | head -c200 > $i/abc.txt <&- 2>>$HOME/error.txt `; done 

After that, I need to join all the command sequences into one single command.

When I tried joining the commands and placing done at the end, it seems to be something is missing.. please help

#10 +Karl L.

Karl L.

    xorangekiller

  • Tech Issues Solved: 15
  • Joined: 24-January 09
  • Location: Virginia, USA
  • OS: Debian Testing

Posted 28 October 2012 - 17:58

'done' indicates the end of a loop, not the end of a command sequence. 'joining' the command should be as simple as placing them all on one line with semicolons between them. Based on the four commands you quoted in your last post, your final command should look something like this:
for i in $(seq 4); do mkdir `< /dev/urandom tr -dc A-Z0-9 | head -c8`<&- 2>$HOME/error.txt; done; for i in `ls` ; do `mkdir -p $i/{yes,no,yesorno}/{1..9}` <&- 1>$HOME/output.txt 2>>$HOME/error.txt; done; for i in $(find . -type d -name [1-9]); do `mkdir -p $i/{yes,no,yesorno} <&- 1>$HOME/output.txt 2>>$HOME/error.txt `;done; for i in $(find . -type d); do `< /dev/urandom tr -dc A-Z0-9 | head -c200 > $i/abc.txt <&- 2>>$HOME/error.txt `; done


#11 OP zoheb

zoheb

    Neowinian

  • Joined: 02-January 10
  • Location: haLLuNicaTeD pAradISe

Posted 29 October 2012 - 01:31

'done' indicates the end of a loop, not the end of a command sequence. 'joining' the command should be as simple as placing them all on one line with semicolons between them. Based on the four commands you quoted in your last post, your final command should look something like this:

for i in $(seq 4); do mkdir `< /dev/urandom tr -dc A-Z0-9 | head -c8`<&- 2>$HOME/error.txt; done; for i in `ls` ; do `mkdir -p $i/{yes,no,yesorno}/{1..9}` <&- 1>$HOME/output.txt 2>>$HOME/error.txt; done; for i in $(find . -type d -name [1-9]); do `mkdir -p $i/{yes,no,yesorno} <&- 1>$HOME/output.txt 2>>$HOME/error.txt `;done; for i in $(find . -type d); do `< /dev/urandom tr -dc A-Z0-9 | head -c200 > $i/abc.txt <&- 2>>$HOME/error.txt `; done




Thank you. . .. Finally. .. :)



for i in $(seq 4); do mkdir `< /dev/urandom tr -dc A-Z0-9 | head -c8`<&- 2>$HOME/error.txt; done;for i in `ls`; do `mkdir -p $i/{yes,no,yesorno}/{1..9}` <&- 1>$HOME/output.txt 2>>$HOME/error.txt; done;for i in $(find . -type d -name [1-9]); do `mkdir -p $i/{yes,no,yesorno}` <&- 1>$HOME/output.txt 2>>$HOME/error.txt; done;for i in $(find . -type d); do `< /dev/urandom tr -dc A-Z0-9 | head -c200 > $i/abc.txt` <&- 2>>$HOME/error.txt; done


#12 OP zoheb

zoheb

    Neowinian

  • Joined: 02-January 10
  • Location: haLLuNicaTeD pAradISe

Posted 29 October 2012 - 05:43

I tried to edit the above post but can't see the edit button, ,, :(



btw, can we redirect the output of mkdir command to a file? i.e output.txt . . .coz its always empty.

And when I execute the command twice, I get an error "Unable to create a directory abc.txt"

How can I make this command to be executed "n" number of times?. . .I don't want to loop it . .. I need to execute the same command on the previous output it generated.


Now I can see the edit button. .

#13 +Karl L.

Karl L.

    xorangekiller

  • Tech Issues Solved: 15
  • Joined: 24-January 09
  • Location: Virginia, USA
  • OS: Debian Testing

Posted 29 October 2012 - 23:35

I think your problem is that mkdir doesn't actually output anything to screen unless it encounters an error; so redirecting its output to a file is virtually useless. If you really want it to print a message, try passing it the -v switch. As for the second part of your question about executing the command 'n' times, I have no idea what you are talking about. My best guess is that you want to create a directory inside of a directory that does not yet exist. To create any intermediate directories you could pass mkdir the -p switch. Putting all of the above together your command may look something like the following:
mkdir -pv 'rar' 1>output.txt 2>&1




Click here to login or here to register to remove this ad, it's free!