LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-08-2013, 04:53 PM   #1
ranito1980
LQ Newbie
 
Registered: Jul 2012
Posts: 4

Rep: Reputation: Disabled
I need help with my shutdown bash script, please help!


Hell everyone. I'm a noob to bash scripting. I'm running LinuxMint 14 (Nadia). I've successfully created a bash script for cleaning out my ram, firefox caches (using shred), updating the OS, etc and at the tail end of the script I'd like to call a script I call shut.sh. However I keep getting errors when I try to run this script. I'm brand new to bash scripting using if...else statements so please help me out. What am I doing wrong?
------
#!/bin/bash
clear
echo "Shutdown?"
read what
if ["$what" = "y"];
then
shutdown -h now
elif ["$what" = "r"];
then
shutdown -r now
else
echo "Terminating Script. Thanks!"
fi
 
Old 03-08-2013, 05:01 PM   #2
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,863
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
What errors are you getting? Not everyone here is mind-reader. (And please use [code] and [/code] tags, when you paste code.)

PS: add some spaces, eg:
Code:
...
 if [ "$what" = "y" ];
...

Last edited by NevemTeve; 03-08-2013 at 05:03 PM.
 
Old 03-09-2013, 02:24 AM   #3
kooru
Senior Member
 
Registered: Sep 2012
Posts: 1,385

Rep: Reputation: 275Reputation: 275Reputation: 275
You should write it into tag code and give us what error you get.
Anyway, as said by NevemTeve, you must insert some spaces for "if" syntax

Last edited by kooru; 03-09-2013 at 02:25 AM.
 
1 members found this post helpful.
Old 03-09-2013, 11:42 AM   #4
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
Please use ***[code][/code]*** tags around your code and data, to preserve the original formatting and to improve readability. Do not use quote tags, bolding, colors, "start/end" lines, or other creative techniques.


Processing of user input when using read and such is generally better done with a case statement.

And for simple multiple choice menus like this I'd suggest using a select loop.

Code:
#!/bin/bash

clear
PS3='What do you want to do?: '

select what in Shutdown Restart Abort; do

    echo "You selected: $what"

    case $REPLY in
        1) shutdown -h now ;;
        2) shutdown -r now ;;
        3) echo "Terminating script without action."
           break  ;;
        *) echo "invalid selection" ;;
    esac

done

exit 0
BTW, remember that shutdown requires that the user running the script have proper permission to do so.

Last edited by David the H.; 03-09-2013 at 11:47 AM. Reason: fix formatting error
 
Old 03-10-2013, 03:42 PM   #5
Xeratul
Senior Member
 
Registered: Jun 2006
Location: UNIX
Distribution: FreeBSD
Posts: 2,657

Rep: Reputation: 255Reputation: 255Reputation: 255
Wink

spaces might be needed.

here it goes:

Code:
#!/bin/bash
clear
echo "Shutdown?" ; read what

if [ -f /sbin/halt ] ; then
  echo "Notice: you might also make use of halt since this file exists"
  # sudo -l ;)
fi

if [ "$what" = "y" ] ; then
     shutdown -h now
elif [ "$what" = "r" ] ; then
     shutdown -r now
fi
echo "Terminating Script. Thanks!"
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Start-Up Script & Shutdown/Kill Script needed guggilamsandeep Red Hat 1 05-11-2011 08:58 AM
SSH connection from BASH script stops further BASH script commands tardis1 Linux - Newbie 3 12-06-2010 08:56 AM
[SOLVED] Using a long Bash command including single quotes and pipes in a Bash script antcore Linux - General 9 07-22-2009 11:10 AM
Bash?? System Restart (shutdown -r now) woranl Linux - Newbie 4 04-13-2004 09:33 PM
bash: shutdown: command not found Mimicafe Linux - General 7 02-23-2003 02:44 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:52 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration