I am receiving an error with this BASH script I am creating.
Here is the code:
Code:
#!/bin/bash
clear; #clear the screen
#usage function
function usage() {
echo '
No Options selected.
usage: sysinfo [-u -s -r]
-u User Information
-s System Information
-r Generate Report
'
}
function home_files() {
printf "contains: \t%s regular file(s), %s hidden\n" `ls -l $HOME | grep -E '^-' | wc -l` `ls -Al $HOME | awk ' /^-/{ print $8 }' | grep "^\." |wc -l` #display count of regular files not hidden and hidden
printf "\t\t%s directorie(s), %s hidden\n" `ls -l $HOME | grep -E '^d' | wc -l` `ls -Al $HOME | awk ' /^d/{ print $8 }' | grep "^\." |wc -l` #display count of directory files not hidden and hidden
printf "\t\t%s other type(s), %s hidden\n" `ls -Al $HOME | grep -E ^[^td-] | wc -l` `ls -Al $HOME | awk ' /^[^td-]/{ print $8 }' | grep "^\." |wc -l` #display count of any special files not hidden and hidden
}
#check if any arguments was giving
if [ $# -lt 1 ] ; then
usage;
exit 1;
fi
#display user information
function user_information() {
printf "****** User Information ********\n" # display user information header
printf "Username:\t%s\n" `whoami` # display current user name
printf "User ID:\t%s\n" `id -g` # display current user ID number
printf "size:\t\t%s\n" `du -sh $HOME | awk ' { print $1 } ' ` # display home directory size
printf "Home directory:\t%s\n" `echo $HOME` # display home directory
home_files #display a list of home file information
printf "Default shell:\t%s\n\n" `echo $SHELL` # display default shell
}
#display system information
function system_information() {
printf "******** System Information ********\n" # display system information header
printf "Hostname: \t\t %s\n" `hostname` # display host name
printf "Uptime: \t\t %s\n" `uptime | awk ' { print $3 } ' | tr , ' '` # display the time that the system been up and running
printf "Local filesystems: \t %s mounted\n" `mount | grep ^/dev | wc -l` # display local mounted hard drives
printf "Filesystem \t\t Mount Point \t\t Size \t\t Free Space\n" # display header for hard drive informations
df -h | grep ^/dev | awk ' { printf "%s \t\t %s \t\t\t %s \t\t %s\n", $1, $6, $2, $4 } ' # display hard drive information
free -m | grep -i ^mem | awk ' { printf "Memory: \t\t %sM Total \t %sM Free\n", $2, $4 } ' # display memory information
free -m | grep -i ^swap | awk ' { printf "Swap: \t\t\t %sM Total \t %sM Free\n\n", $2, $4 } ' # display swap memory information
}
#generate user and system information report
function generate_report() {
directory="$1" #holds directory argument giving to function
save_point="$directory/SYSINFO_REPORT"
if [ $directory -w ] #if the directory exist and is writable build report
then
echo "Creating report $save_point...." # Display creating report
user_information > "$save_point" # write user information to report
system_information >> "$save_point" # append system information to report
echo "Report Complete" # display report is complete
else
echo "Error: $directory does not exist or not writeable" # display error message is the directory does not exisit or is not writeable
#repeat loop if giving invalid directory or if the direction is not writeable
while ! [ $directory -w ]
do
echo "Enter in an existing directory to create a report" #ask user to enter in a existing directory
read "$directory" #read the directory from standard input from user
if [ "$directory" -w ] #check if it the directory giving is writeable and does exists if true generate the report
then
save_point="$directory/SYSINFO_REPORT"
echo "Creating report $save_point...." # Display creating report
user_information > "$save_point" # write user information to report
system_information >> "$save_point" # append system information to report
echo "Report Complete" # display report is complete
fi
done
fi
}
echo $1
#loop through arguments
while getopts "usr:" OPTION; do
case "$OPTION" in
u)
user_information
;;
s)
system_information
;;
r)
generate_report $OPTARG
;;
?)
usage;; #if a invalid agurment is giving display usage
esac
done
The problem is when I am trying to run the generate a report. The directory that I input does exist and is writeable. For some reason it is not linking the directory to the filename I want the report to be. I receive this error.
./sysinfo.sh: line 56: [: /home/codecaine/: unary operator expected
Error: /home/codecaine/ does not exist or not writeable
./sysinfo.sh: line 65: [: /home/codecaine/: unary operator expected
Enter in an existing directory to create a report
I know it is probably something simple! Thanks in advance
---------- Post updated at 09:42 PM ---------- Previous update was at 09:02 PM ----------
I figured it out. I had the -w backwards in the if statement. I also needed to ensure that I had quotes around my variable
Last edited by codecaine; 04-16-2011 at 11:31 PM..
Hi,
For my bash script, terminal with bash is generate an OK output and program works right.
already,
terminal with ash have "line 48: syntax error: Bad substitution" output and program don't work.
:confused: (0 Replies)
Hi All,
I need to pass a variable to perl script from bash script, where in perl i am using if condition. Here is the cmd what i am using in perl
FROM_DATE="06/05/2008"
TO_DATE="07/05/2008"
"perl -ne ' print if ( $_ >="$FROM_DATE" && $_ <= "$TO_DATE" ) ' filename"
filename has... (10 Replies)
hi,
i have a script that runs on bash and would like to run it on a machine that has csh and bash. the default setting on that machine is csh. i dont want to change my code to run it with a csh shell. is there any way i can run the script (written in bash) on this machine? in other words is there... (3 Replies)
Hey guys, i'm having trouble complete one of my bash scripts
I'm hoping to ---
1. Modify bash so that then the user types "ls" the command that is executed is "ls -al"
2. Modify the point of entry in bash when the user accesses it, moving the initial location to /var
I've somewhat done #2,... (9 Replies)
Hi everybody,
Lets say, I have two bash scripts named down.sh and up.sh located in two different folders named ~/home/a/ and ~/home/b/
Now I want to write another bash script, located in ~/home/ which runs these other two scripts, so that I only have to execute this one comprehensive script... (1 Reply)
# check host value regex='^(||1|2|25)(\.(||1|2|25)){3}$' if ')" != "" ]; then if ]; then echo host $host not found exit 4 fi elif ]; then echo $host is an invalid host address exit 5 fi
espeacailly the top regex part?
---------- Post updated at 06:58 PM ---------- Previous update was... (1 Reply)
I want to give my long scripts to customer. The customer must not be able to read the scripts even if he has the password. The following command locks and unlocks the script but the set +x is simply ignored.
The code:
read -p 'Script: ' S && C=$S.crypt H='eval "$((dd if=$0 bs=1 skip=//|gpg... (7 Replies)
So I'm trying to pass certain json elements as env vars and use them later on in a script.
Sample json:
JSON='{
"Element1": "file-123456",
"Element2": "Name, of, company written in, a very weird way",
"Element3": "path/to/some/file.txt",
}'
(part of the) script:
for s... (5 Replies)
I'm new to utilities like socat and netcat and I'm not clear if they will do what I need.
I have a "compileDeployStartWebServer.sh" script and a "StartBrowser.sh" script that are started by emacs/elisp at the same time in two different processes.
I'm using Cygwin bash on Windows 10.
My... (3 Replies)
In Bash shell - the ps -ef shows only the /bin/bash but the script name is not displayed ? Is there any way to get the script names for the process command ?
--- Post updated at 08:39 AM ---
in KSH (Korn Shell), my command output shows the script names but when run in the Bash Shell... (3 Replies)