LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 10-23-2010, 06:11 PM   #1
Colorinb
LQ Newbie
 
Registered: Oct 2010
Posts: 8

Rep: Reputation: 0
Script problem after putting the file in .bashrc


Hi, I am newbie in Linux, I have a problem with my script. The problem is the system keeps rebooting after I put the directory file in .bashrc. The intentional for putting the file in .bashrc is to run the script automatically after login as root, I don know why is this happen. It was working fine for the first time without putting the file in .bashrc. I could break the loop after hitting the "ESC" key. would it be the script problems?


Here is my script

count=20
while [ $count -gt 0 ];do
sleep 1
echo Press ESC to break the operation
((count=count-1))

if [ $count = 0 ]
then reboot
fi

escape=$'x1b'
read -s -n1 -t1 KEY
if [ "$KEY" == "$escape" ]
then echo You have break the operation
break
else
continue
fi
done
 
Old 10-23-2010, 06:48 PM   #2
forrestt
Senior Member
 
Registered: Mar 2004
Location: Cary, NC, USA
Distribution: Fedora, Kubuntu, RedHat, CentOS, SuSe
Posts: 1,288

Rep: Reputation: 99
I'm guessing that you want:
in .bashrc
Code:
/root/reboot_query.sh
w/ the contents of /root/reboot_query.sh:
Code:
#!/bin/sh

# the below character is generated by hitting Ctrl-v and then Esc
escape=^[

# Read in the value for $KEY w/ a 20 sec timeout
read -s -n1 -t20 KEY
if [ "$KEY" == "$escape" ] ; then
        echo You canceled the operation
else
        echo reboot
fi
Remove the echo before the "reboot" when you are done testing.

HTH

Forrest
 
Old 10-23-2010, 08:32 PM   #3
devnull10
Member
 
Registered: Jan 2010
Location: Lancashire
Distribution: Slackware Stable
Posts: 548

Rep: Reputation: 116Reputation: 116
Might also be worth putting it in .loginrc rather than .bashrc as .bashrc will get read every time you launch a shell whereas .loginrc will only get called the once (unless you specifically request a login shell of course).
 
Old 10-23-2010, 08:36 PM   #4
forrestt
Senior Member
 
Registered: Mar 2004
Location: Cary, NC, USA
Distribution: Fedora, Kubuntu, RedHat, CentOS, SuSe
Posts: 1,288

Rep: Reputation: 99
Good point.
 
Old 10-25-2010, 01:07 PM   #5
Colorinb
LQ Newbie
 
Registered: Oct 2010
Posts: 8

Original Poster
Rep: Reputation: 0
May i know where is the .loginrc file located?

Thanks
 
Old 10-25-2010, 03:36 PM   #6
Colorinb
LQ Newbie
 
Registered: Oct 2010
Posts: 8

Original Poster
Rep: Reputation: 0
Hi, I found out the problem still going on, it was working fine when I didn't remove the word "echo" in front of reboot, I can break the loop after hitting the ESC button, the message "you canceled the operation" was shown and the "reboot" message was not shown because the loop was successfully break. But, after I have done testing and remove the word "echo" in front of reboot, after hitting the ESC button, the message "you canceled the operation" was shown, AND after few seconds, the system reboot itself. I supposed the system will not reboot because the operation was break. How could this happen? I've put the file in .bashrc .The system login as root.

The script:

#!/bin/sh
echo Please hit "ESC" to cancel the operation or system will reboot in 20s"
escape=^[
read -s -n1 -t20 KEY
if [ "$KEY" == "$escape" ] ; then
echo You canceled the operation
else
echo reboot
fi
 
Old 10-25-2010, 03:47 PM   #7
forrestt
Senior Member
 
Registered: Mar 2004
Location: Cary, NC, USA
Distribution: Fedora, Kubuntu, RedHat, CentOS, SuSe
Posts: 1,288

Rep: Reputation: 99
Yeah, that's the reason you need to put it in ~/.login (create that file if it doesn't exist). When you put it in .bashrc (and this is where I steered you wrong) it is launching a new shell when it calls the script. You are escaping out of that one, but the original one you are NOT escaping out of. Did you have two messages about the reboot before you removed the echo?

Forrest
 
Old 10-25-2010, 04:20 PM   #8
Colorinb
LQ Newbie
 
Registered: Oct 2010
Posts: 8

Original Poster
Rep: Reputation: 0
nope, only one message was shown. How do I create the .loginrc file? Should I put it in root? sorry for the inconvenience, I am totally new in Linux, thanks for the help!
 
Old 10-25-2010, 06:25 PM   #9
onebuck
Moderator
 
Registered: Jan 2005
Location: Midwest USA, Central Illinois
Distribution: SlackwareŽ
Posts: 12,541
Blog Entries: 23

Rep: Reputation: 1943Reputation: 1943Reputation: 1943Reputation: 1943Reputation: 1943Reputation: 1943Reputation: 1943Reputation: 1943Reputation: 1943Reputation: 1943Reputation: 1943
Hi,

Quote:
Originally Posted by Colorinb View Post
Hi, I found out the problem still going on, it was working fine when I didn't remove the word "echo" in front of reboot, I can break the loop after hitting the ESC button, the message "you canceled the operation" was shown and the "reboot" message was not shown because the loop was successfully break. But, after I have done testing and remove the word "echo" in front of reboot, after hitting the ESC button, the message "you canceled the operation" was shown, AND after few seconds, the system reboot itself. I supposed the system will not reboot because the operation was break. How could this happen? I've put the file in .bashrc .The system login as root.

The script:

#!/bin/sh
echo Please hit "ESC" to cancel the operation or system will reboot in 20s"
escape=^[
read -s -n1 -t20 KEY
if [ "$KEY" == "$escape" ] ; then
echo You canceled the operation
else
echo reboot
fi
Why??? You could source the file(your script) from ~.bashrc. What exactly are you trying to do?
 
Old 10-25-2010, 08:21 PM   #10
vipul_jain_in
LQ Newbie
 
Registered: Oct 2010
Posts: 3

Rep: Reputation: 0
You have to create deamon who can listen in background for ESC key,and automatically reboot the system after 20s.use xlib c interface to handle ESC key , and then convert it into deamon and then add executable into your script.
 
Old 10-25-2010, 08:43 PM   #11
tij
LQ Newbie
 
Registered: Oct 2010
Posts: 3

Rep: Reputation: 0
Quote:
How do I create the .loginrc file?
I'll assume you're trying to create this file in your home directory:

-Type "cd" into your terminal (without the quotes) and press enter.
-Create the .loginrc file with your text editor (I'll assume gedit) by typing "gedit .loginrc" (without the quotes)
-Write the desired contents of .loginrc in gedit
-Save and quit

The period before "loginrc" means that this file will be hidden so don't worry if you can't see it (you can change it with "gedit .loginrc")

I'm not sure of your specific intentions but I noticed that nobody responded to your question of "How do I create the .loginrc file?"
 
Old 10-26-2010, 05:54 PM   #12
Colorinb
LQ Newbie
 
Registered: Oct 2010
Posts: 8

Original Poster
Rep: Reputation: 0
Quote:
Why??? You could source the file(your script) from ~.bashrc. What exactly are you trying to do?
Yes, I could source the file from .bashrc ,but it will launch a new shell every time it calls the script. That's only allow me to escape the "new" launch shell but not the original one. The command should be "reboot" instead of "echo reboot". I suppose the original script was running in background?


Quote:
You have to create deamon who can listen in background for ESC key,and automatically reboot the system after 20s.use xlib c interface to handle ESC key , and then convert it into deamon and then add executable into your script.
Thanks for the information. Could you give more direction/information on how to create daemon?

Quote:
I'll assume you're trying to create this file in your home directory:

-Type "cd" into your terminal (without the quotes) and press enter.
-Create the .loginrc file with your text editor (I'll assume gedit) by typing "gedit .loginrc" (without the quotes)
-Write the desired contents of .loginrc in gedit
-Save and quit

The period before "loginrc" means that this file will be hidden so don't worry if you can't see it (you can change it with "gedit .loginrc")

I'm not sure of your specific intentions but I noticed that nobody responded to your question of "How do I create the .loginrc file?"
Thanks for the information, but what I need is the commands/contents in the .loginrc file
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Problem with root file .bashrc doraimom Debian 2 11-23-2008 09:43 AM
Putting awk in bash script the_imax Programming 9 05-12-2007 04:50 PM
Problem with bashrc file johnstv3 Red Hat 10 02-26-2004 03:09 AM
putting shell script accessible in all directory demmylls Linux - General 1 02-23-2004 11:37 AM
Putting Script Names In Shell Scripts sayble96 Linux - Newbie 4 12-17-2003 11:16 PM


All times are GMT -5. The time now is 06:28 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration