Archive for 2015
Membuat File Tidak Dapat di Copy : FUSE
Halo sobat, kali ini naga akan memberi anda tutorial tentang FUSE. Tutorial disini akan menjelaskan source code dimana, dalam file system FUSE file yang kita baca bisa di copy, tetapi setelah di copy file Original menjadi tidak bisa di buka dan tidak bisa di copy lagi. Tetapi file hasil copy-an bisa di copy dan dibaca. Berikut Source Code nya
#include <fuse.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <dirent.h>
#include <errno.h>
#include <sys/statfs.h>
static const char *dirpath = "/home/kevin/Documents/";
char alamat[1000];
static int xmp_getattr(const char *path, struct stat *stbuf)
{
int res;
char fpath[1000];
sprintf(fpath, "%s%s", dirpath, path);
res = lstat(fpath, stbuf);
if (res == -1)
return -errno;
return 0;
}
static int xmp_getdir(const char *path, fuse_dirh_t h, fuse_dirfil_t filler)
{
DIR *dp;
struct dirent *de;
int res = 0;
char fpath[1000];
sprintf(fpath, "%s%s", dirpath, path);
dp = opendir(fpath);
if(dp == NULL)
return -errno;
while((de = readdir(dp)) != NULL)
{
res = filler(h, de->d_name, de->d_type);
if(res != 0)
break;
}
closedir(dp);
return res;
}
static int xmp_read(const char *path, char *buf, size_t size, off_t offset)
{
int fd;
int res;
char fpath[1000];
sprintf(fpath, "%s%s", dirpath, path);
strcpy(alamat,path);
fd = open(fpath, O_RDONLY);
if(fd == -1)
return -errno;
res = pread(fd, buf, size, offset);
if(res == -1)
res = -errno;
close(fd);
return res;
}
static int xmp_mknod(const char *path, mode_t mode, dev_t rdev)
{
int res;
char fpath[1000];
sprintf(fpath, "%s%s", dirpath, path);
res = mknod(fpath, mode, rdev);
if(res == -1)
return -errno;
return 0;
}
static int xmp_chmod(const char *path, mode_t mode)
{
int res;
char fpath[1000];
sprintf(fpath, "%s%s", dirpath, path);
res = chmod(fpath, mode);
if(res == -1)
return -errno;
return 0;
}
static int xmp_write(const char *path, const char *buf, size_t size, off_t offset)
{
int fd;
int res;
int res1;
char fpath[1000],temp1[1000];
sprintf(fpath, "%s%s", dirpath, path);
fd = open(fpath, O_WRONLY);
sprintf(temp1, "%s%s", dirpath, alamat);
res1 = chmod(temp1, 0000);
if(res1 == -1)
res1 = -errno;
if(fd == -1)
return -errno;
res = pwrite(fd, buf, size, offset);
if(res == -1)
res = -errno;
close(fd);
return res;
}
static int xmp_open(const char *path, int flags)
{
int res;
char fpath[1000];
sprintf(fpath, "%s%s", dirpath, path);
res = open(fpath, flags);
if(res == -1)
return -errno;
close(res);
return 0;
}
static struct fuse_operations xmp_oper =
{
.getattr = xmp_getattr,
//.readlink = xmp_readlink,
.getdir = xmp_getdir,
.mknod = xmp_mknod,
//.mkdir = xmp_mkdir,
//.symlink = xmp_symlink,
//.unlink = xmp_unlink,
//.rmdir = xmp_rmdir,
//.rename = xmp_rename,
//.link = xmp_link,
.chmod = xmp_chmod,
//.chown = xmp_chown,
//.truncate = xmp_truncate,
//.utime = xmp_utime,
.open = xmp_open,
.read = xmp_read,
.write = xmp_write,
//.release = xmp_release,
//.fsync = xmp_fsync,
//.readdir = hello_readdir
};
int main(int argc, char *argv[])
{
return fuse_main(argc, argv, &xmp_oper);
}
Jangan lupa, ganti alamat yang berwarna merah dengan alamat yang ingin di mount. Untuk compile ketik di source code :
gcc -Wall fuse1.c `pkg-config fuse --cflags --libs` -o fuse1
Lalu jangan lupa membuat direktori dengan
mkdir /tmp/fuse
Lalu jalankan program dengan
./fuse1 /tmp/fuse
Jika sudah cek folder tmp/fuse pada partisi hardisk
#include <fuse.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <dirent.h>
#include <errno.h>
#include <sys/statfs.h>
static const char *dirpath = "/home/kevin/Documents/";
char alamat[1000];
static int xmp_getattr(const char *path, struct stat *stbuf)
{
int res;
char fpath[1000];
sprintf(fpath, "%s%s", dirpath, path);
res = lstat(fpath, stbuf);
if (res == -1)
return -errno;
return 0;
}
static int xmp_getdir(const char *path, fuse_dirh_t h, fuse_dirfil_t filler)
{
DIR *dp;
struct dirent *de;
int res = 0;
char fpath[1000];
sprintf(fpath, "%s%s", dirpath, path);
dp = opendir(fpath);
if(dp == NULL)
return -errno;
while((de = readdir(dp)) != NULL)
{
res = filler(h, de->d_name, de->d_type);
if(res != 0)
break;
}
closedir(dp);
return res;
}
static int xmp_read(const char *path, char *buf, size_t size, off_t offset)
{
int fd;
int res;
char fpath[1000];
sprintf(fpath, "%s%s", dirpath, path);
strcpy(alamat,path);
fd = open(fpath, O_RDONLY);
if(fd == -1)
return -errno;
res = pread(fd, buf, size, offset);
if(res == -1)
res = -errno;
close(fd);
return res;
}
static int xmp_mknod(const char *path, mode_t mode, dev_t rdev)
{
int res;
char fpath[1000];
sprintf(fpath, "%s%s", dirpath, path);
res = mknod(fpath, mode, rdev);
if(res == -1)
return -errno;
return 0;
}
static int xmp_chmod(const char *path, mode_t mode)
{
int res;
char fpath[1000];
sprintf(fpath, "%s%s", dirpath, path);
res = chmod(fpath, mode);
if(res == -1)
return -errno;
return 0;
}
static int xmp_write(const char *path, const char *buf, size_t size, off_t offset)
{
int fd;
int res;
int res1;
char fpath[1000],temp1[1000];
sprintf(fpath, "%s%s", dirpath, path);
fd = open(fpath, O_WRONLY);
sprintf(temp1, "%s%s", dirpath, alamat);
res1 = chmod(temp1, 0000);
if(res1 == -1)
res1 = -errno;
if(fd == -1)
return -errno;
res = pwrite(fd, buf, size, offset);
if(res == -1)
res = -errno;
close(fd);
return res;
}
static int xmp_open(const char *path, int flags)
{
int res;
char fpath[1000];
sprintf(fpath, "%s%s", dirpath, path);
res = open(fpath, flags);
if(res == -1)
return -errno;
close(res);
return 0;
}
static struct fuse_operations xmp_oper =
{
.getattr = xmp_getattr,
//.readlink = xmp_readlink,
.getdir = xmp_getdir,
.mknod = xmp_mknod,
//.mkdir = xmp_mkdir,
//.symlink = xmp_symlink,
//.unlink = xmp_unlink,
//.rmdir = xmp_rmdir,
//.rename = xmp_rename,
//.link = xmp_link,
.chmod = xmp_chmod,
//.chown = xmp_chown,
//.truncate = xmp_truncate,
//.utime = xmp_utime,
.open = xmp_open,
.read = xmp_read,
.write = xmp_write,
//.release = xmp_release,
//.fsync = xmp_fsync,
//.readdir = hello_readdir
};
int main(int argc, char *argv[])
{
return fuse_main(argc, argv, &xmp_oper);
}
Jangan lupa, ganti alamat yang berwarna merah dengan alamat yang ingin di mount. Untuk compile ketik di source code :
gcc -Wall fuse1.c `pkg-config fuse --cflags --libs` -o fuse1
Lalu jangan lupa membuat direktori dengan
mkdir /tmp/fuse
Lalu jalankan program dengan
./fuse1 /tmp/fuse
/tmp/fuse |
kevin/documents |
Dapat dilihat isi dari documents sama dengan fuse. Lalu buka direktori fuse, dan lakukan copy dan paste terhadap sebuah file. Maka hasilnya seperti gambar dibawah
Membuat Game Benteng Takeshi pada Terminal Menggunakan Pipes
Buat game gak harus susah susah. Melalui bahasa C dan metode pipes kita sudah bisa membuat game sederhana dan memainkannya melalui terminal pada linux.
Oke diatas adalah contoh ketika game tersebut dijalankan. Game tersebut bertema kan benteng takeshi. Jadi setiap player akan menaruh ranjau pada lubang 1 - 16. Lalu player satunya akan menebak, dan jika tebakan benar maka poin akan menjadi miliknya jika tidak poin akan menjadi milik lawan. Easy? Right Then, berikut adalah source code nya.
Nah anda bisa copy kode diatas dan di paste kan ke file berbeda dan di compile di terminal berbeda sehingga bisa memainkannya bersama.Oke diatas adalah contoh ketika game tersebut dijalankan. Game tersebut bertema kan benteng takeshi. Jadi setiap player akan menaruh ranjau pada lubang 1 - 16. Lalu player satunya akan menebak, dan jika tebakan benar maka poin akan menjadi miliknya jika tidak poin akan menjadi milik lawan. Easy? Right Then, berikut adalah source code nya.
Judul Spoiler:
Player1
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <stdio.h>
int main(){
int fd,fs,fsc,ffe1,ffe2;
int fc,ranjau[5],tebak[4];
int entry[16];
int counterentry=0,counterentry2=0;;
int flagentry=0;
int flagentry2=0;
char *myfifo = "/tmp/fifo1";
char *fifo2="/tmp/fifo2";
char *fifoscore="/tmp/fifoscore";
int flag;
mkfifo(myfifo,0666);
mkfifo(fifoscore,0666);
fd = open(myfifo, O_WRONLY);
fs = open(fifo2,O_RDONLY);
fsc = open(fifoscore,O_RDWR);
int i,j,counter=0;
int scorePlayer[3];
scorePlayer[1]=0;
scorePlayer[2]=0;
//-----------------------------------------------------------------------------------------------
while(counterentry+counterentry2<32){
for(i=0;i<4;i++)
{
ranjau[i]=0;
}
if(counterentry<16){
system("clear");
for(i=0;i<4;i++)
{
ranjau[i]=0;
}
printf("----------------------------\n");
printf(" \n");
printf(" PLAYER 1 PLAYER 2 \n");
printf(" %d %d \n",scorePlayer[1],scorePlayer[2]);
printf(" \n");
printf("----------------------------\n");
//printf("COUNTER = %d %d\n\n",counterentry,counterentry2);
for(i=0;i<4;i++)
{
printf("masukkan ranjau ke-%d : ",i+1);
scanf("%d",&ranjau[i]);
while(ranjau[i]<0 || ranjau[i]>16)
{
printf("Lubang tersebut tidak tersedia\n");
printf("masukkan ranjau ke-%d : ",i+1);
scanf("%d",&ranjau[i]);
}
for(j=0;j<counterentry;j++)
{
while(ranjau[i]==entry[j])
{
if(ranjau[i]==0) break;
printf("Anda telah memasukkan ranjau ke lubang tersebut\n");
printf("masukkan ranjau ke-%d : ",i+1);
scanf("%d",&ranjau[i]);
}
}
if(ranjau[i]!=0)
{
entry[counterentry]=ranjau[i];
counterentry++;
}
if(counterentry>=16) break;
}
}
printf("%d %d %d %d = %d\n",ranjau[0],ranjau[1],ranjau[2],ranjau[3], counterentry);
ranjau[4]=counterentry;
write(fd,ranjau,sizeof(ranjau));
if(counterentry+counterentry2>=32) break;
//-----------------------------------------------------------------------------------------------
system("clear");
printf("----------------------------\n");
printf(" \n");
printf(" PLAYER 1 PLAYER 2 \n");
printf(" %d %d \n",scorePlayer[1],scorePlayer[2]);
printf(" \n");
printf("----------------------------\n");
//printf("COUNTERINI = %d %d\n\n",counterentry,counterentry2);
read(fsc,scorePlayer,sizeof(scorePlayer));
read(fs, ranjau, sizeof(ranjau));
counterentry2=ranjau[4];
system("clear");
printf("----------------------------\n");
printf(" \n");
printf(" PLAYER 1 PLAYER 2 \n");
printf(" %d %d \n",scorePlayer[1],scorePlayer[2]);
printf(" \n");
printf("----------------------------\n");
//printf("COUNTERTEBAK = %d %d\n\n",counterentry,counterentry2);
printf("READ : %d %d %d %d = %d\n",ranjau[0],ranjau[1],ranjau[2],ranjau[3],counter);
for(i=0;i<4;i++)
{
printf("masukkan tebakan ke-%d : ",i+1);
scanf("%d",&tebak[i]);
while(tebak[i]<=0 || tebak[i]>16)
{ printf("Lubang tersebut tidak tersedia\n");
printf("masukkan tebakan ke-%d : ",i+1);
scanf("%d",&tebak[i]);
}
for(j=0;j<i;j++)
{
while(tebak[i]==tebak[j])
{
printf("Anda telah menebak lubang tersebut\n");
printf("masukkan tebakan ke-%d : ",i+1);
scanf("%d",&tebak[i]);
}
}
}
for(j=0;j<4;j++){
flag=0;
for(i=0;i<4;i++)
{
if(ranjau[j]==tebak[i]) flag=1;
if(ranjau[j]==0) flag=2;
}
if(flag==1) scorePlayer[1]=scorePlayer[1]+1;
else if(flag==0) scorePlayer[2]=scorePlayer[2]+1;
}
write(fsc,scorePlayer,sizeof(scorePlayer));
if(counterentry+counterentry2>=32) break;
}
//-----------------------------------------------------------------------------------------------
read(fsc,scorePlayer,sizeof(scorePlayer));
system("clear");
printf("----------------------------\n");
printf(" \n");
printf(" PLAYER 1 PLAYER 2 \n");
printf(" %d %d \n",scorePlayer[1],scorePlayer[2]);
printf(" \n");
printf("----------------------------\n");
if(scorePlayer[1]>scorePlayer[2])
system("zenity --error --title=GAMEOVER --text=PLAYER1WIN");
else if(scorePlayer[1]==scorePlayer[2])
system("zenity --error --title=GAMEOVER --text=DRAW!!");
else
system("zenity --error --title=GAMEOVER --text=PLAYER2WIN");
close (fd);
close (fc);
close (fs);
unlink(myfifo);
return 0;
}
Judul Spoiler:
Player 2
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <stdio.h>
int main(){
int fd,fc,fs,fsc,ffe1,ffe2;
char * myfifo = "/tmp/fifo1";
char *fifo2="/tmp/fifo2";
char *fifoscore="/tmp/fifoscore";
int entry[16];
int counterentry=0,flagentry=0;
int flagentry2=0;
int counterentry2=0;
mkfifo(fifo2,0666);
mkfifo(fifoscore,0666);
int ranjau[5];
int tebak[4];
int i,j;
int scorePlayer[3];
int counterplayer2=0;
int flag=0;
scorePlayer[1]=0;
scorePlayer[2]=0;
fd = open(myfifo, O_RDONLY);
fs = open(fifo2,O_WRONLY);
fsc = open(fifoscore,O_RDWR);
//-----------RRREEEEAAADDDD
while(counterentry+counterentry2<32){
read(fd, ranjau, sizeof(ranjau));
int flagcounter=0;
if(ranjau[0]==0 && ranjau[1]==0 && ranjau[2]==0 && ranjau[3]==0) flagcounter=1;
if(flagcounter==0)
{
system("clear");
printf("----------------------------\n");
printf(" \n");
printf(" PLAYER 1 PLAYER 2 \n");
printf(" %d %d \n",scorePlayer[1],scorePlayer[2]);
printf(" \n");
printf("----------------------------\n");
//printf("COUNTER = %d %d\n\n",counterentry,counterentry2);
counterentry2=ranjau[4];
printf("READ : %d %d %d %d = %d\n",ranjau[0],ranjau[1],ranjau[2],ranjau[3],counterentry);
for(i=0;i<4;i++)
{
printf("masukkan tebakan ke-%d : ",i+1);
scanf("%d",&tebak[i]);
while(tebak[i]<=0 || tebak[i]>16)
{ printf("Lubang tersebut tidak tersedia\n");
printf("masukkan tebakan ke-%d : ",i+1);
scanf("%d",&tebak[i]);
}
for(j=0;j<i;j++)
{
while(tebak[i]==tebak[j])
{
printf("Anda telah menebak lubang tersebut\n");
printf("masukkan tebakan ke-%d : ",i+1);
scanf("%d",&tebak[i]);
}
}
}
for(j=0;j<4;j++){
flag=0;
for(i=0;i<4;i++)
{
if(ranjau[j]==tebak[i]) flag=1;
if(ranjau[j]==0) flag=2;
}
if(flag==1) scorePlayer[2]=scorePlayer[2]+1;
else if(flag==0) scorePlayer[1]=scorePlayer[1]+1;
}
}
write(fsc,scorePlayer,sizeof(scorePlayer));
//-------------------------------------------------------------------------------------
if(counterentry<16){
system("clear");
printf("----------------------------\n");
printf(" \n");
printf(" PLAYER 1 PLAYER 2 \n");
printf(" %d %d \n",scorePlayer[1],scorePlayer[2]);
printf(" \n");
printf("----------------------------\n");
//printf("COUNTER = %d %d\n\n",counterentry,counterentry2);
for(i=0;i<4;i++)
{
printf("masukkan ranjau ke-%d : ",i+1);
scanf("%d",&ranjau[i]);
while(ranjau[i]<0 || ranjau[i]>16)
{
printf("Lubang tersebut tidak tersedia\n");
printf("masukkan ranjau ke-%d : ",i+1);
scanf("%d",&ranjau[i]);
}
for(j=0;j<counterentry;j++)
{
while(ranjau[i]==entry[j])
{
if(ranjau[i]==0) break;
printf("Anda telah memasukkan ranjau ke lubang tersebut\n");
printf("masukkan ranjau ke-%d : ",i+1);
scanf("%d",&ranjau[i]);
}
}
if(ranjau[i]!=0)
{
entry[counterentry]=ranjau[i];
counterentry++;
}
if(counterentry>=16) break;
}
printf("%d %d %d %d %d= %d\n",ranjau[0],ranjau[1],ranjau[2],ranjau[3], ranjau[4], counterentry);
ranjau[4]=counterentry;
write(fs,ranjau,sizeof(ranjau));
read(fsc,scorePlayer,sizeof(scorePlayer));
}
}
write(fsc,scorePlayer,sizeof(scorePlayer));
system("clear");
printf("----------------------------\n");
printf(" \n");
printf(" PLAYER 1 PLAYER 2 \n");
printf(" %d %d \n",scorePlayer[1],scorePlayer[2]);
printf(" \n");
printf("----------------------------\n");
close (fd);
close (fc);
close (fs);
unlink (fifo2);
return 0;
}
Moving Specific Extension File using C in Linux Ubuntu
Morning! If any of you want to have a folder with specific file extension and you so lazy to filter it. Here's the answer. Using daemon and C programming it works for me in my ubuntu.
Here's are the code. The red one is daemon code, the blue one is the C code to move the file.
#include <sys/types.h>
#include <sys/stat.h>
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <errno.h>
#include <unistd.h>
#include <syslog.h>
#include <string.h>
#include <dirent.h>
int main(void)
{ pid_t pid, sid;
pid = fork();
if (pid < 0){
exit(EXIT_FAILURE); }
if (pid > 0){
exit(EXIT_SUCCESS); }
umask(0);
sid = setsid();
if (sid < 0){
exit(EXIT_FAILURE); }
if ((chdir("/")) < 0){
printf("test");
exit(EXIT_FAILURE); }
close(STDIN_FILENO);
close(STDERR_FILENO); //////////////////////////////////////
while (1){
sleep(2);
DIR *dir;
char src[]="home/kevin/tes";
char dest[]="home/kevin/tester";
struct dirent *ent;
if ((dir = opendir ("home/kevin/tes")) != NULL)
{ while ((ent = readdir (dir)) != NULL)
{
int len_file;
len_file=strlen(ent->d_name);
if(len_file>2) {
char ext[3];ext[0]=ent->d_name[len_file-4];
ext[1]=ent->d_name[len_file-3];
ext[2]=int->d_name[len_file-2];
ext[3]=ent->d_name[len_file-1];
if(strcmp(ext,".txt")==0) { }
else {
char buf_src[100]; snprintf(buf_src,100,"%s/%s",src,ent->d_name);
char buf_dest[100]; snprintf(buf_dest,100,"%s/%s",dest,ent->d_name); rename(buf_src,buf_dest);
} } }
closedir (dir); /* could not open directory */ perror (""); } ///////////////////////////////////// }
exit(EXIT_SUCCESS);
}
1. Compile it using gcc *namefile* -o *outputfile*
2. Run your *outputfile*
3. Move the file to folder "tes"
4. You see 2 files with c extension and txt extension in tes folder
5. Well about 1 second system will move the c into tester folder
6. Voila! appears in tester folder
7. Using command ps aux you can see ./movfile still working.
8. You can kill it using pkill *namefile*
Here's are the code. The red one is daemon code, the blue one is the C code to move the file.
#include <sys/types.h>
#include <sys/stat.h>
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <errno.h>
#include <unistd.h>
#include <syslog.h>
#include <string.h>
#include <dirent.h>
int main(void)
{ pid_t pid, sid;
pid = fork();
if (pid < 0){
exit(EXIT_FAILURE); }
if (pid > 0){
exit(EXIT_SUCCESS); }
umask(0);
sid = setsid();
if (sid < 0){
exit(EXIT_FAILURE); }
if ((chdir("/")) < 0){
printf("test");
exit(EXIT_FAILURE); }
close(STDIN_FILENO);
close(STDERR_FILENO); //////////////////////////////////////
while (1){
sleep(2);
DIR *dir;
char src[]="home/kevin/tes";
char dest[]="home/kevin/tester";
struct dirent *ent;
if ((dir = opendir ("home/kevin/tes")) != NULL)
{ while ((ent = readdir (dir)) != NULL)
{
int len_file;
len_file=strlen(ent->d_name);
if(len_file>2) {
char ext[3];ext[0]=ent->d_name[len_file-4];
ext[1]=ent->d_name[len_file-3];
ext[2]=int->d_name[len_file-2];
ext[3]=ent->d_name[len_file-1];
if(strcmp(ext,".txt")==0) { }
else {
char buf_src[100]; snprintf(buf_src,100,"%s/%s",src,ent->d_name);
char buf_dest[100]; snprintf(buf_dest,100,"%s/%s",dest,ent->d_name); rename(buf_src,buf_dest);
} } }
closedir (dir); /* could not open directory */ perror (""); } ///////////////////////////////////// }
exit(EXIT_SUCCESS);
}
How it works :
1. Compile it using gcc *namefile* -o *outputfile*
2. Run your *outputfile*
3. Move the file to folder "tes"
4. You see 2 files with c extension and txt extension in tes folder
5. Well about 1 second system will move the c into tester folder
6. Voila! appears in tester folder
7. Using command ps aux you can see ./movfile still working.
8. You can kill it using pkill *namefile*
Bash Script Linux : Bilangan Prima
Bash Scripting dapat digunakan untuk berbagai macam hal. Salah satunya adalah untuk menentukan bilangan bilangan prima. Disini admin akan membantu teman teman untuk scripting bilangan prima dengan Bash.
1. Buka Terminal Linux
2. Ketikkan sudo su
Setelah mengetiikan sudo su silakan masukkan password sobat
3. Buat File Script
Inputkan ,/prima.sh
1. Buka Terminal Linux
2. Ketikkan sudo su
Setelah mengetiikan sudo su silakan masukkan password sobat
3. Buat File Script
Setelah sobat berhasil menjadi super user dengan sudo su. Silakan membuat file script. Disini admin menggunakan prima.sh. Sobat bisa mengganti nama file sobat. Cukup mengetik nano namafile.sh. Jangan lupa menyertakan " .sh " agar program tersebut bisa dijalankan.
4. Mulai Scripting
Akan muncul menu seperti diatas. Disini sobat akan memulai melakukan bash scripting. Silakan mengetikkan script kode berikut
#!/bin/bash
echo "Input Number : "
read input
for((i=2;i<$input;i++)); do
counter=0
for((j=2;j<$1;j++)); do
if [ $((i%j)) -eq 0 ];
then
counter = 1
break
fi
done
if [ $counter -eq 0 ];
then
echo $i
fi
done
Selesai scripting, silakan sobat tekan ctrl+x lalu Y lalu enter.
Bagi sobat yang ingin mengerti algoritma silakan membaca algoritma berikut
#!/bin/bash deklarasi bash
echo "Input Number : " mengeluarkan tulisan Input Number : pada terminal
read input user menginputkan angka dengan "input" sebagai variabel
for((i=2;i<$input;i++)); do dengan perulangan yang dimulai dari 2 karena 2 adalah bilangan prima paling awal. Dengan "input" sebagai batas
counter=0 mendeklarasikan counter yang akan digunakan untuk menentukan prima
for((j=2;j<$1;j++)); do perulangan untuk pembagi, sehingga i akan dibagi dengan j
if [ $((i%j)) -eq 0 ]; jika i mod j adalah 0. Atau i habis dibagi dengan j
then
counter = 1 maka counter 1. Berarti j adalah faktor dari i
break
fi
done
if [ $counter -eq 0 ]; jika counter = 0. Maka i akan di print ke terminal
then
echo $i
fi
done
5. Ubah Permission File
Ketikkan chmod 777 prima.sh. Kode ini perlu di inputkan agar file dapat di eksekusi
6. Jalankan File
Inputkan ,/prima.sh
Lalu masukkan batas. Misal 50 maka akan keluar input seperti diatas.