LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   fun shell scripts (https://www.linuxquestions.org/questions/linux-newbie-8/fun-shell-scripts-655236/)

tommytomthms5 07-12-2008 12:03 AM

fun shell scripts
 
im trying to get a grasp on how to make shell scripts...

my current experiment is to take for example folder ./1 has 25 files 1.examp 2.examp 3.exap.... and so on

i wanna just for the hell of it take each file and make 1000 copies of it...

so in the end folder ./1 will have 25,025 files in it (dont forget the original 25...)

heres my thoughts

Code:

#start with a

i=1 #as in i is an integer equal to 1

IFS=$'\n' # ignore spaces in files

let i++ # makes integer go up in numbers

if [ i<25000 ] # if integer i is less then 25000 (25 files times 1000)

then cp ./*.examp $(printf "%01d"$i).examp # copy all .examp files to 0(number_that_integer_equals).examp

else echo "finished" # when done tell me

fi #close loop

done # close operation

but it dosen't quite work like that...

kummiliim 07-12-2008 06:46 AM

for i in $(ls 1);do a=0;while [ "$a" -ne "1000" ];do cp "1/$i" "1/$i-copy-$a";let a++;done;done

: )

unSpawn 07-12-2008 06:57 AM

Or slightly shorter notation: "for ((i=1;i<10;i++)); do doSomethingWith${i}; done".

tommytomthms5 07-12-2008 08:02 AM

can you break down the code so i learn it thats the point not to run it

pixellany 07-12-2008 08:15 AM

Quote:

Originally Posted by tommytomthms5 (Post 3212165)
can you break down the code so i learn it thats the point not to run it

Time for a book. I would start with Bash Guide for Beginners by Machtelt Garrels---free at http://tldp.org

To give you a head start, the example given is a "for loop"--one of many kinds of loops that are a staple of any programming language. In Bash, the basic syntax is:

for <conditions>; do
<task1>
<task2>
<task3>
.
.
done

"conditions" can be a variety of things. In the 1st example, the construct:
for i in $(ls 1)
translates as follows:
run the ls command on the directory "1"
take this list and iterate through it, assigning each successive filename to the variable "i"

In this case, the loop finishes simply when it reaches the end of the list.


All times are GMT -5. The time now is 09:13 PM.