Shell Scripting – Loops & Tests

Shell scripting: test scripts and for loops

Network administrator, pentester jobs includes lots of tasks that need to be performed on daily basis and its obvious that it is frustrating to perform the same bunch of tasks daily, but people are smart enough to make their jobs a little less boring by using some smart automation techniques such as shell scripting, yes there is python which can do a lot of automation, but it’s not a substitution of shell scripting, reason being the way shell scripting handles files is just amazing which you will realize in this shell scripting series, many times you need to know if  a specific file is present or not to perform a particular function, there is a shell commands called test which can be used to perform many conditional tasks.

What is test shell command?

The test command in Unix evaluates the expression parameter. In most recent shell implementations, it is a shell bulletin, even though the external version still exists. In the second form of the command, the [ ] (brackets) must be surrounded by blank spaces. This is because [ is a program and POXIS compatible shells require a space between the program name and its arguments. One must test explicitly for file names in the C shell. File-name substitution (globbing) causes the shell script to exit.

The test command is not to be confused with the [[ reserved word that was introduced with ksh88. The latter is not a command but part of the ksh88 syntax and does not apply the file-name substitution to glob expressions.

The following arguments are used to construct this parameter:

-e FileName – FileName exists

All remaining arguments return true if the object (file or string) exists, and the condition specified is true.

-b Filename – Returns a True exit value if the specified FileName exists and is a block special file

-c FileName – FileName is a character special file

-d FileName – FileName is a directory


-f FileName – FileName is a regular file

-g FileName – FileName’s Set Group ID bit is set

-h FileName – FileName is a symbolic link

-k FileName – FileName’s sticky bit is set

-L FileName – FileName is a symbolic link

-p FileName – FileName is a named pipe (FIFO)

-r FileName – FileName is readable by the current process

-s FileName – FileName has a size greater than 0

-t FileDescriptor – FileDescriptor is open and associated with a terminal

-u FileName – FileName’s Set User ID bit is set


Example test script:


If [ -d /usr/share/wordlist];


  echo “the directory does exist”


  echo “the directory doesn’t exist”


after giving required permissions, when you run the script, if the wordlists file exist then it will print “the directory does exist” otherwise it will print the statement after else.

For better understanding of test scripts, refer to the video above.

For loops in shell scripts

If you have done some programming in other languages then you would already know how useful for loops are and how it helps to do some repeated tasks, how the iteration in for loops can be used in different situations, for loop can be used in shell scripting for iterating through contents of a file, for printing out specific content in a file or directory, to understand it better lets take a look at an example

  • Just create a small txt file, ex names.txt containing some names
  • Create a new shell file and copy the following program in it



For NAMES in $(cat names.txt); do

   echo “the names are: $NAMES”


  • Run this shell file giving required permissions.
  • Output will be list of names in the names.txt file


You can refer to the video above to see all this steps and other information related to the command used, for loop will be used again and again in the coming videos, some regular use of it includes filtering Nmap scan report, picking important information from ping scan, netstat etc. so stay tuned and keep learning.

Liked it? Take a second to support Alexis on Patreon!
Share this post