{"id":19956594,"url":"https://github.com/abhinavkorpal/shell","last_synced_at":"2025-10-15T02:56:43.805Z","repository":{"id":130431561,"uuid":"97442243","full_name":"abhinavkorpal/Shell","owner":"abhinavkorpal","description":"A shell script is a computer program designed to be run by the Unix shell, a command-line interpreter.","archived":false,"fork":false,"pushed_at":"2019-04-04T20:09:25.000Z","size":173,"stargazers_count":1,"open_issues_count":0,"forks_count":1,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-01-12T07:10:14.940Z","etag":null,"topics":["programming-language","scripting-language","shell","shellscript"],"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/abhinavkorpal.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":"2017-07-17T06:27:06.000Z","updated_at":"2023-12-03T03:09:33.000Z","dependencies_parsed_at":null,"dependency_job_id":"f9b2eabc-420d-4c08-a170-95d369396cb2","html_url":"https://github.com/abhinavkorpal/Shell","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/abhinavkorpal%2FShell","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/abhinavkorpal%2FShell/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/abhinavkorpal%2FShell/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/abhinavkorpal%2FShell/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/abhinavkorpal","download_url":"https://codeload.github.com/abhinavkorpal/Shell/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":241389110,"owners_count":19955106,"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":["programming-language","scripting-language","shell","shellscript"],"created_at":"2024-11-13T01:34:56.281Z","updated_at":"2025-10-15T02:56:43.715Z","avatar_url":"https://github.com/abhinavkorpal.png","language":"Shell","funding_links":[],"categories":[],"sub_categories":[],"readme":"# ShellScript\nA shell script is a computer program designed to be run by the Unix shell, a command-line interpreter. The various dialects of shell scripts are considered to be scripting languages. Typical operations performed by shell scripts include file manipulation, program execution, and printing text. A script which sets up the environment, runs the program, and does any necessary cleanup, logging, etc. is called a wrapper.\n\nThe term is also used more generally to mean the automated mode of running an operating system shell; in specific operating systems they are called other things such as batch files (MSDos-Win95 stream, OS/2), command procedures (VMS), and shell scripts (Windows NT stream and third-party derivatives like 4NT—article is at cmd.exe), and mainframe operating systems are associated with a number of terms.\n\nThe typical Unix/Linux/Posix-compliant installation includes the Korn Shell (ksh) in several possible versions such as ksh88, Korn Shell '93 and others. The oldest shell still in common use is the Bourne shell (sh); Unix systems invariably also include the C Shell (csh), Bourne Again Shell (bash), a remote shell (rsh), a secure shell for SSL telnet connections (ssh), and a shell which is a main component of the Tcl/Tk installation usually called tclsh; wish is a GUI-based Tcl/Tk shell. The C and Tcl shells have syntax quite similar to that of said programming languages, and the Korn shells and Bash are developments of the Bourne shell, which is based on the ALGOL language with elements of a number of others added as well. On the other hand, the various shells plus tools like awk, sed, grep, and BASIC, Lisp, C and so forth contributed to the Perl programming language.\n\nOther shells available on a machine or available for download and/or purchase include ash, msh, ysh, zsh (a particularly common enhanced Korn Shell), the Tenex C Shell (tcsh), a Perl-like shell (psh) and others. Related programs such as shells based on Python, Ruby, C, Java, Perl, Pascal, Rexx \u0026c in various forms are also widely available. Another somewhat common shell is osh, whose manual page states it \"is an enhanced, backward-compatible port of the standard command interpreter from Sixth Edition UNIX.\"\n\nWindows-Unix interoperability software such as the MKS Toolkit, Cygwin, UWIN, Interix and others make the above shells and Unix programming available on Windows systems, providing functionality all the way down to signals and other inter-process communication, system calls and APIs. The Hamilton C Shell is a Windows shell that is very similar to the Unix C Shell. Microsoft distributes Windows Services for UNIX for use with its NT-based operating systems in particular, which have a Posix environmental subsystem.\n\n7. Loops for, while and until\n\nIn this section you'll find for, while and until loops.\n\nThe for loop is a little bit different from other programming languages. Basically, it let's you iterate over a series of 'words' within a string.\n\nThe while executes a piece of code if the control expression is true, and only stops when it is false (or a explicit break is found within the executed code.\n\nThe until loop is almost equal to the while loop, except that the code is executed while the control expression evaluates to false.\n\nIf you suspect that while and until are very similar you are right.\n\n7.1 For sample\n```shell\n         #!/bin/bash\n        for i in $( ls ); do\n            echo item: $i\n        done\n```\n```shell\noutput:\n         item: README.md\n```\nOn the second line, we declare i to be the variable that will take the different values contained in $( ls ).\n\nThe third line could be longer if needed, or there could be more lines before the done (4).\n\n'done' (4) indicates that the code that used the value of $i has finished and $i can take a new value.\n\nThis script has very little sense, but a more useful way to use the for loop would be to use it to match only certain files on the previous example\n\n7.2 C-like for\nfiesh suggested adding this form of looping. It's a for loop more similar to C/perl... for.\n```shell\n         #!/bin/bash\n        for i in `seq 1 10`;\n        do\n                echo $i\n        done    \n ```\n ```shell\n output:\n         1\n         2\n         3\n         4\n         5\n         6\n         7\n         8\n         9\n         10\n```\n7.3 While sample\n```shell\n          #!/bin/bash \n         COUNTER=0\n         while [  $COUNTER -lt 10 ]; do\n             echo The counter is $COUNTER\n             let COUNTER=COUNTER+1 \n         done\n```\n```shell\noutput:\n         The counter is 0\n         The counter is 1\n         The counter is 2\n         The counter is 3\n         The counter is 4\n         The counter is 5\n         The counter is 6\n         The counter is 7\n         The counter is 8\n         The counter is 9\n```\nThis script 'emulates' the well known (C, Pascal, perl, etc) 'for' structure\n\n7.4 Until sample\n```shell\n          #!/bin/bash \n         COUNTER=20\n         until [  $COUNTER -lt 10 ]; do\n             echo COUNTER $COUNTER\n             let COUNTER-=1\n         done\n```\n```shell\noutput:\n         COUNTER 20\n         COUNTER 19\n         COUNTER 18\n         COUNTER 17\n         COUNTER 16\n         COUNTER 15\n         COUNTER 14\n         COUNTER 13\n         COUNTER 12\n         COUNTER 11\n         COUNTER 10\n```\n8. Functions\n\nAs in almost any programming language, you can use functions to group pieces of code in a more logical way or practice the divine art of recursion.\n\nDeclaring a function is just a matter of writing function my_func { my_code }.\n\nCalling a function is just like calling another program, you just write its name.\n\n8.1 Functions sample\n```shell\n            #!/bin/bash \n           function quit {\n               exit\n           }\n           function hello {\n               echo Hello!\n           }\n           hello\n           quit\n           echo foo \n```    \nLines 2-4 contain the 'quit' function. Lines 5-7 contain the 'hello' function If you are not absolutely sure about what this script does, please try it!.\n\nNotice that a functions don't need to be declared in any specific order.\n\nWhen running the script you'll notice that first: the function 'hello' is called, second the 'quit' function, and the program never reaches line 10.\n\n8.2 Functions with parameters sample\n```shell\n                 #!/bin/bash \n                function quit {\n                   exit\n                }  \n                function e {\n                    echo $1 \n                }  \n                e Hello\n                e World\n                quit\n                echo foo \n```\n           \nThis script is almost identically to the previous one. The main difference is the funcion 'e'. This function, prints the first argument it receives. Arguments, within funtions, are treated in the same manner as arguments given to the script.\n\n9. User interfaces\n\n9.1 Using select to make simple menus\n```shell\n            #!/bin/bash\n           OPTIONS=\"Hello Quit\"\n           select opt in $OPTIONS; do\n               if [ \"$opt\" = \"Quit\" ]; then\n                echo done\n                exit\n               elif [ \"$opt\" = \"Hello\" ]; then\n                echo Hello World\n               else\n                clear\n                echo bad option\n               fi\n           done\n ```\nIf you run this script you'll see that it is a programmer's dream for text based menus. You'll probably notice that it's very similar to the 'for' construction, only rather than looping for each 'word' in $OPTIONS, it prompts the user.\n\n9.2 Using the command line\n```shell\n           #!/bin/bash        \n          if [ -z \"$1\" ]; then \n              echo usage: $0 directory\n              exit\n          fi\n          SRCD=$1\n          TGTD=\"/var/backups/\"\n          OF=home-$(date +%Y%m%d).tgz\n          tar -cZf $TGTD$OF $SRCD\n```  \nWhat this script does should be clear to you. The expression in the first conditional tests if the program has received an argument ($1) and quits if it didn't, showing the user a little usage message. The rest of the script should be clear at this point.\n\n10. Misc\n10.1 Reading user input with read\nIn many ocations you may want to prompt the user for some input, and there are several ways to achive this. This is one of those ways. As a variant, you can get multiple values with read, this example may clarify this.\n```shell\n#!/bin/bash\necho Please, enter your name\nread NAME \necho \"Hi $NAME!\"\n\necho Please, enter your firstname and lastname\nread FN LN \necho \"Hi $FN $LN\"\n```\n\n```shell\noutput:\n         Please, enter your name\n         abhinav\n         Hi abhinav!\n         Please, enter your firstname and lastname\n         abhinav korpal\n         Hi abhinav korpal\n```\n10.4 Getting the return value of a program\nIn bash, the return value of a program is stored in a special variable called $?.\n\nThis illustrates how to capture the return value of a program, I assume that the directory dada does not exist. (This was also suggested by mike)\n```shell\n         #!/bin/bash\n        cd /dada \u0026\u003e /dev/null\n        echo rv: $?\n        cd $(pwd) \u0026\u003e /dev/null\n        echo rv: $?\n```\n```shell\noutput:\n         rv: 1\n         rv: 0\n```\n10.5 Capturing a commands output\nThis little scripts show all tables from all databases (assuming you got MySQL installed). Also, consider changing the 'mysql' command to use a valid username and password.\n```shell\n         #!/bin/bash\n        DBS=`mysql -uroot  -e\"show databases\"`\n        for b in $DBS ;\n        do\n                mysql -uroot -e\"show tables from $b\"\n        done\n```\n\n#### List of common exit codes for GNU/Linux\nExit Code\tDescription\n\n0\tSuccess\n\n1\tOperation not permitted\n\n2\tNo such file or directory\n\n3\tNo such process\n\n4\tInterrupted system call\n\n5\tInput/output error\n\n6\tNo such device or address\n\n7\tArgument list too long\n\n8\tExec format error\n\n9\tBad file descriptor\n\n10\tNo child processes\n\n11\tResource temporarily unavailable\n\n12\tCannot allocate memory\n\n13\tPermission denied\n\n14\tBad address\n\n15\tBlock device required\n\n16\tDevice or resource busy\n\n17\tFile exists\n\n18\tInvalid cross-device link\n\n19\tNo such device\n\n20\tNot a directory\n\n21\tIs a directory\n\n22\tInvalid argument\n\n23\tToo many open files in system\n\n24\tToo many open files\n\n25\tInappropriate ioctl for device\n\n26\tText file busy\n\n27\tFile too large\n\n28\tNo space left on device\n\n29\tIllegal seek\n\n30\tRead-only file system\n\n31\tToo many links\n\n32\tBroken pipe\n\n33\tNumerical argument out of domain\n\n34\tNumerical result out of range\n\n35\tResource deadlock avoided\n\n36\tFile name too long\n\n37\tNo locks available\n\n38\tFunction not implemented\n\n39\tDirectory not empty\n\n40\tToo many levels of symbolic links\n\n42\tNo message of desired type\n\n43\tIdentifier removed\n\n44\tChannel number out of range\n45\tLevel 2 not synchronized\n46\tLevel 3 halted\n47\tLevel 3 reset\n48\tLink number out of range\n49\tProtocol driver not attached\n50\tNo CSI structure available\n51\tLevel 2 halted\n52\tInvalid exchange\n53\tInvalid request descriptor\n54\tExchange full\n55\tNo anode\n56\tInvalid request code\n57\tInvalid slot\n59\tBad font file format\n60\tDevice not a stream\n61\tNo data available\n62\tTimer expired\n63\tOut of streams resources\n64\tMachine is not on the network\n65\tPackage not installed\n66\tObject is remote\n67\tLink has been severed\n68\tAdvertise error\n69\tSrmount error\n70\tCommunication error on send\n71\tProtocol error\n72\tMultihop attempted\n73\tRFS specific error\n74\tBad message\n75\tValue too large for defined data type\n76\tName not unique on network\n77\tFile descriptor in bad state\n78\tRemote address changed\n79\tCan not access a needed shared library\n80\tAccessing a corrupted shared library\n81\t.lib section in a.out corrupted\n82\tAttempting to link in too many shared libraries\n83\tCannot exec a shared library directly\n84\tInvalid or incomplete multibyte or wide character\n85\tInterrupted system call should be restarted\n86\tStreams pipe error\n87\tToo many users\n88\tSocket operation on non-socket\n89\tDestination address required\n90\tMessage too long\n91\tProtocol wrong type for socket\n92\tProtocol not available\n93\tProtocol not supported\n94\tSocket type not supported\n95\tOperation not supported\n96\tProtocol family not supported\n97\tAddress family not supported by protocol\n98\tAddress already in use\n99\tCannot assign requested address\n100\tNetwork is down\n101\tNetwork is unreachable\n102\tNetwork dropped connection on reset\n103\tSoftware caused connection abort\n104\tConnection reset by peer\n105\tNo buffer space available\n106\tTransport endpoint is already connected\n107\tTransport endpoint is not connected\n108\tCannot send after transport endpoint shutdown\n109\tToo many references\n110\tConnection timed out\n111\tConnection refused\n112\tHost is down\n113\tNo route to host\n114\tOperation already in progress\n115\tOperation now in progress\n116\tStale file handle\n117\tStructure needs cleaning\n118\tNot a XENIX named type file\n119\tNo XENIX semaphores available\n120\tIs a named type file\n121\tRemote I/O error\n122\tDisk quota exceeded\n123\tNo medium found\n125\tOperation canceled\n126\tRequired key not available\n127\tKey has expired\n128\tKey has been revoked\n129\tKey was rejected by service\n130\tOwner died\n131\tState not recoverable\n132\tOperation not possible due to RF-kill\n133\tMemory page has hardware error\n\n### Let's Echo\n\nA quick introduction to 'Echo' \nThis is the equivalent of common output commands in most programming language (print or puts statements).\n\nFor Example:\n\necho \"Greetings\"\nThis outputs just one word \"Greetings\" (without the quotation marks).\n\necho \"Greetings $USER, your current working directory is $PWD\"\nThis picks up the values of the environment variables  and  and displays something like:\n\nGreetings prashantb1984, your current working directory is /home/prashantb1984  \nThe above message, of course, will vary from system to system, depending on the setting of environment variables.\n\nRecommended Resource \nA quick but useful tutorial for bash newcomers is here. \n\n### More on Conditionals\n\nif statements in Bash are often used in four important ways:\n\n1. if...then...fi statements\n2. if...then...fi...else statements  \n3. if..elif..else..fi  \n4. if..then..else..if..then..fi..fi.. (Nested Conditionals)\n\nThe Recommended Resources section may give you a clearer idea of conditionals in Bash.\n\nRecommended Resources\n\nA quick but useful tutorial for Bash newcomers is here. \nHandling input is documented and explained quite well on this page. \nDifferent ways in which 'if' statements may be used in Bash are demonstrated here.\n\n\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fabhinavkorpal%2Fshell","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fabhinavkorpal%2Fshell","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fabhinavkorpal%2Fshell/lists"}