LinuxQuestions.org
View the Most Wanted LQ Wiki articles.
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 12-29-2009, 06:27 AM   #46
Agrouf
Senior Member
 
Registered: Sep 2005
Location: France
Distribution: LFS
Posts: 1,591

Rep: Reputation: 79

Quote:
Originally Posted by sumeet inani View Post
As i have said that even if I mention full address without using ~ or $HOME etc. then also wallpaper does not change in ubuntu 9.04 though it does in ubuntu 8.04.
So I think repo is right.
It's not the same as setting the $HOME variable. gconftool-2 uses $HOME.
Try that:
Code:
cat /etc/gconf/2/path | grep HOME
As you see, gconf does use your HOME to change your gconf keys. So, even if you use the full path and didn't set your HOME environment variable, then gconf can't set any value.

Here is what I suspect:
in 8.04, there is nothing in $HOME/.gconf.path and it does not matter if $HOME is not set. In 9.04, they decided to use $HOME/.gconf.path and it does matter now.
 
Old 12-29-2009, 09:36 AM   #47
sumeet inani
Member
 
Registered: Oct 2008
Posts: 882
Blog Entries: 26

Original Poster
Rep: Reputation: 47
Okay i added at beginning of script

env | sed "s/^/export /g" | sed "s/=/=\"/" | sed "s/$/\"/g" >/tmp/env.sh
chmod a+x /tmp/env.sh
/tmp/env.sh

then
wp_fn=$HOME/Pictures/$i.jpg

but still in cron the script does nothing (no erors to mail) while executing in terminal gives instant effect.what do you suggest ?

Last edited by sumeet inani; 12-29-2009 at 10:05 AM.
 
Old 12-29-2009, 06:39 PM   #48
DrLove73
Senior Member
 
Registered: Sep 2009
Location: Srbobran, Serbia
Distribution: CentOS 5.5 i386 & x86_64
Posts: 1,118
Blog Entries: 1

Rep: Reputation: 129Reputation: 129
Add some echo lines that will print variables (like env) to stdout. Those echo's will be passed to syslog so you can view the output.
 
Old 12-29-2009, 08:18 PM   #49
Agrouf
Senior Member
 
Registered: Sep 2005
Location: France
Distribution: LFS
Posts: 1,591

Rep: Reputation: 79
Quote:
Originally Posted by sumeet inani View Post
Okay i added at beginning of script

env | sed "s/^/export /g" | sed "s/=/=\"/" | sed "s/$/\"/g" >/tmp/env.sh
chmod a+x /tmp/env.sh
/tmp/env.sh

then
wp_fn=$HOME/Pictures/$i.jpg
This script should NOT be added at the beginning of your script. It should be executed in the terminal. It records your environment to be restored in your script.
Execute that in the terminal:
Code:
env | sed "s/^/export /g" | sed "s/=/=\"/" | sed "s/$/\"/g"  >/tmp/env.sh
chmod a+x /tmp/env.sh
This will create a file /tmp/env.sh which contains the instructions to restore your environment for the script to be run by cron.
THEN, in your script, you should execute /tmp/env.sh with a dot (.) like that:
Code:
. /tmp/env.sh
at the very start of your script.
Make sure /tmp/env.sh contains what it is expected to contain. It should be a list of variables. Check that HOME is one of them.

Last edited by Agrouf; 12-29-2009 at 08:23 PM.
 
1 members found this post helpful.
Old 12-30-2009, 08:03 AM   #50
sumeet inani
Member
 
Registered: Oct 2008
Posts: 882
Blog Entries: 26

Original Poster
Rep: Reputation: 47
it worked.
thank You agrouf.
after much toil you finally solved it.

But
(1)I added
export HOME="/home/lxuser" instead of ". /tmp/env.sh"
Then nothing happened.So we don't know if all environment variables(total 37) have to be initialized.
(2)I thought that to execute a script you have to mention full path of file ( if you are in some other folder ) & if you are in same folder then run ./filename.

Last edited by sumeet inani; 12-30-2009 at 08:14 AM.
 
Old 01-24-2010, 05:23 AM   #51
sumeet inani
Member
 
Registered: Oct 2008
Posts: 882
Blog Entries: 26

Original Poster
Rep: Reputation: 47
So the questions that remain to be answered are
(1)Why do we use ". /tmp/env.sh" to execute that file not "/tmp/env.sh"
(2)Also I was thinking how can we extract last line from a text input using a command?
 
Old 01-24-2010, 04:47 PM   #52
Agrouf
Senior Member
 
Registered: Sep 2005
Location: France
Distribution: LFS
Posts: 1,591

Rep: Reputation: 79
Well, when you set an environment variable, it lives in the current environment. The current environment is actually the script being executed. If you create a script foobar.sh that set foo to bar and print it like that:
Code:
foo=bar
echo $foo
in the life of the script, foo's value is bar. But as soon as you exit the script, foo is undefined, because it only lives in the script. Try it like that:
Code:
$ ./foobar.sh
bar
$ echo foo
$
foo is no more defined after the script. It isn't even defined in the scripts being called by foobar.sh. For that you have to export the variable like that:
Code:
export foo=bar
it means that foo lives inside the subscripts of foobar.sh, but not in the script calling foobar.sh
Now, when you use the dot (.) to launch a script, you are actually telling bash to include the script as if you typed the whole script in the current environment. It means that the variables defined by the script will be available after the script has exited (it does not really exit actually.
You can see it like that:
Code:
$ . ./foobar.sh
bar
$ echo $foo
bar
foobar.sh has been executed in the current environment instead of creating a child environment for itself.
I hope this clears things up.

About the script changing the wallpapers being called by cron, you have to know that cron does not setup any environment. Your profile is not called and therefore no environment variable is available from your profile. You have to set the environment yourself. Obviously, gconftool-2 uses some variables from your environment in Ubuntu 9.10, although I don't know which. You can find them with ltrace, or with the documentation if it is documented, or just by removing them from the script and see what happened (remove half of them until you find the one that is needed)

About your 2nd question, you can use the tail command to extract the last line of text from any command (cat is the command you need in your case)
Code:
cat file.txt | tail -1
This means show the content of file.txt (cat file.txt) and extract the last line (tail -1)

Last edited by Agrouf; 01-24-2010 at 04:49 PM.
 
1 members found this post helpful.
Old 01-25-2010, 01:11 AM   #53
sumeet inani
Member
 
Registered: Oct 2008
Posts: 882
Blog Entries: 26

Original Poster
Rep: Reputation: 47
Very nice explanation Agrouf.
I had decided to keep wallpaper files anywhere on system and just a file containing full path address at ~/path.txt.
I will try
Code:
if [ -e ~/Pictures/.int.txt ]; then
echo 0 > /dev/null
else
echo 1 > ~/Pictures/.int.txt
fi
var number=$(head -1  /home/lxuser/Pictures/.int.txt)
var bg=$(head  -$number /home/lxuser/path.txt | tail -1 )
where $number starts from 1 & incremented with every run.
I hope this will work.
 
Old 01-25-2010, 04:11 AM   #54
Agrouf
Senior Member
 
Registered: Sep 2005
Location: France
Distribution: LFS
Posts: 1,591

Rep: Reputation: 79
I think it should work. For the first if, you can use the ! (not) operator like that:
Code:
if [ ! -e /home/lxuser/Pictures/.int.txt ]; then
   echo 1 > /home/lxuser/Pictures/.int.txt
fi
or, you can use the && operator for a one-liner like that:
Code:
[ ! -e /home/lxuser/Pictures/.int.txt ] && echo 1 > /home/lxuser/Pictures/.int.txt
 
Old 01-25-2010, 05:01 AM   #55
sumeet inani
Member
 
Registered: Oct 2008
Posts: 882
Blog Entries: 26

Original Poster
Rep: Reputation: 47
I know c language.How can I use it to run commands like ls,cat,etc. inside c program & process their output.
Then i will be able to
$gcc C-Program.c
$./a.out
because I find this bash scripting very different.
 
Old 01-25-2010, 05:24 AM   #56
Agrouf
Senior Member
 
Registered: Sep 2005
Location: France
Distribution: LFS
Posts: 1,591

Rep: Reputation: 79
If you want to run a bash command from a C program, you can call the system() function from stdlib.h like that:
Code:
#include <stdlib.g>
int main(int argc, char *argv[]) { system("echo 1 > /home/lxuser/Pictures/.int.txt"); return 0; }
If you want to process the output of such command, you need to use the popen function from stdio.h:
Code:
#include <stdio.h>
int main(int argc, char *argv[]) {
   char buffer[1024];
   FILE *f=popen("ls", "r");
   while (fgets(buffer, 1024, f)) {
     printf("ls output = %s", buffer);
   }
   fclose(f);
   return 0;
}
 
1 members found this post helpful.
Old 01-29-2010, 05:09 AM   #57
sumeet inani
Member
 
Registered: Oct 2008
Posts: 882
Blog Entries: 26

Original Poster
Rep: Reputation: 47
I will try the popen & system command.
i have noted one thing
You have used && in one liner because it will evaluate second option only if first is true that is file does not exist.That is the difference from & operator.
 
Old 01-29-2010, 08:10 AM   #58
Agrouf
Senior Member
 
Registered: Sep 2005
Location: France
Distribution: LFS
Posts: 1,591

Rep: Reputation: 79
It's tricky in bash. the & operator is to execute commandd in parallel.
Code:
sleep 10 & echo "ok"
This will print "ok" at the same time as sleeping for 10 seconds, whereas
Code:
sleep 10 ; echo "ok"
Will sleep 10 seconds and print "ok" after the end of it.
Code:
sleep 10 && echo "ok"
Is the same thing, but it would not print "ok" is sleep 10 was not successful.
 
Old 03-06-2010, 09:18 AM   #59
sumeet inani
Member
 
Registered: Oct 2008
Posts: 882
Blog Entries: 26

Original Poster
Rep: Reputation: 47
I tried system & popen function and they are good.

(1)I have seen that in fresh install of ubuntu I cannot use gcc to compile programs until I install g++
 
  


Reply

Tags
software, wallpaper


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
Can't change wallpaper w/ openbox / how to change mousespeed johnnyxxxcakes Linux - Newbie 14 05-24-2009 08:11 PM
change wallpaper? matrixon Linux - Software 1 06-12-2006 04:20 AM
Wallpaper Won't change Rick069 Linux - Newbie 3 04-16-2006 02:03 PM
How to change wallpaper pacranch Amigo 9 06-27-2005 02:30 PM
change wallpaper TravisB Linux - General 4 10-23-2002 06:57 PM


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

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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration