› Forums › Basic skills › Programming › Shell scripting (in bash) › All-in-one mini-tutorial
- This topic has 0 replies, 1 voice, and was last updated 4 years, 6 months ago by Simon.
Viewing 0 reply threads
-
AuthorPosts
-
-
January 24, 2020 at 11:17 #10626
Written by Jason Fong
#find out current location in disk (stands for "print working directory") pwd #list contents of current directory ls #list contents of current directory with more information, and in list format ls -l #make a directory mkdir new_dir #change into a directory cd new_dir #leave the directory cd .. #remove the directory rmdir new_dir #create blank file touch new_file #write contents to the file echo hello world >> new_file # >> does not overwrite contents of file, appends echo hello world2 >> new_file #print contents of file to terminal cat new_file #browse contents of file # q to quit # j,k to navigate up and down # u,d to navigate pages up and down less new_file #search for string in file grep 2 new_file #redirect output of a command to another new file grep 2 new_file > newer_file #overwrite contents of file echo bye world > new_file # > does overwrite contents of the file #create a bash file using a text editor touch test.sh #touch creates a file if one doesn't exist #check read (r), write (w) and execute (x) permissions of the test.sh file ls -al #type 'man ls' to examine what the flags 'a' and 'l' do #add a bash command to the file echo "echo 'hello world'" > test.sh # > redirects output to file #examine the contents of this file less test.sh #execute the file bash test.sh #add another line to the file echo "echo 'bye bye world'" >> test.sh # >> concatenates and doesn't overwrite #add executable permission to it chmod a+x test.sh #now we can execute this file using dot notation ./test.sh #add 'sha-bang' to the file so that it's interpreted as a bash script #use a text editor to open the file and add #!/bin/bash to the very top #or use "vim test.sh" or "nano test.sh" to edit it #how to create a variable var1='hello world' var2=helloworld var3=999 var4=$var3 #can assign a variable to the contents of another variable var5=${var4}appendedtext #can use curly brackets to clarify variable names #how to use a variable echo var1 is $var1 echo var2 is $var2 echo var3 is $var3 echo var4 is $var4 echo var5 is $var5 #for loops over files touch 1.txt 2.txt 3.txt #make a few dummy files for demonstration for filename in *; do #loop over files in current directory echo $filename #* is a wildcard that matches anything done for filename in *.txt; do #loop over files with given extension echo $filename done for filename in ../*; do #loop over files in the parent directory echo $filename #('..' is an alias for the parent directory) done #how to loop over numbers for ((i=0; i<5; i++)); do #c++ style for loop echo $i done for j in $(seq 5 10); do #use seq command echo $j done #how to call a script from within another script for ((i=0; i<5; i++)); do ./test.sh done #how to call a script with arguments touch test2.sh chmod a+x test2.sh echo "echo 'printing number' \$1 \$2" > test2.sh # $1 refers to first command line argument #note: we use \$ to escape the dollar sign for ((i=0; i<5; i++)); do ./test2.sh $i wow done #how to iterate over the lines contained within a text file touch test3.txt chmod a+x test3.txt echo 'line1' >> test3.txt echo 'line2' >> test3.txt echo 'line3' >> test3.txt echo 'line4' >> test3.txt echo 'line5' >> test3.txt for line in `cat test3.txt`; do #note: we are using `! not ' or "... echo $line done #be careful using single and double quotes #they have different behaviours when expanding variables echo 'var4 contains $var4' echo "var4 contains $var4" #if statement - check if two strings are equal boolvar=lol101 if [ $boolvar = lol101 ]; then #make sure to add the spaces around the square brackets echo 'yes' else echo 'no' fi if [ $boolvar = should_echo_no ]; then #make sure to add the spaces around the square brackets echo 'yes' else echo 'no' fi
-
-
AuthorPosts
Viewing 0 reply threads
- You must be logged in to reply to this topic.