LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 09-16-2012, 03:20 PM   #1
Radelix
LQ Newbie
 
Registered: Nov 2007
Distribution: Debian/backtrack/Slackware.
Posts: 15

Rep: Reputation: 0
script help, I'm teaching myself scripting


First off I am very new to shell scripting and I am looking for guidance.

I figured a good way to learn is to just write an automation script that presents a menu and allows me to pick a window manager to start. I have that part down. Since the machine I am using to learn is an older HP dv9720us it has some issues and the window manager doesn't always like to wake up properly. I want to add an option that would restart the existing window manager

My thinking is:

run ps aux and dump to a file
grep out the window manager name (gdm3, xfce, etc)
assign it to a variable
run /etc/init.d/window manager variable stop and start

so far I can't get the grep output to assign to the variable.

Also if this can't be done due to the way X works then that is fine too. I am just learning and I want to see what I can do.

Thanks,
Chris
 
Old 09-16-2012, 06:25 PM   #2
Ser Olmy
Senior Member
 
Registered: Jan 2012
Distribution: Slackware
Posts: 3,333

Rep: Reputation: Disabled
Quote:
Originally Posted by Radelix View Post
so far I can't get the grep output to assign to the variable.
This is probably the easiest way to accomplish that part of the problem:
Code:
var=$(ps aux | grep whatever)
You may want to filter the output from grep through awk or sed to remove unneeded information. If you remove all but the second column, you're left with just the PID.
 
Old 09-16-2012, 06:27 PM   #3
ted_chou12
Member
 
Registered: Aug 2010
Location: Zhongli, Taoyuan
Distribution: slackware, windows, debian (armv4l GNU/Linux)
Posts: 431
Blog Entries: 32

Rep: Reputation: 3
I recently found that xlsclients -a(l) is a pretty useful tool to get window programs.

Quote:
You may want to filter the output from grep through awk or sed to remove unneeded information. If you remove all but the second column, you're left with just the PID.
In that case it's:
Code:
var=$(ps aux | grep "watever" | awk '{print $2}')
To strictly follow your method of thinking:
Code:
FILE="shit.txt"
echo $(ps aux) > $FILE
var=$(cat "$FILE" | grep "watever" | awk '{print $2}')
echo "$var"
In both scripting (commandline is also equally important), a double quote ", doesn't seem too obvious at writing the script, but when running, vars with spaces can often cause unwanted troubles.
Ted

Last edited by ted_chou12; 09-16-2012 at 06:58 PM.
 
Old 09-16-2012, 06:41 PM   #4
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,771

Rep: Reputation: 2070Reputation: 2070Reputation: 2070Reputation: 2070Reputation: 2070Reputation: 2070Reputation: 2070Reputation: 2070Reputation: 2070Reputation: 2070Reputation: 2070
Quote:
Originally Posted by Ser Olmy View Post
Code:
var=$(ps aux | grep whatever)
You may want to filter the output from grep through awk or sed to remove unneeded information. If you remove all but the second column, you're left with just the PID.
pgrep is nicer if you're just after the PID (otherwise you need grep [w]hatever to avoid matching the grep itself).
 
Old 09-18-2012, 01:55 AM   #5
Radelix
LQ Newbie
 
Registered: Nov 2007
Distribution: Debian/backtrack/Slackware.
Posts: 15

Original Poster
Rep: Reputation: 0
Thanks for the help! I managed to solve my issue by doing this:

test=0

if [ $osch -eq 4 ] ; then
echo "restarting existing window manager"
echo $(ps aux) > dump
if [ $test -eq 0 ] ; then
test=$(cat dump | grep -o gdm3 | head -1)
else
test="my other bloody choice"
fi
rm -rf dump
/etc/init.d/$test stop
echo "Please wait while I restart $test"
/etc/init.d/$test start
closing fi's here and below


I figure there are better and simpler ways to do this but I just want to stretch my legs on scripting.
 
Old 09-18-2012, 07:12 AM   #6
Habitual
LQ Veteran
 
Registered: Jan 2011
Location: Abingdon, VA
Distribution: Catalina
Posts: 9,374
Blog Entries: 37

Rep: Reputation: Disabled
Quote:
Originally Posted by Radelix View Post
...I just want to stretch my legs on scripting.
and so you shall!
http://www.tldp.org/HOWTO/Bash-Prog-Intro-HOWTO.html
http://www.tldp.org/LDP/Bash-Beginne...tml/index.html
http://www.gnu.org/software/bash/man...ode/index.html
http://www.grymoire.com/Unix/Sh.html
http://www.tldp.org/LDP/abs/html/
http://mywiki.wooledge.org/BashFAQ
http://mywiki.wooledge.org/BashPitfalls
http://rute.2038bug.com/index.html.gz

Last edited by Habitual; 09-18-2012 at 10:53 AM.
 
  


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
[SOLVED] Bash scripting: Arguments to script evillan Programming 3 02-27-2011 04:00 AM
teaching shell scripting: cool scripting examples? fax8 Linux - General 1 04-20-2006 04:29 AM
Scripting audio_cd.script and video_dvd.script rolandjdc Mandriva 2 09-13-2004 08:23 AM
teaching active directory w/o teaching network concept (possible???) Tafta General 4 01-21-2004 06:12 PM
scripting the unix command script lmcthbe Linux - General 7 05-28-2003 02:49 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 10:25 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