jeudi 30 avril 2015

If statement syntax error (BASH)

I'm trying to get the disk usage of some sites on a server using the script below, but I'm getting this error whenever I try to run it:

args.sh: line 50: syntax error near unexpected token `fi'
args.sh: line 50: `fi'

I can't see any syntax errors, but I'm obviously doing something wrong. Can anyone tell me what's going on?

Here's my code:

#!/bin/bash

prefix="/var/www/html/"
suffix=".example.com"
path="$prefix$1$suffix"
args_length="$#"
paths_array=[]
args_array=("$@")

# If there is one argument, calculate disk usage of given site.
if [ args_length -eq 1];
    then echo "Calculating disk usage..."
    output=$(du -sh $path)
    echo "This site is currently using $output"
    exit 1

# If there are no arguments, calculate disk usage of all sites.
elif [ args_length -lt 1];
    then echo "Calculating disk usage for all sites..."
    # I haven't done this part yet!
    exit 1

# If there is more than one site, calculate their disk usage    
elif [ args_length > 1];
    then echo "Calculating disk usage for selected sites..."    
    #Save arguments to sites_array
    for x in args_array; do
        paths_array[x] = args_array[x]
    done
    #Loop through array, creating paths.
    for i in paths_array; do
        site = paths_array[i]
        paths_array[i] = "$prefix$site$suffix"
    done
    #Print out disk usage for each path in array.
    for y in paths_array; do
        output = $(du -sh $paths_array[y]) 
        echo "This site is currently using $output"
fi

Side Note: For the section that I haven't written yet, can anyone tell me how I should go about saving the names of all the folders in the current working directory to an array? I've heard that parsing the output of 'ls' is a bad idea, so I'm looking for an alternative to that if anyone has one.

Aucun commentaire:

Enregistrer un commentaire