{"id":23159073,"url":"https://github.com/classfunc/learn_bash","last_synced_at":"2025-04-04T18:41:54.867Z","repository":{"id":121878699,"uuid":"380751387","full_name":"ClassFunc/learn_bash","owner":"ClassFunc","description":null,"archived":false,"fork":false,"pushed_at":"2021-06-27T15:17:25.000Z","size":4,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-02-10T03:47:35.278Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"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/ClassFunc.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,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2021-06-27T13:47:53.000Z","updated_at":"2021-06-29T04:12:27.000Z","dependencies_parsed_at":null,"dependency_job_id":"e1668801-3edc-43c0-9225-426f9b04af31","html_url":"https://github.com/ClassFunc/learn_bash","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ClassFunc%2Flearn_bash","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ClassFunc%2Flearn_bash/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ClassFunc%2Flearn_bash/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ClassFunc%2Flearn_bash/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ClassFunc","download_url":"https://codeload.github.com/ClassFunc/learn_bash/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247234844,"owners_count":20905852,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","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":"2024-12-17T22:28:56.052Z","updated_at":"2025-04-04T18:41:54.844Z","avatar_url":"https://github.com/ClassFunc.png","language":"Shell","readme":"# 25 bash script example\n\n### 1. Hello bash script\n\n```shell\n: '\nCách viết nhiều dòng comment.\nCách viết nhiều dòng comment.\nCách viết nhiều dòng comment.\n'\n```\n\n### 2. Khai báo biến\n\n```shell\nNAME=\"Tuan\"\nAGE=18\n```\n\n### 3. Ghi nội dung ra màn hình\n\n```shell\necho \"Tôi tên là $name, Năm nay tôi $AGE tuổi.\"\n```\n\n### 4. Lấy nội dung nhập vào của người dùng\n\n```shell\necho \"Bạn đang công việc gì ?\"\nread -r job\necho \"$job là một công việc tuyệt vời\"\n```\n\n### 5. Thực hiện tính toàn\n\n```shell\necho \"Nhập vào X:\"\nread -r X\necho \"Nhập vào Y\"\nread -r Y\n\n((sum=X+Y))\necho \"Sum: $sum\"\n```\n\n### 6. Điều kiện rẽ nhánh if, else if, else\n\n```shell\necho \"Nhập vào số may mắn của bạn\"\nread -r n\n\nif [ \"$n\" -eq 101 ]; then\n     echo \"Chúc mừng bạn đã đạt giải nhất\"\nelif [ \"$n\" -eq 510 ]; then\n     echo \"Bạn đạt giải nhì\"\nelif [ \"$n\" -eq 999 ]; then\n     echo \"Bạn đạt giải ba\"\nelse\n     echo \"Chúc bạn may mắn lần sau\"\nfi\n```\n\n### 7. Điều kiện rẽ nhánh case\n\n```shell\necho \"Nhập vào số may mắn của bạn\"\nread -r n\n\ncase $n in\n     101) echo echo \"Chúc mừng bạn đã đạt giải nhất\" ;;\n     510) echo \"Bạn đạt giải nhì\" ;;\n     999) echo \"Bạn đạt giải ba\" ;;\n     *) echo \"Chúc bạn may mắn lần sau\" ;;\nesac\n```\n\n### 8. Khởi tạo và gọi function\n\n```shell\nfunction F1() {\n     echo 'I like bash programming'\n}\n\nF1\n```\n\n### 9. Khởi tạo function nhận tham số đầu vào\n\n```shell\nDien_Tich() {\n     area=$(($1 * $2))\n     echo \"Diện Tích là : $area m2\"\n}\n\nDien_Tich 10 20\n```\n\n### 10. Khởi tạo function trả về giá trị\n\n```shell\nfunction greeting() {\n     str=\"Hello, $name\"\n     echo \"$str\"\n}\n\necho \"Enter your name\"\nread -r name\n\nval=$(greeting)\necho \"Return value of the function is $val\"\n```\n\n### 11. Vòng lặp while\n\n```shell\nvalid=true\ncount=1\nwhile [ $valid ]; do\n    echo $count\n\n    if [ $count -eq 5 ]; then\n        break\n    fi\n\n    ((count++))\ndone\n```\n\n### 12. Vòng lặp for\n\n```shell\nfor (( counter=10; counter\u003e0; counter-- )); do\n     echo -n \"$counter \"\ndone\nprintf \"\\n\"\n```\n\n### 13. Tạo thư mục\n\n```shell\necho \"Enter directory name\"\nread -r newdir\nmkdir \"$newdir\"\n```\n\n### 14. Kiểm tra sự tồn tại của thư mục\n\n```shell\necho \"Enter directory name\"\nread -r ndir\n\nif [ -d \"$ndir\" ]; then\n     echo \"Directory exist\"\nelse\n     mkdir \"$ndir\"\n     echo \"Directory created\"\nfi\n```\n\n### 15. Tạo file\n\n```shell\necho \"Enter file name\"\nread -r newfile\ntouch \"$newfile\"\n```\n\n### 16. Kiểm tra sự tồn tại của file\n\n```shell\nfilename=$1\nif [ -f \"$filename\" ]; then\n  echo \"File exists\"\nelse\n  echo \"File does not exist\"\nfi\n```\n\n### 17. Đọc nội dụng một file\n\n```shell\nfile='book.txt'\nwhile read line; do\n     echo \"$line\"\ndone \u003c $file\n```\n\n### 18. Thêm nội dung vào file\n\n```shell\necho \"Before appending the file\"\ncat book.txt\n\necho \"Learning Laravel 5\"\u003e\u003e book.txt\necho \"After appending the file\"\ncat book.txt\n```\n\n### 19. Xoá file\n\n```shell\necho \"Enter filename to remove\"\nread -r fn\nrm -i \"$fn\"\n```\n\n### 20. Truyền tham số cho file bash\n\nTạo file test.sh và lưu trữ đoạn code dưới vào file\n\n```shell\n#!/bin/bash\necho \"Total arguments : $#\"\necho \"1st Argument = $1\"\necho \"2nd argument = $2\"\n```\n\nterminal exec\n\n```shell\n$ bash test.sh Linux Hint\n```\n\n### 21. Truyền tham số cho file bash với tham số được gán tên\n\nTạo file test2.sh và lưu trữ đoạn code dưới vào file\n\n```shell\n#!/bin/bash\nfor arg in \"$@\"; do\n    index=$(echo $arg | cut -f1 -d=)\n    val=$(echo $arg | cut -f2 -d=)\n    case $index in\n        X) x=$val;;\n        Y) y=$val;;\n        *)\n    esac\ndone\n\n((result=x+y))\n\necho \"X+Y=$result\"\n```\n\nterminal exec\n\n```shell\n$ bash command_line.sh X=20 Y=50\n```\n\n### 22. Đọc biến môi trường từ file\n\n```shell\nFILE_ENV=\"./.env\"\nif [ -f \"$FILE_ENV\" ]; then\n     . \"$FILE_ENV\"\nfi\necho \"$ENV\"\n```\n\n### 23. Lấy thời gian\n\n```shell\nYear=$(date +%Y)\nMonth=$(date +%m)\nDay=$(date +%d)\nHour=$(date +%H)\nMinute=$(date +%M)\nSecond=$(date +%S)\necho $(date)\necho \"Current Date is: $Day-$Month-$Year\"\necho \"Current Time is: $Hour:$Minute:$Second\"\n```\n\n### 24. sleep\n\n```shell\necho \"Wait for 5 seconds\"\nsleep 5\necho \"Completed\"\n\n```\n\n### 25. send mail\n\n```shell\nRecipient=\"admin@example.com\"\nSubject=\"Greeting\"\nMessage=\"Welcome to our site\"\nmail -s $Subject $Recipient \u003c\u003c\u003c \"$Message\"\n```","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fclassfunc%2Flearn_bash","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fclassfunc%2Flearn_bash","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fclassfunc%2Flearn_bash/lists"}