LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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 06-25-2013, 06:47 AM   #1
RudraB
Member
 
Registered: Mar 2007
Distribution: Fedora
Posts: 264

Rep: Reputation: 23
$variable in shell script


Dear friends,
I am trying to write a pbs via a shell script as

Code:
 cat >Hutespot.pbs<<EOF
  #!/bin/bash
  #PBS -l nodes=64:ppn=1,pvmem=1900mb,walltime=1:00:00
  #PBS -V
 
  cd $PBS_O_WORKDIR
 
 
  mpirun ~/bin/kkr.run ${job}.inp >> ${job}.out
 EOF
Now, where, cd $PBS_O_WORKDIR should not be expanded to its value, I mean, in the working Hutespot.pbs, I should ha "cd $PBS_O_WORKDIR" as it is.
instead, while I am running this, as expected, I am having

Code:
  1  #!/bin/bash
  2  #PBS -l nodes=64:ppn=1,pvmem=1900mb,walltime=1:00:00
  3  #PBS -V
  4
  5  cd
  6
  7
  8  mpirun ~/bin/kkr.run input.inp >> input.out
instead of
Code:
 1  #!/bin/bash
  2  #PBS -l nodes=64:ppn=1,pvmem=1900mb,walltime=1:00:00
  3  #PBS -V
  4
  5  cd $PBS_O_WORKDIR
  6
  7
  8  mpirun ~/bin/kkr.run input.inp >> input.out
any help please?
 
Old 06-25-2013, 07:23 AM   #2
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,862
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
You are trying to say that you have a directory called $PBS_O_WORKDIR?
Unusual, but not illegal. There are a few possibilities, here is the simplest:
Code:
#!/bin/sh
...
export PBS_O_WORKDIR='$PBS_O_WORKDIR'
...
cd $PBS_O_WORKDIR
...
 
Old 06-25-2013, 08:12 AM   #3
RudraB
Member
 
Registered: Mar 2007
Distribution: Fedora
Posts: 264

Original Poster
Rep: Reputation: 23
Quote:
Originally Posted by NevemTeve View Post
You are trying to say that you have a directory called $PBS_O_WORKDIR?
No, that file itself is a script, and take $PBS_O_WORKDIR from env.
I have managed it with escaping $ :
Code:
cd \$PBS_O_WORKDIR
 
Old 06-25-2013, 08:55 AM   #4
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,862
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
If this command works, then that "file" is actually a directory, and it's name is $PBS_O_WORKDIR.
 
Old 06-28-2013, 04:48 AM   #5
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
The proper way to keep a here document from expanding variables and command substitutions is simply to quote the opening string.

Also, if you use "<<-", then you can safely indent the content, and even the closing string if you want to, with tabs (and only tabs). They will be removed upon evaluation and won't appear in the final output.

Code:
#the heredoc contents are tab indented
cat >Hutespot.pbs <<-'EOF'
	#!/bin/bash
	#PBS -l nodes=64:ppn=1,pvmem=1900mb,walltime=1:00:00
	#PBS -V
 
	cd "$PBS_O_WORKDIR"
 
	mpirun "$HOME/bin/kkr.run" "$job.inp" >> "$job.out"
EOF
Of course, this will disable all variable expansions, so if you need some of them to expand and some not to, then you'll have to escape them individually, as you've already discovered.

It's more appropriate to use $HOME in scripts than "~". The latter is a kind of convenience alias designed more for interactive use, and is more limited than the preset shell variable.

QUOTE ALL OF YOUR VARIABLE EXPANSIONS. Note that the quotes inside the heredoc will not be directly evaluated, but will be passed literally to the target location, to be properly evaluated when the script is actually executed.


Finally, and this is just a bit of stylistic advice, I also recommend dropping the brackets around your variables. The full "${var}" form is usually unneeded (with some exceptions), and does nothing but add extra typing and clutter to your code, which can lead to an increased chance of errors. Take it from someone who's been there, you're better off without them.
 
  


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
Shell script variable question DemonWav Programming 3 06-01-2012 11:56 PM
pass variable from one shell script into another shell script xskycamefalling Programming 9 10-03-2009 01:45 AM
What is the best way to get a variable from another server with a shell script? abefroman Programming 2 01-29-2009 03:34 PM
Shell script --cannot assign variable-- ralvez Programming 6 02-24-2006 04:56 PM
expanding variable in shell script dipenchaudhary Programming 8 02-08-2006 05:05 PM

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

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