curl 명령어를 이용해 리소스(파일이나 웹페이지)를 받아보자.


환경

  • Linux 기반 시스템
  • Bash shell(/bin/bash)
  • curl의 사용법


curl 명령어에 대해서

  • curl : Client URL의 약자로 지원하는 프로토콜들을 이용해 서버에 데이터를 보내거나 가져올 때 사용하는 도구입니다.
  • 사용자의 인터랙션 없이 사용할 수 있으며 많은 프로토콜과 메소드를 지원하며 다양한 옵션들을 이용한 많은 기능들을 사용할 수 있습니다.
  • 지원 프로토콜들 : DICT, FILE, FTP, FTPS, GOPHER, HTTP, HTTPS, IMAP, IMAPS, LDAP, LDAPS, POP3, POP3S, RTMP, RTSP, SCP, SFTP, SMB, SMBS, SMTP, SMTPS, TELNET, TFTP and ETC.


curl을 이용해 웹에 있는 리소스 받기

(방법 1) -o 또는 –output 옵션을 이용하기

  • 해당 옵션은 현재 디렉토리에만 받을 수 있습니다.
curl [URL] -o [RESOURCE_NAME]

or

curl [URL] --output [RESOURCE_NAME]


(방법 2) Redirection 이용하기

  • 원하는 PATH에 다운받을 수 있습니다.
curl [URL] > [RESOURCE_NAME_WITH_PATH]


예제 : curl을 이용해 cirros 이미지 받기

사용할 정보


(방법 1) -o 또는 –output 옵션 이용하기

  • 파일 다운로드
curl https://download.cirros-cloud.net/0.4.0/cirros-0.4.0-x86_64-disk.img -o cirros-0.4.0-x86_64-disk.img
  • MD5 해시값 확인
  • MD5 해시 명령어는 OS에 따라 다를 수 있습니다.
$ md5sum cirros-0.4.0-x86_64-disk.img
443b7623e27ecf03dc9e01ee93f67afe  cirros-0.4.0-x86_64-disk.img


(방법 2) Redirection 이용하기

  • 파일 다운로드
curl https://download.cirros-cloud.net/0.4.0/cirros-0.4.0-x86_64-disk.img > /home/twpower/test/cirros-0.4.0-x86_64-disk.img
  • MD5 해시값 확인
  • MD5 해시 명령어는 OS에 따라 다를 수 있습니다.
$ md5sum /home/twpower/test/cirros-0.4.0-x86_64-disk.img
443b7623e27ecf03dc9e01ee93f67afe


참고자료

Download resources(files or web pages) by using curl command.


Environment and Prerequisite

  • Linux base system
  • Bash shell(/bin/bash)
  • Usage of curl command


About curl command

  • curl : It stands for Client URL. It is a tool to transfer data from or to a server, using one of the supported protocols
  • The command is designed to work without user interaction. It supports many protocols and methods also with many options.
  • Supported Protocols : DICT, FILE, FTP, FTPS, GOPHER, HTTP, HTTPS, IMAP, IMAPS, LDAP, LDAPS, POP3, POP3S, RTMP, RTSP, SCP, SFTP, SMB, SMBS, SMTP, SMTPS, TELNET, TFTP and ETC.


Download resource using curl command

(Method 1) Use -o or –output option

  • These options only support download to current working directory.
curl [URL] -o [RESOURCE_NAME]

or

curl [URL] --output [RESOURCE_NAME]


(Method 2) Use redirection

  • You can download resource to any directory.
curl [URL] > [RESOURCE_NAME_WITH_PATH]


Example : Download cirros image using curl command

Requirements


(Method 1) Use -o or –output option

  • Download file
curl https://download.cirros-cloud.net/0.4.0/cirros-0.4.0-x86_64-disk.img -o cirros-0.4.0-x86_64-disk.img
  • Check MD5 hash value
  • MD5 hash value could be different depends on OS.
$ md5sum cirros-0.4.0-x86_64-disk.img
443b7623e27ecf03dc9e01ee93f67afe  cirros-0.4.0-x86_64-disk.img


(Method 2) Use redirection

  • Download file
curl https://download.cirros-cloud.net/0.4.0/cirros-0.4.0-x86_64-disk.img > /home/twpower/test/cirros-0.4.0-x86_64-disk.img
  • Check MD5 hash value
  • MD5 hash value could be different depends on OS.
$ md5sum /home/twpower/test/cirros-0.4.0-x86_64-disk.img
443b7623e27ecf03dc9e01ee93f67afe


Reference

쉘 스크립트에서 sudo 패스워드를 전달 받거나 스크립트에서 사용해보자


환경

  • Linux 기반 시스템
  • Bash shell(/bin/bash)


방법

  • -S 옵션과 |를 이용
echo "PASSWORD" | sudo -S apt-get update
  • 파일을 이용하는 경우
cat << EOF > password.txt
> PASSWORD
> EOF

cat password.txt | sudo -S apt-get update


  • --stdin 옵션과 |를 이용
echo "PASSWORD" | sudo --stdin apt-get update
  • 파일을 이용하는 경우
cat << EOF > password.txt
> PASSWORD
> EOF

cat password.txt | sudo --stdin apt-get update


참고자료

Process file or result of command in while loop line by line in shell script.


Environment and Prerequisite

  • Linux base system
  • Bash shell(/bin/bash)


Solution

  • Use -S option and |
echo "PASSWORD" | sudo -S apt-get update
  • Usage with file
cat << EOF > password.txt
> PASSWORD
> EOF

cat password.txt | sudo -S apt-get update


  • Use --stdin option and |
echo "PASSWORD" | sudo --stdin apt-get update
  • Usage with file
cat << EOF > password.txt
> PASSWORD
> EOF

cat password.txt | sudo --stdin apt-get update

Reference

Implement basic form of singly linked list


Environment and Prerequisite

  • C++


What is Singly Linked List?

Concept

  • It is a data structure which have datas in one node and point to other node sequentially.
  • It is an advantage compared with array in insertion and deletion. However accessing takes O(n).
  • It is a linear collection of data elements, whose order is not given by their physical placement in memory. Instead, each element points to the next. It is a data structure consisting of a collection of nodes which together represent a sequence.



Implementation

Common

  • list is a head of list
  • You can modify insertion, deletion and finding codes if you want.
#include <stdio.h>
#include <stdlib.h>

// Node
struct Node {
	int data;
	Node * next;
};

// Global list
Node * list;


Insertion/Delettion Common

  • It is easy to implement if we consider the head(or first element) of list
  • It is helpful if there are cur pointer which points current node and prev pointer which points previous node of current node. They are helpful in insertion and deletion.
  • In deletion, if there is a node that we want to delete, then it returns true. If not, then return false.


Insertion

  • Like below picture, find the position of new node. Then, point new node’s next to next node and previous node’s next to new node.
  • There are 3 types of functions.
  • add : add just new node
  • ascending_order_add : add node in middle(by ascending order)
  • add_unique : add only unique node



// Add - one by one
void add(int key) {

	Node * new_node = (Node*)malloc(sizeof(Node));
	new_node->data = key;
	new_node->next = NULL;

	// Check first element
	if (list == NULL) {
		list = new_node;
	}
	else {
		// Add new node to head
		new_node->next = list;
		list = new_node;
	}
}
// Add - add ascending order
void ascending_order_add(int key) {

	Node * new_node = (Node*)malloc(sizeof(Node));
	new_node->data = key;
	new_node->next = NULL;

	if (list == NULL) {
		list = new_node;
	}
	else {

		Node * cur = list;
		Node * prev = NULL;

		// If first element is larger than key
		if (cur->data > new_node->data) {
			new_node->next = cur;
			list = new_node;
		}
		// Other cases
		else {
			while (cur != NULL && cur->data < new_node->data) {
				prev = cur;
				cur = cur->next;
			}
			// Add in middle
			if (cur != NULL) {
				new_node->next = cur;
				prev->next = new_node;
			}
			// Add to end
			else {
				prev->next = new_node;
			}
		}
	}
}
// Add - add only unique value
void add_unique(int key) {
	Node * new_node = (Node*)malloc(sizeof(Node));
	new_node->data = key;
	new_node->next = NULL;

	if (list == NULL) {
		list = new_node;
	}
	else {
		Node * cur = list;

		// Duplication check
		while (cur != NULL) {
			if (cur->data == key) {
				return;
			}
			cur = cur->next;
		}

		new_node->next = list;
		list = new_node;
	}
}


Deletion

  • Like below picture, find the position of node which will be deleted. Then, points previous node’s next to current node’s(which will be deleted) next.
  • Use curand prev pointers.



// Remove
bool remove(int key) {

	if (list == NULL) {
		return false;
	}

	if (list->data == key) {
		Node * cur = list;
		list = list->next;
		free(cur);
		return true;
	}
	else {
		Node * cur = list->next;
		Node * prev = list;
		while (cur != NULL && cur->data != key) {
			prev = cur;
			cur = cur->next;
		}

		if (cur == NULL) return false;

		prev->next = cur->next;
		free(cur);
		return true;
	}
}


Code

#include <stdio.h>
#include <stdlib.h>

// Node
struct Node {
	int data;
	Node * next;
};

// Global list
Node * list;

// Init
void init() {

	if (list == NULL) {
		return;
	}
	else {
		Node * cur;
		cur = list;

		while (cur != NULL) {
			list = cur->next;
			free(cur);
			cur = list;
		}
	}
}

// Add - one by one
void add(int key) {

	Node * new_node = (Node*)malloc(sizeof(Node));
	new_node->data = key;
	new_node->next = NULL;

	// Check first element
	if (list == NULL) {
		list = new_node;
	}
	else {
		// Add new node to head
		new_node->next = list;
		list = new_node;
	}
}

// Add - add ascending order
void ascending_order_add(int key) {

	Node * new_node = (Node*)malloc(sizeof(Node));
	new_node->data = key;
	new_node->next = NULL;

	if (list == NULL) {
		list = new_node;
	}
	else {

		Node * cur = list;
		Node * prev = NULL;

		// If first element is larger than key
		if (cur->data > new_node->data) {
			new_node->next = cur;
			list = new_node;
		}
		// Other cases
		else {
			while (cur != NULL && cur->data < new_node->data) {
				prev = cur;
				cur = cur->next;
			}
			// Add in middle
			if (cur != NULL) {
				new_node->next = cur;
				prev->next = new_node;
			}
			// Add to end
			else {
				prev->next = new_node;
			}
		}
	}
}

// Add - add only unique value
void add_unique(int key) {
	Node * new_node = (Node*)malloc(sizeof(Node));
	new_node->data = key;
	new_node->next = NULL;

	if (list == NULL) {
		list = new_node;
	}
	else {
		Node * cur = list;

		// Duplication check
		while (cur != NULL) {
			if (cur->data == key) {
				return;
			}
			cur = cur->next;
		}

		new_node->next = list;
		list = new_node;
	}
}

// Remove
bool remove(int key) {

	if (list == NULL) {
		return false;
	}

	if (list->data == key) {
		Node * cur = list;
		list = list->next;
		free(cur);
		return true;
	}
	else {
		Node * cur = list->next;
		Node * prev = list;
		while (cur != NULL && cur->data != key) {
			prev = cur;
			cur = cur->next;
		}

		if (cur == NULL) return false;

		prev->next = cur->next;
		free(cur);
		return true;
	}
}

// Traverse
void traverse() {

	Node * cur = list;
	while (cur != NULL) {
		printf("%d ", cur->data);
		cur = cur->next;
	}
	printf("\n");

}

int main() {

	int arr[9] = { 2, 4, 6, 8, 1, 3, 5, 7, 9 };
	int arr_duplicated[18] = { 8, 1, 3, 2, 4, 6, 8, 1, 3, 5, 7, 9, 2, 4, 6, 5, 7, 9 };
	int arr_rmv[4] = { 2, 9, 8, 100 };


	// Add to list 1
	init();
	for (int i = 0; i < sizeof(arr) / sizeof(arr[0]); ++i) {
		add(arr[i]);
	}
	printf("After add(normal) : ");
	traverse();


	// Add to list 2
	init();
	for (int i = 0; i < sizeof(arr) / sizeof(arr[0]); ++i) {
		ascending_order_add(arr[i]);
	}
	printf("After add(ascending) : ");
	traverse();


	// Add to list 3
	init();
	for (int i = 0; i < sizeof(arr_duplicated) / sizeof(arr_duplicated[0]); ++i) {
		add_unique(arr_duplicated[i]);
	}
	printf("After add(unique) : ");
	traverse();


	// Remove specific values in list
	for (int i = 0; i < sizeof(arr_rmv) / sizeof(arr_rmv[0]); ++i) {
		remove(arr_rmv[i]);
	}
	printf("After remove : ");
	traverse();

	return 0;

}

Result

After add(normal) : 9 7 5 3 1 8 6 4 2
After add(ascending) : 1 2 3 4 5 6 7 8 9
After add(unique) : 9 7 5 6 4 2 3 1 8
After remove : 7 5 6 4 3 1


Reference