If you, like so many other people these days, switch to Linux you will come in contact with something called unux scripts, shell scripts or bash scripts. These scripts are text-files that execute commands just like bat-files in Windows. I prefer shell-scripts because i think they are more powerful then bat-files but that is just my opinion.
Some things you should know: #!/bin/bash usually is the first line in a bash script. It is called shebang (also called a hashbang, hashpling, or pound bang) and specifies in which shell to execute the code that follows below.
Everything after a # is a comment and does not get executed.
Below you will find examples of a few things that you can do with shell scripts on Linux and other Unixes. If you have questions: have a look at the man page of the command or post a comment and I will try to explain.
#!/bin/bashMENU="
1 Date and Time
2 Calendar for current month
3 quit
"whiletrue; doclearecho"$MENU"echo-n"Please make your choice: "read INPUT # Read user input and assign it to variable INPUTcase$INPUTin1)dateecho press ENTER to continueread;;2)calecho press ENTER to continueread;;3|q|Q)# If user presses 3, q or Q we terminateexit0;;*)# All other user input results in an usage messageclearecho Please choose alternatves 1, 2 or 3sleep2;;esacdone
count
1
2
3
4
5
6
7
8
9
10
#!/bin/bashBEGIN=1# Start counting hereEND=10# Stop counting hereEND=`expr$END + 1`while[$END-ne$BEGIN]; do# While END is not equal to BEGIN do ...echo This is iteration $BEGINBEGIN=`expr$BEGIN + 1`# Increasing the value of BEGIN by onedone
for
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#!/bin/bash# Defining VariableSTAFF="Anders Bertil Cesar Denise Emilia Filippa"echo First LOOP
for PERSON in$STAFF; doecho$PERSON is a member of the staff.
doneecho Second LOOP
echo-n"The staff members are: "for PERSON in$STAFF; doecho-n"$PERSON "# -n instructs echo not to print the trailing newline chardoneecho
if
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#!/bin/bashVALIDUSER="Thorsten"if[$VALIDUSER = anders ]; thenecho$VALIDUSER is a valid user
exit0elif[$VALIDUSER = bertil ]; thenecho$VALIDUSER is a valid user
exit0elif[$VALIDUSER = Thorsten ]; thenecho$VALIDUSER is a valid user
exit0elseecho no valid user found
exit1fi
while
1
2
3
4
5
6
7
8
9
10
#!/bin/bashSTART=1# The value to begin withEND=10# When this value is reached we are doneSPEED=1# Number of seconds between each iterationwhile[$START-le$END]; doecho this is iteration $START of $ENDSTART=`expr$START + 1`sleep$SPEEDdone
User Input
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#!/bin/bash# Check if there is input to this scriptif[$#-lt1]; then# $# contains the total number of arguments to the scriptecho"Usage: $0 [argument1 ...]"# $0 is the executed commandexit1fiNUMPARAMETERS=`echo$#`# The number of parameters provded to this scriptecho"This script has $NUMPARAMETERS parameters"COUNT=1while[$COUNT-le$NUMPARAMETERS]; doecho Parameter $COUNT contains $1# $1 is the first argumentCOUNT=`expr$COUNT + 1`# Increasing COUNT by oneshift# Shifts input parameters. $2 becomes $1 and the old $1 is discardeddone