{"id":38710967,"url":"https://github.com/heidsoft/heidsoft-shell","last_synced_at":"2026-01-17T11:00:11.420Z","repository":{"id":11444487,"uuid":"13902599","full_name":"heidsoft/heidsoft-shell","owner":"heidsoft","description":"Shell使用指南","archived":false,"fork":false,"pushed_at":"2018-12-06T03:47:56.000Z","size":35,"stargazers_count":0,"open_issues_count":0,"forks_count":2,"subscribers_count":3,"default_branch":"master","last_synced_at":"2024-04-15T00:43:07.861Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"Shell","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/heidsoft.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2013-10-27T14:34:17.000Z","updated_at":"2018-12-06T03:48:00.000Z","dependencies_parsed_at":"2022-09-13T13:12:32.859Z","dependency_job_id":null,"html_url":"https://github.com/heidsoft/heidsoft-shell","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/heidsoft/heidsoft-shell","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/heidsoft%2Fheidsoft-shell","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/heidsoft%2Fheidsoft-shell/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/heidsoft%2Fheidsoft-shell/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/heidsoft%2Fheidsoft-shell/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/heidsoft","download_url":"https://codeload.github.com/heidsoft/heidsoft-shell/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/heidsoft%2Fheidsoft-shell/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":28506593,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-01-17T10:25:30.148Z","status":"ssl_error","status_checked_at":"2026-01-17T10:25:29.718Z","response_time":85,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.6:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2026-01-17T11:00:09.750Z","updated_at":"2026-01-17T11:00:11.330Z","avatar_url":"https://github.com/heidsoft.png","language":"Shell","funding_links":[],"categories":[],"sub_categories":[],"readme":"# 系统命令\n## cpio命令\n```\n利用cpio 可将文件或目录从文件库获取出来或将散列文件拷贝到文件库。cpio 的指令格式： \n        cpio –i[bcdmrtuv] [patterns] \n        cpio –o [abcv] \n        cpio –p [adlmuv][directory] \n\n说明：cpio 共有三种基本模式，-o即copy-out 模式，将一组文件copy到一个文件库，-i 即copy-in 模式，读取文件库，并将其展开在当前目录。-p 能从某个目录读取所有文件（包括子目录到另一个目录），且不以archive(归档)的方式存放。 \n    cpio 常配合shell使用。-o常用标准输入设备读取要copy 的文件名称，并将copy成的archive file 通过标准输出设备输出。一般利用输入/输出重定向或管道的原理，达到真正复制的功能。 \n\n（一）利用cpio备份： \n[例1] \n        $ find temo –print | cpio –ocdv \u003e /dev/rfd0135ds18 \n例: find . |cpio -co \u003e ../initrd\n\n将temp目录下的文件备份到软盘上。-o表示输出模式，-c生成一个带有头信息的文件。 -d表示按需要生成目录，-v表示命令执行时不断显示信息。用“\u003e”把结果定向到软盘。 \n[例2] \n        $ ls |cpio –o \u003e/usr/backup\n将当前目录下的所有文件复制成backup archive file \n[例3] \n        $ ls *.c | cpio –o\u003ebackup\n复制工作目录中的所有的c程序. \n[例4] \n        $ ls| cpio –p /usr/linfs/tempdir \n复制当前工作目录下的文件到/usr/linfs/tempdir 目录，不生成archive  file \n\n（二）利用cpio 复原： \n[例5] \n        $ cpio –icdv \u003c /dev/rfd0135ds18 \n例： cpio -i \u003c ../initrd将上层目录的initrd文件释放到当前文件夹\n\n将软盘中的文件复原。-i告诉cpio把文件作为它的输入，-d按需要生成目录，-v显示执行时的所有信息，-c具有头文件格式。 \n[例6] \n        $ cpio –icdv \"*stat.wp\" \u003c /dev/rfd0135ds18 \n仅复原*stat.wp的文件 \n[例7] \n        $ cpio –i \u003c/usr/linfs/old1 *.f \n仅复原*.f 文件\n\n序：\n(gzip 压缩文件)\ngzip 压缩文件对应的解压缩文件为gunzip。压缩后的文件名称为“.gz”。命令格式： \n        gzip  filename \n        gunzip  filename \n\n\n```\n\n## shell 计算\n```\nI. Intro\n \n1. A shell script contains commands that are executed as if you typed them in the terminal.\n \n2. We'll be using Vim for this tutorial\n \n\ta. Install Vim : sudo apt-get install vim\n\t\n\tb. Vim Commands\n\t\n\t\t1. i : insert mode\n \n\t\t2. \u003cESC\u003e : enter command mode\n\t\t\ti. w : Save / Don't Exit\n\t\t\tii. wq : Save / Quit\n\t\t\tiii. q! : Quit / Discard Changes\n\t\t\tiv. w : Move to front of next word\n\t\t\tv. b : Move backwards to front of word\n\t\t\tvi. 0 : Move to start of line\n\t\t\tvii. $ : Move to end of line\n\t\t\tviii. G : Jump to last line\n\t\t\n\t\t3. Move around with arrows\n\t\t\n\t\t4. :set number : Displays line numbers\n\t\t\n\t\t5. :syntax on : Syntax Highlighting\n\t\t\n\t\t6. :set tabstop=2 : Spaces in tab\n\t\t\n\t\t7. :set autoindent : Indent new lines\n\t\t\n\t\t8. Save these in your home/~/.vimrc file\n\t\t\n\t\t\ta. Find out what vimrc file you are using with this command in Vim :echo $MYVIMRC\n \n3. Hello World Script\n# The #! shebang tells the system the interpreter to use for the script\n#!/bin/bash\n# Comment\necho 'Hello World' # Print the string to the screen\n \n\ta. To make it executable chmod 755 hello_world\n\t\n\tb. Execute with ./hello_world\n\t\n\tc. The numbers after chmod define who can do what with the file\n\t\n\td. The numbers represent the Owner, the Group and Everyone else\n\t\n\te. What the numbers mean\n\t\n\t\t1. 7 : Read, Write \u0026 Execute\n\t\t2. 6 : Read \u0026 Write\n\t\t3. 5 : Read \u0026 Execute\n\t\t4. 4 : Read Only\n\t\t5. 3 : Write \u0026 Execute\n\t\t6. 2 : Write Only\n\t\t7. 1 : Execute Only\n\t\t8. 0 : None\n\t\t\n4. We define variables like this myName=\"Derek\"\n \n\ta. The variable name starts with a letter or _ and then can also contain numbers\n\t\n\tb. The shell treats all variables as strings\n\t\n\tc. When declaring a variable you can't have whitespace on either side of the =\n\t\n\td. \n\t#!/bin/bash\n\tdeclare -r NUM1=5 # Declare a constant\n\tnum2=4\n\t\n\t# Use arithmetic expansion for adding\n\tnum3=$((NUM1+num2))\n\tnum4=$((NUM1-num2))\n\tnum5=$((NUM1*num2))\n\tnum6=$((NUM1/num2))\n\t\n\t# Place variables in strings with $\n\techo \"5 + 4 = $num3\"\n\techo \"5 - 4 = $num4\"\n\techo \"5 * 4 = $num5\"\n\techo \"5 / 4 = $num6\"\n\techo $(( 5**2 ))\n\techo $(( 5%4 ))\n\t\n\t# Assignment operators allow for shorthand arithmetic \n\t# +=, -=, *=, /=\n\trand=5\n\tlet rand+=4\n\techo \"$rand\"\n\t\n\t# Shorthand increment and decrement\n\techo \"rand++ = $(( rand++ ))\"\n\techo \"++rand = $(( ++rand ))\"\n\techo \"rand-- = $(( rand-- ))\"\n\techo \"--rand = $(( --rand ))\"\n\t\n\t# Use Python to add floats\n\tnum7=1.2\n\tnum8=3.4\n\tnum9=$(python -c \"print $num7+$num8\")\n\techo $num9\n\t\n\t# You can print over multiple lines with a Here Script\n\t# cat prints a file or any string past to it\n\tcat \u003c\u003c END\n\tThis text\n\tprints on\n\tmany lines\n\tEND\n \n II. Functions\n \n \t1. You can use functions to avoid the need to write duplicate code\n \t\n \t2. Delete all code in Vim with gg then dG\n \t\n \t3. #!/bin/bash\n \t# Define function\n \tgetDate() {\n \t\t\n \t\t# Get current date and time\n \t\tdate\n \t\t\n \t\t# Return returns an exit status number between 0 - 255\n \t\treturn\n \t}\n \t\n \tgetDate\n \t\n \t# This is a global variable\n \tname=\"Derek\"\n \t\n \t# Local variable values aren't available outside of the function\n \tdemLocal() {\n \t\tlocal name=\"Paul\"\n \t\treturn\n \t}\n \t\n \tdemLocal\n \t\n \techo \"$name\"\n \t\n \t# A function that receives 2 values and prints a sum\n \tgetSum() {\n \t\n \t\t# Attributes are retrieved by referring to $1, $2, etc.\n \t\tlocal num3=$1\n \t\tlocal num4=$2\n \t\t\n \t\t# Sum values\n \t\tlocal sum=$((num3+num4))\n \t\t\n \t\t# Pass values back with echo\n \t\techo $sum\n \t}\n \t\n \tnum1=5\n \tnum2=6\n \t\n \t# You pass atributes by separating them with a space\n \t# Surround function call with $() to get the return value\n \tsum=$(getSum num1 num2)\n \techo \"The sum is $sum\"\n \t\nIII. Conditionals / Input \n \n\t1. \n\t#!/bin/bash\n\t\n\t# You can use read to receive input which is stored in name\n\t# The p option says that we want to prompt with a string\n  \tread -p \"What is your name? \" name\n  \techo \"Hello $name\"\n  \n  \tread -p \"How old are you? \" age\n  \t \n  \t# You place your condition with in []\n  \t# Include a space after [ and before ]\n  \t# Integer Comparisons: eq, ne, le, lt, ge, gt\n  \tif [ $age -ge 16 ]\n  \tthen\n  \t\techo \"You can drive\"\n  \t\n  \t# Check another condition\n  \telif [ $age -eq 15 ]\n  \tthen\n  \t\techo \"You can drive next year\"\n  \t\t\n  \t# Executed by default\n \telse\n \t  echo \"You can't drive\"\n \t  \n \t# Closes the if statement\n \tfi\n \t\n \t2. Extended integer test\n \t#!/bin/bash\n \t\n \tread -p \"Enter a number : \" num\n \t\n \tif ((num == 10)); then\n \t\techo \"Your number equals 10\"\n \tfi\n \t\n \tif ((num \u003e 10)); then\n \t\techo \"It is greater then 10\"\n \telse\n \t\techo \"It is less then 10\"\n \tfi\n \t\n \tif (( ((num % 2)) == 0 )); then\n \t\techo \" It is even\"\n \tfi\n \t\n \t# You can use logical operators like \u0026\u0026, || and !\n \tif (( ((num \u003e 0)) \u0026\u0026 ((num \u003c 11)) )); then\n \t\techo \"$num is between 1 and 10\"\n \tfi\n \t\n \t# \u0026\u0026 and || can be used as control structures\n \t\n \t# Create a file and then if that worked open it in Vim\n \ttouch samp_file \u0026\u0026 vim samp_file\n \t\n \t# If samp_dir doesn't exist make it\n \t[ -d samp_dir ] || mkdir samp_dir\n \t\n \t# Delete file rm samp_file\n \t# Delete directory rmdir samp_dir\n \n\t3. Testing strings\n\t#!/bin/bash\n\tstr1=\"\"\n\tstr2=\"Sad\"\n\tstr3=\"Happy\"\n\t\n\t# Test if a string is null\n\tif [ \"$str1\" ]; then\n\t\techo \"$str1 is not null\"\n\tfi\n\t\n\tif [ -z \"$str1\" ]; then\n\t\techo \"str1 has no value\"\n\tfi\n\t\n\t# Check for equality\n\tif [ \"$str2\" == \"$str3\" ]; then\n\t\techo \"$str2 equals $str3\"\n\telif [ \"$str2\" != \"$str3\" ]; then\n\t\techo \"$str2 is not equal to $str3\"\n\tfi\n\t\n\tif [ \"$str2\" \u003e \"$str3\" ]; then\n\t\techo \"$str2 is greater then $str3\"\n\telif [ \"$str2\" \u003c \"$str3\" ]; then\n\t\techo \"$str2 is less then $str3\"\n\tfi\n\t\n\t# Check the file test_file1 and test_file2\n\tfile1=\"./test_file1\"\n\tfile2=\"./test_file2\"\n\t\n\tif [ -e \"$file1\" ]; then\n\t\techo \"$file1 exists\"\n\t\t\n\t\tif [ -f \"$file1\" ]; then\n\t\t\techo \"$file1 is a normal file\"\n\t\tfi\n\t\t\n\t\tif [ -r \"$file1\" ]; then\n\t\t\techo \"$file1 is readable\"\n\t\tfi\n\t\t\n\t\tif [ -w \"$file1\" ]; then\n\t\t\techo \"$file1 is writable\"\n\t\tfi\n\t\t\n\t\tif [ -x \"$file1\" ]; then\n\t\t\techo \"$file1 is executable\"\n\t\tfi\n\t\t\n\t\tif [ -d \"$file1\" ]; then\n\t\t\techo \"$file1 is a directory\"\n\t\tfi\n\t\t\n\t\tif [ -L \"$file1\" ]; then\n\t\t\techo \"$file1 is a symbolic link\"\n\t\tfi\n\t\t\n\t\tif [ -p \"$file1\" ]; then\n\t\t\techo \"$file1 is a named pipe\"\n\t\tfi\n\t\t\n\t\tif [ -S \"$file1\" ]; then\n\t\t\techo \"$file1 is a network socket\"\n\t\tfi\n\t\t\n\t\tif [ -G \"$file1\" ]; then\n\t\t\techo \"$file1 is owned by the group\"\n\t\tfi\n\t\t\n\t\tif [ -O \"$file1\" ]; then\n\t\t\techo \"$file1 is owned by the userid\"\n\t\tfi\n\t\t\n\tfi\n\t\n\t4. With extended test [[ ]] you can use Regular Expressions\n\t#!/bin/bash\n\t\n\tread -p \"Validate Date : \" date\n\t\n\tpat=\"^[0-9]{8}$\"\n\t\n\tif [[ $date =~ $pat ]]; then\n\t\techo \"$date is valid\"\n\telse\n\t\techo \"$date is not valid\"\n\tfi\n\t\n\t5. # Read multiple values\n\t#!/bin/bash\n\t\n\tread -p \"Enter 2 Numbers to Sum : \" num1 num2\n\t\n\tsum=$((num1+num2))\n\t\n\techo \"$num1 + $num2 = $sum\"\n\t\n\t# Hide the input with the s code\n\tread -sp \"Enter the Secret Code\" secret\n\t\n\tif [ \"$secret\" == \"password\" ]; then\n\t\techo \"Enter\"\n\telse\n\t\techo \"Wrong Password\"\n\tfi\n\t\n\t6. You can set what separates the values with IFS\n\t#!/bin/bash\n\t\n\t# Store the original value of IFS\n\tOIFS=\"$IFS\"\n\t\n\t# Set what separates the input values\n\tIFS=\",\"\n\t\n\tread -p \"Enter 2 numbers to add separated by a comma\" num1 num2\n\t\n\t# Use the parameter expansion ${} to substitute any whitespace\n\t# with nothing\n\tnum1=${num1//[[:blank:]]/}\n\tnum2=${num2//[[:blank:]]/}\n \n\tsum=$((num1+num2))\n\t\n\techo \"$num1 + $num2 = $sum\"\n\t\n\t# Reset IFS to the original value\n\tIFS=\"$OIFS\"\n\t\n\t# Parameter expansion allows you to do this\n\tname=\"Derek\"\n\techo \"${name}'s Toy\"\n\t\n\t# The search and replace allows this\n\tsamp_string=\"The dog climbed the tree\"\n\techo \"${samp_string//dog/cat}\"\n\t\n\t# You can assign a default value if it doesn't exist\n\techo \"I am ${name:-Derek}\"\n\t\n\t# This uses the default if it doesn't exist and assigns the value\n\t# to the variable\n\techo \"I am ${name:=Derek}\"\n\techo $name\n\t\n\t7. Use case to when it makes more sense then if\n\t#!/bin/bash\n\t\n\tread -p \"How old are you : \" age\n\t\n\t# Check the value of age\n\tcase $age in\n\t\n\t# Match numbers 0 - 4\n\t[0-4]) \n\t\techo \"To young for school\"\n\t\t;; # Stop checking further\n\t\t\n\t# Match only 5\n\t5)\n\t\techo \"Go to kindergarten\"\n\t\t;;\n\t\t\n\t# Check 6 - 18\n\t[6-9]|1[0-8])\n\t\tgrade=$((age-5))\n\t\techo \"Go to grade $grade\"\n\t\t;;\n\t\t\n\t# Default action\n\t*)\n\t\techo \"You are to old for school\"\n\t\t;;\n\tesac # End case\n\t\n\t8. Ternary Operator performs different actions based on a condition\n\t#!/bin/bash\n\tcan_vote=0\n\tage=18\n\t\n\t((age\u003e=18?(can_vote=1):(can_vote=0)))\n\techo \"Can Vote : $can_vote\"\n\t\n\t\nIV. Parameter Expansions and Strings\n \n\t1. Strings\n\t#!/bin/bash\n\t\n\trand_str=\"A random string\"\n\t\n\t# Get string length\n\techo \"String Length : ${#rand_str}\"\n\t\n\t# Get string slice starting at index (0 index)\n\techo \"${rand_str:2}\"\n\t\n\t# Get string with starting and ending index\n\techo \"${rand_str:2:7}\"\n\t\n\t# Return whats left after A\n\techo \"${rand_str#*A }\"\n \nV. Looping\n \n\t1. While Loop\n\t#!/bin/bash\n\t\n\tnum=1\n\t\n\twhile [ $num -le 10 ]; do\n\t\techo $num\n\t\tnum=$((num + 1))\n\tdone\n\t\n\t2. Continue and Break\n\t#!/bin/bash\n\t\n\tnum=1\n\t\n\twhile [ $num -le 20 ]; do\n\t\n\t\t# Don't print evens\n\t\tif (( ((num % 2)) == 0 )); then\n \t\t\tnum=$((num + 1))\n \t\t\tcontinue\n \t\tfi\n \t\t\n \t\t# Jump out of the loop with break\n \t\tif ((num \u003e= 15)); then\n \t\t\tbreak\n \t\tfi\n \t\t\n\t\techo $num\n\t\tnum=$((num + 1))\n\tdone\n\t\n\t3. Until loops until the loop is true\n\t#!/bin/bash\n\t\n\tnum=1\n\t\n\tuntil [ $num -gt 10 ]; do\n\t\techo $num\n\t\tnum=$((num + 1))\n\tdone\n\t\n\t4. Use read and a loop to output file info\n\t#!/bin/bash\n  \twhile read avg rbis hrs; do\n  \t\n  \t\t# printf allows you to use \\n\n  \t\tprintf \"Avg: ${avg}\\nRBIs: ${rbis}\\nHRs: ${hrs}\\n\"\n  \t\t\n  \t# Pipe data into the while loop\n  \tdone \u003c barry_bonds.txt\n  \t\n  \t5. There are many for loop options. Here is the C form.\n  \t#!/bin/bash\n  \tfor (( i=0; i \u003c= 10; i=i+1 )); do\n  \t\techo $i\n  \tdone\n  \t\n  \t6. We can cycle through ranges\n  \t#!/bin/bash\n  \tfor i in {A..Z}; do\n  \t\techo $i\n  \tdone\n  \t\n  \t7.\n  \t\nVI. Arrays\n \n\t1. Bash arrays can only have one dimension and indexes start at 0\n\t\n\t2. Messing with arrays\n\t#!/bin/bash\n\t\n\t# Create an array\n\tfav_nums=(3.14 2.718 .57721 4.6692)\n\t\n\techo \"Pi : ${fav_nums[0]}\"\n\t\n\t# Add value to array\n\tfav_nums[4]=1.618\n\t\n\techo \"GR : ${fav_nums[4]}\"\n\t\n\t# Add group of values to array\n\tfav_nums+=(1 7)\n\t\n\t# Output all array values\n\tfor i in ${fav_nums[*]}; do\n\t\techo $i;\n\tdone\n\t\n\t# Output indexes\n\tfor i in ${!fav_nums[@]}; do\n\t\techo $i;\n\tdone\n\t\n\t# Get number of items in array\n\techo \"Array Length : ${#fav_nums[@]}\"\n\t\n\t# Get length of array element\n\techo \"Index 3 length : ${#fav_nums[3]}\"\n\t\n\t# Sort an array\n\tsorted_nums=($(for i in \"${fav_nums[@]}\"; do\n\t\techo $i;\n\tdone | sort))\n\t\n\tfor i in ${sorted_nums[*]}; do\n\t\techo $i;\n\tdone\n\t\n\t# Delete array element\n\tunset 'sorted_nums[1]'\n\t\n\t# Delete Array\n\tunset sorted_nums\n \n\t\nVII. Positional Parameters\n \n\t1. Positional parameters are variables that can store data on the command line in variable names 0 - 9\n\t\n\t\ta. $0 always contains the path to the executed script\n\t\t\n\t\tb. You can access names past 9 by using parameter expansion like this ${10}\n\t\t\n\t2. Add all numbers on the command line\n\t#!/bin/bash\n\t\n\t# Print the first argument\n\techo \"1st Argument : $1\"\n\t\n\tsum=0\n\t\n\t# $# tells you the number of arguments\n\twhile [[ $# -gt 0 ]]; do\n\t\n\t\t# Get the first argument\n\t\tnum=$1\n\t\tsum=$((sum + num))\n\t\t\n\t\t# shift moves the value of $2 into $1 until none are left\n\t\t# The value of $# decrements as well\n\t\tshift\n\tdone\n\t\n\techo \"Sum : $sum\"\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fheidsoft%2Fheidsoft-shell","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fheidsoft%2Fheidsoft-shell","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fheidsoft%2Fheidsoft-shell/lists"}