Source .sh file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Source .sh file
# 1  
Old 10-06-2014
Source .sh file

Dear all,
For setting up my working environment, I need to source some files. What i usualy do is to do it manually,
Code:
cd /setup
source setup.sh

it take care of everything and I am able to run the commands like
Code:
root

.

As it was manual task every time, I defined this in .bashrc itself like
Code:
export ENV='/setup/setup.sh'

Now, I source the .bashrc and tried using the command, root for example, but it was failed to run.
Code:
$ root --l  
zsh: command not found: root

Any piece of suggestion would be great.

thanks,
emily
# 2  
Old 10-06-2014
Quote:
Originally Posted by emily
Code:
$ root --l  
zsh: command not found: root

You seem not to use bash or ksh at all, but in fact zsh. To use a script for one shell (.bashrc is for bash, as the name suggests) and let it run in another shell (zsh) may work - especially when the shells were built with compatibility in mind, like bash and Bourne Shell or Korn Shell and Bourne Shell - but doesn't have to at all.

Further, you use a command named "root" which is not a standard command i am aware of at all. Issue a

Code:
which root

and use "ls" and "file" to find out if it is a link, script, or whatever. Depending on your system and shell you might have to substitute "whence" or "whereis" for "which". For example:

Code:
# which blabla
/home/bakunin/bin/blabla

# ls -l /home/bakunin/bin/blabla
lrwxrwxrwx    1 bakunin  bakunin          22 Feb  5 2014  /home/bakunin/bin/blabla -> /usr/bin/ls

# file /usr/bin/ls
/usr/bin/ls: 64-bit XCOFF executable or object module

So the file "blabla" in my "$HOME/bin" directory is a link, linking to an executable program named "/usr/bin/ls".

I hope this helps.

bakunin


/PS:

Quote:
Originally Posted by emily
I defined this in .bashrc itself like
Code:
export ENV='/setup/setup.sh'

First, the "export" keyword and a variable declaration should not go on the same line:

Code:
ENV='/setup/setup.sh'
export ENV

Second: The content of the "ENV" variable tells the bash executable which configuration file to use at startup. When you execute ".bashrc" bash is already starting so this telling happens at a point in time when it is already useless.

You could place the "ENV=..."-line in your "~/.profile", but then your normal .bashrc would not be executed at all (instead "/setup/setup.sh" would) and this is probably not what you want. The correct way to to this is to first add the following lines to yor profile (per default "~/.profile"):

Code:
ENV="~/.bashrc"
export ENV

Then, you add the following line to the file "~/.bashrc":

Code:
. /setup/setup.sh

The leading "." (notice the space between it and the command) will do the same as the command "source" - it will execute every command of "/setup/setup.sh" in the current environment without opening a subshell, so every change to the environment this script makes stays when it exits.

Depending on what you want you can put this line to the end - then it will override every setting you did before if they coincide - or at the beginning. In the latter case it will provide a "base environment" which you alter for your personal purpose with the rest of your "~/.bashrc".

I hope this helps.

bakunin

Last edited by bakunin; 10-06-2014 at 07:14 AM..
# 3  
Old 10-06-2014
Quote:
Originally Posted by emily
Dear all,
For setting up my working environment, I need to source some files. What i usualy do is to do it manually,
Code:
cd /setup
source setup.sh

it take care of everything and I am able to run the commands like
Code:
root

.

As it was manual task every time, I defined this in .bashrc itself like
Code:
export ENV='/setup/setup.sh'

Now, I source the .bashrc and tried using the command, root for example, but it was failed to run.
Code:
$ root --l  
zsh: command not found: root

Any piece of suggestion would be great.

thanks,
emily
Hi Emily,

Not 100% sure but we can create a shell script as follows.

Code:
PATH=$PATH:/setup/setup.sh
setup.sh

This will not permanently set PATH environment in your system. To set it permanently you can edit your .profile.
You can try this in a non live(testing) environment.

Thanks,
R. Singh
# 4  
Old 10-06-2014
The PATH should consist of directories only, not commands nor scripts.
# 5  
Old 10-06-2014
Sorry after defining this in the .bashrc like this:
Code:
export ENV='/setup/setup.sh'

I used to execute it like this:
Code:
source $ENV
root -l


and it did not used to work. I am wondering if its not possible to source a script likethis?

thanks again,
emily
# 6  
Old 10-06-2014
Can you give us the result of:
Code:
echo $0

Because as Bakunin already mentionned you dont use source something to source with bash, read again post #2
# 7  
Old 10-06-2014
Code:
echo $0
-zsh

Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Compare the source definition file with data file

Hi How to compare the source definition file in unix with the data file . Please can you share me example if some one has done it before (3 Replies)
Discussion started by: Raj4fusion
3 Replies

2. UNIX for Dummies Questions & Answers

Match the amount fields in the source file vs trigger file

Hello, I have to write a script to compare the sum of the amount fields in a source file and the amount field in another file. details are: based on the name of the source file (say SALES as an example), a file already available in a path will be grabbed (say SALES_ParmFile) and this file... (4 Replies)
Discussion started by: vijaylak
4 Replies

3. Shell Programming and Scripting

Split File by Pattern with File Names in Source File... Awk?

Hi all, I'm pretty new to Shell scripting and I need some help to split a source text file into multiple files. The source has a row with pattern where the file needs to be split, and the pattern row also contains the file name of the destination for that specific piece. Here is an example: ... (2 Replies)
Discussion started by: cul8er
2 Replies

4. Programming

how to copy downloaded file into my source file folder (putty/unix)

I need to "Ensure that when you download libchat.a from the VLE you have copied it to the same folder on ius as your source files. You then refer to the library (and the libraries it needs) with: gcc -o outputfile sourcefile.c -L. -lchat -lsocket -lnsl" But I have no idea what this means! (I... (2 Replies)
Discussion started by: fakuse
2 Replies

5. Shell Programming and Scripting

rsync - update file on backup when file renamed on source

hi all, Please help me with rsync. I configured rsync to preserve timestamps using the -a option. When i renamed fileA to fileB on source machine I have to copies at the backup server. The aim is to keep the most recent file. fileA & fileB has same contents. When i renamed fileB to... (2 Replies)
Discussion started by: coolatt
2 Replies

6. Shell Programming and Scripting

extracting columns using pattern file from source file

Hi experts,Please help me for the below requirement. i have a source file.(lets say contains 50 columns). I am extarcting five columns from the source file by using pattern file. for example input file:--------a,b,c,d,"a,g","v b",s,koutputfile=======a,"a,g","v b",s,kThanks in advance subhendu (2 Replies)
Discussion started by: subhendu81
2 Replies

7. Shell Programming and Scripting

use of format file to extract columns from a source file

hi experts lets say my format file is B B ========= column no,name,type,length 1,ee,N,12 3,hj,N.4 4,kl,N,5 source file ======== d e f g h i 5 8 9 7 6 5 1 3 4 5 6 6 (2 Replies)
Discussion started by: subhendu81
2 Replies

8. Shell Programming and Scripting

Post Shell programming: Question about source a file and read data from the file

This is shell programming assignment. It needs to create a file called .std_dbrc contains STD_DBROOT=${HOME}/class/2031/Assgn3/STD_DB (which includes all my simple database files) and I am gonna use this .std_dbrc in my script file (read the data from the database files) like this: .... (3 Replies)
Discussion started by: ccwq
3 Replies

9. Shell Programming and Scripting

Get file from source server and delete the ftp file

Hi there Everyone, I need some help/information/advise for the following questions: I'm writing the following script: #Beginning of the script ftp -n <source server> > ${log_dir}/test_get.log << END user <user_name>@<IP Address> <passwd> verbose bin !echo "'List Files Before Getting... (3 Replies)
Discussion started by: lweegp
3 Replies
Login or Register to Ask a Question