-1

I have my script in /home/testing/program. My script name is test.sh. I want to run my script from other directory. I changed the permissions of the file. In my script I have redirected the output to some(output-test.sh) file. I need to run all my script from other directory. If i run the script in the current directory it works fine. Please help me to do this. Thanks in advance.

disha
  • 51
  • 1
  • 8

1 Answers1

0

In Unix systems, absolute paths start with / . So you can use absolute path to run a script. In your case all you have to do is run this command from the terminal sh /home/testing/program/test.sh . It does not matter from which directory you run the command from until you use the absolute path of the script.

Also the file in which the output is written should be defined with absolute path in script file. For example, in your case

/home/centos/rr/email-body.txt

so that the file is recognized by the OS even when the script is executed from the other directory or from any directory whatsoever.

daedalus_hamlet
  • 208
  • 1
  • 2
  • 8
  • In my test.sh file i have redirected the output to output-test.sh. If i run sh /home/testing/program/test.sh ... the test.sh is running fine and am getting permission denied error for output-test .... I am using Linux – disha Feb 08 '19 at 06:00
  • If you are comfortable with sharing the script here,do so. It helps us to help you better ! – daedalus_hamlet Feb 08 '19 at 06:02
  • Please help me...I am trying for 2 days – disha Feb 08 '19 at 06:11
  • I posted my code in the description – disha Feb 08 '19 at 06:17
  • post your test.sh script. It consists of the code to execute the output-test.sh . Maybe that is where the problem lies. – daedalus_hamlet Feb 08 '19 at 06:27
  • I posted my code in the description !!!! – disha Feb 08 '19 at 08:32
  • post both the script files. test.sh and output-test.sh – daedalus_hamlet Feb 08 '19 at 09:00
  • The output of some commands will be stored in output-test,sh...And the body of the mail will be the contents of output-test.sh – disha Feb 08 '19 at 09:14
  • There is a confusion. If you want to save the contents in a file,save it in output-test.txt not output-test.sh . I still do not understand when you receive the error.. While executing test.sh or output-test.sh ? – daedalus_hamlet Feb 08 '19 at 09:17
  • Sorry am saving the output in output-test.txt .... Sir when i run the script from the current directory am receiving the body of the mail from output-test.txt....But now i want to run my script from other directory .....How to run my test.sh script from other directory Please help me – disha Feb 08 '19 at 09:20
  • can you please say me the command how to run test.sh from other directory.... – disha Feb 08 '19 at 09:34
  • I did. That is the answer of this thread. – daedalus_hamlet Feb 08 '19 at 09:35
  • Sir i have posted my error page...Please have a look – disha Feb 08 '19 at 09:49
  • I cannot come to a conclusion of the cause of the error without taking a look at the line4 of v1.01.sh . But try this. `chmod +x /home/centos/rr/v1.01.sh` `./home/centos/rr/v1.01.sh` – daedalus_hamlet Feb 08 '19 at 09:55
  • cat output-test.txt | mail -s "output" "abc@gmail.com" This is the 4th line....Can u help me – disha Feb 08 '19 at 10:01
  • Fine. This is where the problem is, I guess. Change the file name i.e email-body.txt to /home/centos/rr/email-body.txt in the test.sh(line 4) provided that is where email-body.txt is. So it should be like `cat /home/centos/rr/output-test.txt | mail -s "output" "abc@gmail.com"` – daedalus_hamlet Feb 08 '19 at 10:26
  • thank u ... I got it – disha Feb 08 '19 at 10:47