Shell Scripting

Hmmm.. Ok u mean to say that first store all IP's in a different file and then parse the file again and count the number of times each IP appears in it ??
 
neoindian said:
#! /bin/bash

getlist(){

line="$@"

echo $line

}

FILE=""

if [ "$0" == "" ]; then

FILE="/dev/stdin"

else

`${"$0"#"./"}`

FILE="$0"

fi

if [ ! -r $FILE ]; then

echo "$FILE : cannot read"

exit 2

fi

exec 3<&0

exec 0<$FILE

while read line

do

getlist $line

done

exec 0<&3

exit 0

If u just run this script it basically prints its source code. Thats the purpose . Hey Karan is this what u wanted.

as nuke rightly pointed out.... these programs are quite infamous.... and are usually 1 liners :)

ok someone point out the obvious to me... does this qualify:

Code:
#!/bin/sh

cat $0

:p

btw, both the above programs fall under cheat category.... its a smart answer, but not a acceptable answer to be a quine. you must not read the code and print it out... rather use commands in such a way and the output resembles the source 100% :)
 
To Josh's answer, u r rt buddy. Thats what i meant. And Karan, yeah i also was reading on quines quite a bit and this shell script falls under cheating.

Will find a solution without a cheat.
 
^^ But this wont work for me. Anyways thanks for that. ;)

I wrote a perl script for that but the file i store the IP's in is getting big and opening and closing file happens too many times. Will use MySQL.
 
KiD0M4N said:
Ahah... nice thread Nishant!

Ok, let me put forth a simple challenge.

Write a program which prints itself. Meaning the output should exactly be the mirror of the source code. So that if i did this.....

(assuming your file is called challenge.sh)

if [ `cat challenge.sh` = `./challenge.sh` ]; then
echo "Passed !"
fi

Any takers?

haven't tried with scripts.
But this c++ code should work ;-) in windows...

Code:
#include <iostream.h>
#include "stdio.h"
#include "stdlib.h"

void main()
{
	system("type test.cpp");

}

Good thread mate! :hap2:
 
hey buddy . I suck for asking u the above question. But buddy the code snippet you have provide falls under the cheat category. Bcoz basically what u r doing is just types out the file contents.

See the right way to do it, is by accessing the stack in runtime to get this thing done.

I will try to get the correct quine in C on linux...
 
I didn't know it falls under the cheat category. :-(

Anyways, i am not good with stack and stuff.. let me know when you get the code done man :)
 
Ok ppl... Wikipedia produced this when I was looking for a quine in C...
/* A simple quine (self-printing program), in standard C. */

/* Note: in designing this quine, we have tried to make the code clear
* and readable, not concise and obscure as many quines are, so that
* the general principle can be made clear at the expense of length.
* In a nutshell: use the same data structure (called "progdata"
* below) to output the program code (which it represents) and its own
* textual representation. */

#include <stdio.h>

void quote(const char *s)
/* This function takes a character string s and prints the
* textual representation of s as it might appear formatted
* in C code. */
{
int i;

printf(" \"");
for (i=0; s; ++i) {
/* Certain characters are quoted. */
if (s == '\\')
printf("\\\\");
else if (s == '"')
printf("\\\"");
else if (s == '\n')
printf("\\n");
/* Others are just printed as such. */
else
printf("%c", s);
/* Also insert occasional line breaks. */
if (i % 48 == 47)
printf("\"\n \"");
}
printf("\"");
}

/* What follows is a string representation of the program code,
* from beginning to end (formatted as per the quote() function
* above), except that the string _itself_ is coded as two
* consecutive '@' characters. */
const char progdata[] =
"/* A simple quine (self-printing program), in st"
"andard C. */\n\n/* Note: in designing this quine, "
"we have tried to make the code clear\n * and read"
"able, not concise and obscure as many quines are"
", so that\n * the general principle can be made c"
"lear at the expense of length.\n * In a nutshell:"
" use the same data structure (called \"progdata\"\n"
" * below) to output the program code (which it r"
"epresents) and its own\n * textual representation"
". */\n\n#include <stdio.h>\n\nvoid quote(const char "
"*s)\n /* This function takes a character stri"
"ng s and prints the\n * textual representati"
"on of s as it might appear formatted\n * in "
"C code. */\n{\n int i;\n\n printf(\" \\\"\");\n "
" for (i=0; s; ++i) {\n /* Certain cha"
"racters are quoted. */\n if (s == '\\\\')"
"\n printf(\"\\\\\\\\\");\n else if (s["
"i] == '\"')\n printf(\"\\\\\\\"\");\n e"
"lse if (s == '\\n')\n printf(\"\\\\n\");"
"\n /* Others are just printed as such. */\n"
" else\n printf(\"%c\", s);\n "
" /* Also insert occasional line breaks. */\n "
" if (i % 48 == 47)\n printf(\"\\\"\\"
"n \\\"\");\n }\n printf(\"\\\"\");\n}\n\n/* What fo"
"llows is a string representation of the program "
"code,\n * from beginning to end (formatted as per"
" the quote() function\n * above), except that the"
" string _itself_ is coded as two\n * consecutive "
"'@' characters. */\nconst char progdata[] =\n@@;\n\n"
"int main(void)\n /* The program itself... */\n"
"{\n int i;\n\n /* Print the program code, cha"
"racter by character. */\n for (i=0; progdata[i"
"]; ++i) {\n if (progdata == '@' && prog"
"data[i+1] == '@')\n /* We encounter tw"
"o '@' signs, so we must print the quoted\n "
" * form of the program code. */\n {\n "
" quote(progdata); /* Quote all. */\n"
" i++; /* Skip second '"
"@'. */\n } else\n printf(\"%c\", p"
"rogdata); /* Print character. */\n }\n r"
"eturn 0;\n}\n";

int main(void)
/* The program itself... */
{
int i;

/* Print the program code, character by character. */
for (i=0; progdata; ++i) {
if (progdata == '@' && progdata[i+1] == '@')
/* We encounter two '@' signs, so we must print the quoted
* form of the program code. */
{
quote(progdata); /* Quote all. */
i++; /* Skip second '@'. */
} else
printf("%c", progdata); /* Print character. */
}
return 0;
}


But I wonder whether its really a Quine or not.... Ne1 to explain???
PS::: Found this link to be interesting... Have a look: ftp://quatramaran.ens.fr/pub/madore/selfrep/selfrep/
 
Back
Top