シェルスクリプト

Linuxインフラエンジニアとして シェルも高いレベルで理解を求められる。。 では今回は bashのオプション -e エラーが発生したら途中で止めます。 シェルスクリプトの先頭に #!/bin/shと記載します。 はまったこと sh コマンドで実行する場合は sh -e xxx.sh…

C言語でネットワークプログラミング

まずはソケットにファイルディスクリプタを代入する。 socketはシステムコールです。 socketシステムコール TCPまたはUDP通信を行う場合に、OSにソケットの作成を依頼します。 #include<sys/types.h> #include<sys/socket.h> int socket(int domain, int…

perlの配列の(1..1000)

1番から1000番までを表示する方法。 Linuxではseqがあるからいいけど。 まぁperlも便利な方法がありますね。 #!/usr/bin/perl use strict;use warnings; my @box = (1..1000); foreach (@box){ print "$_\n";}~~

perlの配列のqw

qwを使用した場合は、 文字列時のシングルコーテーションやダブルコーテーション、または「カンマ」などを 文字のセパレート使用するものを省略できます。 #!/usr/bin/perl use strict;use warnings; my @box = qw(yoshida ando nakajima); foreach (@box) {…

perlの文字列の場合の配列

#!/usr/bin/perl use strict;use warnings; my @box = ("kengo","manabu","taku","takeshi","hiroyuki"); foreach(@box){ print "$_\n";}~

foreachについて

foreachについて。どうですか。 配列を展開するときなんかは便利ですね。 ちなみに文字列を配列にリスト代入する方法は異なります。C言語みたい。 #!/usr/bin/perl use strict;use warnings; my @box = (1,2,3); foreach (@box) { print $_;}~

perlで大事なもの

chomp・・・文字列比較の際に、改行コードを除く。perlで文字列検索するときは必須。意外と忘れる。 if文・・・・開始の{と終了の}が必要です。 出力演算にはprintを使用しましょう。!!

はい。perlです。STDINを使った場合について

まずはperl復習。だいぶ忘れている。。 [root@gochamaze Perl]# cat stdin.pl#!/usr/bin/perl use strict;use warnings; print "What are your name ?\n"; my $box = <STDIN>; chomp($box); if ($box eq "yoshida"){ print "yoshida";}else { print "No yoshida";}</stdin>

/proc/statの計算式を入れてみた。(ただしこれは計算式が間違っています)。後日修正予定。。。

[root@gochamaze C]# cat cpuinfo3.c#include <stdio.h>#include <string.h>#include <stdlib.h> typedef struct cpuinfo { char name[50]; int count1; int count2; int count3; int count4; int count5; int count6; int count7; int count8; int count9;} cpu; int main(void){ FILE *r</stdlib.h></string.h></stdio.h>…

C言語で/proc/statの値をfscanfしてみる

いつもfsanfして間違えるところは、fscanfをint型を受け取るとき &を付け忘れること。。。 気を付けましょう [root@gochamaze C]# cat cpuinfo3.c#include <stdio.h>#include <string.h>#include <stdlib.h> typedef struct cpuinfo { char name[50]; int count1; int count2; int count3;</stdlib.h></string.h></stdio.h>…

Ver検索のバージョンアップ版

指定したディレクトリ内のファイルの中身を検索し、 Verという文字がファイルにあれば、その一番したのVerを出す。 [root@gochamaze C]# cat directory.c#include <stdio.h>#include <dirent.h>#include <string.h> #define BUF 256 int main(void){ DIR *dp; FILE *rfp; struct dirent *d</string.h></dirent.h></stdio.h>…

C言語でDirectoryからファイルをひっぱり、ファイルの中を検索する

[root@gochamaze C]# cat directory.c#include <stdio.h>#include <dirent.h>#include <string.h> #define BUF 256 int main(void){ DIR *dp; FILE *rfp; struct dirent *dir; char *path = "/root/C/"; char buf[BUF]; char buf2[BUF]; char *sp; if *1 == NULL) { fprintf(stderr,"ERROR</string.h></dirent.h></stdio.h>…

sscanfを覚えよう。

指定した文字列から書式文字列に従ってデータを取得します. sscanf(s,"%s %s %s",name,name1,name2); #include <stdio.h>#int main(void){ char s[] = "yoshida yoshida1 yohida2"; char name[20]; char name1[20]; char name2[20]; sscanf(s,"%s %s %s",name,name1,</stdio.h>…

fscanfを使った場合

#include <stdio.h>#include <stdlib.h> #define MAX 1024 typedef struct meminfo { char name[40]; char ave[20]; char inch[10]; } s_meminfo; int main(void){ struct meminfo mem[MAX]; int i; int k; FILE *rfp; if ( (rfp = fopen("/proc/meminfo","r")) == NULL ){ fpri</stdlib.h></stdio.h>…

fscanfを使ってみよう。

まずはfscanfの説明。 ファイルポインタをあらかじめ用意して そこからファイルの内容をscanして あらかじめ準備しておいた配列に格納する。 fscanf(rfp,"%s %s %s",mem[i].name,mem[i].ave,mem[i].inch ) != EOF)

string型の変数の文字数を確認する方法

public class string2 { public static void main(String[] args) { int i; String s1 = "iiii"; i = s1.length(); System.out.print(s1); System.out.print("文字数は" + i + "です"); } }

javaでの文字連結方法

javaを書いていて思うけどやっぱりクラスが便利。 package string2; public class string2 { public static void main(String[] args) { String s1 = "iiii"; String s2 = "uiuiui"; s1 = s1.concat(s2); System.out.print(s1); } }

またまたjavaさん。次はstring

package string2; public class string2 { public static void main(String[] args) { String s1 = "yoshida"; String s2 = "taotao"; System.out.println(s1); System.out.println(s2); } }

またまたjavaさん 次は処理時間をjavaで計測する方法。 またまたクラスさんですわ。。。。 さすがjava クラスを知らんとなにもできない感じだな。。 package syori2;import java.util.Date; public class syori2 { public static void main(String[] args) {…

javaをすこしずつ②

またまたjava クラスが豊富なjavaさん ではrandom数字生成を package random2; public class ramdom2 { public static void main(String[] args) { double d; d = Math.random()*10; System.out.print((int)d); } }

javaを少しずつプロジェクト

今回はカレンダーコマンド javaは便利なクラスがあるなーと思いました。 月で今月の月の情報をgetしたい場合は、 配列の0計算のためプラス1をする必要があります。 package calendar2; import java.util.Calendar; public class calendar2 { public static v…

ちなみに wコマンドでは8文字制限を発見。 strncpy(uname, u->ut_user, USERSZ); /* force NUL term for printf */ if (formtype) { printf("%-9.8s%-9.8s", uname, u->ut_line); topコマンドも static FLD_t Fieldstab[] = {/* .lflg anomolies: P_UID, L_N…

この部分がすごく怪しいと思い、自分で最初に乗せたソースコードを記載したが 問題なく上の処理が実施された。。。。 なんで。。。。。 なぞなぞだ。。。。。 static const char *do_user(void){ static char buf[32]; static struct passwd *p; static int …

いつかまとめる予定③

psコマンドでUSER情報が8文字になるのはどうもこれが原因みたい。。。 ただなんでuidが8文字を超える場合は、UIDで表示するのかはわからない。 printf( "%8s %5d %5d %s %s %s %s", do_user(), P_pid, P_ppid, do_cpu(0), do_stime(), P_tty_text, do_time(P…

いつかまとめる予定②

さっきに続き とりあえずpsコマンドのソース minimal.c #ifdef __linux__/* return 1 if it works, or 0 for failure */static int stat2proc(int pid) { char buf[800]; /* about 40 fields, 64-bit decimal is about 20 chars */ int num; int fd; char* t…

いつかまとめる予定

メモ書き まずはソースコード #include <sys/stat.h>#include <sys/types.h>#include <sys/stat.h>#include <fcntl.h>#include <pwd.h> #define BUF 256 int main(void){ int p; char buf[800]; int rfp = 0; struct stat sb; struct passwd *k; rfp = open("/proc/2583/stat",O_RDONLY); printf("%d\n",rfp); fsta</pwd.h></fcntl.h></sys/stat.h></sys/types.h></sys/stat.h>…

二次元配列を使用してのfor文。

sizeofを使っています。 #include <stdio.h> int main(void){ char filename[][256] = {"aaa.txt","bbb.txt","ccc.txt"}; int i; int count = 0; FILE *rfp; count = sizeof(filename) / sizeof(filename[0]); for( i = 0; i < count; i++ ){ printf("%d\n",count); i</stdio.h>…

C言語 表示。

頭文字をつけると超ややこしい。 #include <stdio.h> int main(void){ /* 掛け算 */ int i; /* 行数 */ int k; /* 表示する数字 */ int p; int p1; int p2; int count = 1; int sum = 0; for (p = 1; p < 11; p++ ){ printf("%4d",p); } puts(""); puts("------------</stdio.h>…

表示にこだわる。

#include <stdio.h> int main(void){ int i; /* 行数 */ int k; /* 表示する数字 */ for (k = 1; k < 101; k++){ printf("%4d",k); if ( k % 10 == 0 ) { puts(""); } } }~</stdio.h>

C言語 少数点の計算

atofがなぜかわからないが stdlibなしでコンパイルが通る。。。。 それでatofが動作していないってえええええ gccコンパイル、なんとかいってくれーーーーーーーーーーーーーーーーーー!!! #include <stdio.h>#include <stdlib.h> #define BUF 256 int main(void){ double su</stdlib.h></stdio.h>…