{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# read" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "read -a arr\n", "\n", "for s in ${arr[@]}; do echo $s; done" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# join" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "- 1\r\n", "- 2\r\n", "+ 1\r\n", "+ 2\r\n", "+ 3\r\n", "+ 4\r\n", "- 1\r\n", "- 2\r\n", "- 3\r\n", "- 4\r\n", "- 5\r\n", "- 6" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "arr01=(1 2)\n", "printf -- '- %s\\n' \"${arr01[@]}\"\n", "arr02=(${arr01[@]} 3 4)\n", "printf -- '+ %s\\n' \"${arr02[@]}\"\n", "arr02+=(5 6)\n", "printf -- '- %s\\n' \"${arr02[@]}\"" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# cmd result to array" ] }, { "cell_type": "code", "execution_count": 198, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "-rw-r--r-- 1 root root 643 May 5 17:34 /etc/hosts\n", "drwxr-xr-x 2 root root 4096 May 21 14:01 /etc/hosts dummyfolder\n", "-rw-r--r-- 1 root root 411 Oct 25 2018 /etc/hosts.allow\n", "-rw-r--r-- 1 root root 711 Oct 25 2018 /etc/hosts.deny\n", "\n" ] } ], "source": [ "p=/etc/hosts*\n", "ls $p -ld\n", "echo\n", "arr1=($p)" ] }, { "cell_type": "code", "execution_count": 199, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "4\n", "/etc/hosts /etc/hosts dummyfolder /etc/hosts.allow /etc/hosts.deny\n", "\n", ">>>/etc/hosts<<<\n", ">>>/etc/hosts dummyfolder<<<\n", ">>>/etc/hosts.allow<<<\n", ">>>/etc/hosts.deny<<<\n", "--------------\n", "4\n", "/etc/hosts /etc/hosts dummyfolder /etc/hosts.allow /etc/hosts.deny\n", "\n", ">>>/etc/hosts<<<\n", ">>>/etc/hosts dummyfolder<<<\n", ">>>/etc/hosts.allow<<<\n", ">>>/etc/hosts.deny<<<\n" ] } ], "source": [ "IFS=' '\n", "echo \"${#arr1[@]}\"\n", "echo \"${arr1[@]}\"\n", "echo \n", "for x in \"${arr1[@]}\"; do echo \">>>$x<<<\"; done\n", "\n", "echo \"--------------\"\n", "\n", "IFS=$'\\n'\n", "echo \"${#arr1[@]}\"\n", "echo \"${arr1[@]}\"\n", "echo \n", "for x in \"${arr1[@]}\"; do echo \">>>$x<<<\"; done" ] }, { "cell_type": "code", "execution_count": 205, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "- /etc/hosts\n", "- /etc/hosts dummyfolder\n", "- /etc/hosts.allow\n", "- /etc/hosts.deny\n" ] } ], "source": [ "printf -- '- %s\\n' \"${arr1[@]}\"" ] }, { "cell_type": "code", "execution_count": 206, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "/etc/hosts.allow\n", "/etc/hosts.deny\n" ] } ], "source": [ "arr2=($(printf -- '%s\\n' \"${arr1[@]}\" | grep [.]))\n", "printf -- '%s\\n' \"${arr2[@]}\"" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## inconsistent examples" ] }, { "cell_type": "code", "execution_count": 187, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "4\n", "/etc/hosts /etc/hosts dummyfolder /etc/hosts.allow /etc/hosts.deny\n", "\n", ">>>/etc/hosts /etc/hosts dummyfolder /etc/hosts.allow /etc/hosts.deny<<<\n", "--------------\n", "4\n", "/etc/hosts\n", "/etc/hosts dummyfolder\n", "/etc/hosts.allow\n", "/etc/hosts.deny\n", "\n", ">>>/etc/hosts\n", "/etc/hosts dummyfolder\n", "/etc/hosts.allow\n", "/etc/hosts.deny<<<\n" ] } ], "source": [ "IFS=' '\n", "echo \"${#arr1[*]}\"\n", "echo \"${arr1[*]}\"\n", "echo \n", "for x in \"${arr1[*]}\"; do echo \">>>$x<<<\"; done\n", "\n", "echo \"--------------\"\n", "\n", "IFS=$'\\n'\n", "echo \"${#arr1[*]}\"\n", "echo \"${arr1[*]}\"\n", "echo \n", "for x in \"${arr1[*]}\"; do echo \">>>$x<<<\"; done" ] }, { "cell_type": "code", "execution_count": 200, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "4\n", "/etc/hosts /etc/hosts dummyfolder /etc/hosts.allow /etc/hosts.deny\n", "\n", ">>>/etc/hosts<<<\n", ">>>/etc/hosts<<<\n", ">>>dummyfolder<<<\n", ">>>/etc/hosts.allow<<<\n", ">>>/etc/hosts.deny<<<\n", "--------------\n", "4\n", "/etc/hosts /etc/hosts dummyfolder /etc/hosts.allow /etc/hosts.deny\n", "\n", ">>>/etc/hosts<<<\n", ">>>/etc/hosts dummyfolder<<<\n", ">>>/etc/hosts.allow<<<\n", ">>>/etc/hosts.deny<<<\n" ] } ], "source": [ "IFS=' '\n", "echo ${#arr1[*]}\n", "echo ${arr1[*]}\n", "echo \n", "for x in ${arr1[*]}; do echo \">>>$x<<<\"; done\n", "\n", "echo \"--------------\"\n", "\n", "IFS=$'\\n'\n", "echo ${#arr1[*]}\n", "echo ${arr1[*]}\n", "echo \n", "for x in ${arr1[*]}; do echo \">>>$x<<<\"; done" ] }, { "cell_type": "code", "execution_count": 201, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "4\n", "/etc/hosts /etc/hosts dummyfolder /etc/hosts.allow /etc/hosts.deny\n", "\n", ">>>/etc/hosts<<<\n", ">>>/etc/hosts<<<\n", ">>>dummyfolder<<<\n", ">>>/etc/hosts.allow<<<\n", ">>>/etc/hosts.deny<<<\n", "--------------\n", "4\n", "/etc/hosts /etc/hosts dummyfolder /etc/hosts.allow /etc/hosts.deny\n", "\n", ">>>/etc/hosts<<<\n", ">>>/etc/hosts dummyfolder<<<\n", ">>>/etc/hosts.allow<<<\n", ">>>/etc/hosts.deny<<<\n" ] } ], "source": [ "IFS=' '\n", "echo ${#arr1[@]}\n", "echo ${arr1[@]}\n", "echo \n", "for x in ${arr1[@]}; do echo \">>>$x<<<\"; done\n", "\n", "echo \"--------------\"\n", "\n", "IFS=$'\\n'\n", "echo ${#arr1[@]}\n", "echo ${arr1[@]}\n", "echo \n", "for x in ${arr1[@]}; do echo \">>>$x<<<\"; done" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# str to array" ] }, { "cell_type": "code", "execution_count": 169, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ ">>>x \"x2 x3\"<<<\n", ">>>yy<<<\n", ">>>zzz<<<\n", "count: 3\n" ] } ], "source": [ "s='''\n", "x \"x2 x3\"\n", "yy\n", "zzz\n", "'''\n", "IFS=$'\\n' \n", "arr=($s)\n", "printf -- '>>>%s<<<\\n' \"${arr[@]}\"\n", "echo \"count: \" ${#arr[@]} " ] }, { "cell_type": "code", "execution_count": 143, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ ">>>x \"x2 x3\"<<<\n", ">>>yy<<<\n", ">>>zzz<<<\n", "count: 3\n" ] } ], "source": [ "IFS='\n", "' \n", "arr=($s)\n", "printf -- '>>>%s<<<\\n' \"${arr[@]}\"\n", "echo \"count: \" ${#arr[*]} " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## wrong examples" ] }, { "cell_type": "code", "execution_count": 141, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ ">>>\n", "x<<<\n", ">>>\"x2<<<\n", ">>>x3\"\n", "yy\n", "zzz\n", "<<<\n", "count: 3\n" ] } ], "source": [ "IFS=' ' \n", "arr=($s)\n", "printf -- '>>>%s<<<\\n' \"${arr[@]}\"\n", "echo \"count: \" ${#arr[*]} " ] }, { "cell_type": "code", "execution_count": 142, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ ">>>\n", "x \"x2 x3\"\n", "yy\n", "zzz\n", "<<<\n", "count: 1\n" ] } ], "source": [ "IFS='\\n'\n", "arr=($s)\n", "printf -- '>>>%s<<<\\n' \"${arr[@]}\"\n", "echo \"count: \" ${#arr[*]} " ] } ], "metadata": { "kernelspec": { "display_name": "MetaKernel Bash", "language": "bash", "name": "metakernel_bash" }, "language_info": { "file_extension": ".sh", "help_links": [ { "text": "MetaKernel Magics", "url": "https://metakernel.readthedocs.io/en/latest/source/README.html" } ], "mimetype": "text/x-bash", "name": "bash" } }, "nbformat": 4, "nbformat_minor": 4 }