Reading a file from a different directory in a Bash script


 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers Reading a file from a different directory in a Bash script
# 1  
Old 08-31-2017
Reading a file from a different directory in a Bash script

Hi all,

Given here under a section of a script I am using.

Code:
SIMDIR="/home/Ins/forces"
cd $SIMDIR

for file in `ls *.forces`

do 

basename=`echo $file | sed 's/\.[^.]*$//'`
extname=`echo $file | sed 's/[^0-9]*\([0-9]*\)\.\(.*\)/\2\1/'`

echo "Processing file: "$basename

python convert.py $basename.forces >  $extname.vtk

done

exit 0

As is, the convert.py is in the directory with files *.forces.

I want to read convert.py from a different location (directory) say my home directory i.e $HOME

Thank you for your help.
# 2  
Old 08-31-2017
??
Code:
python $HOME/convert.py $basename.forces >  $extname.vtk

Not sure I understood what you want...
This User Gave Thanks to vbe For This Post:
# 3  
Old 08-31-2017
Quote:
Originally Posted by vbe
??
Code:
python $HOME/convert.py $basename.forces >  $extname.vtk

Not sure I understood what you want...
Thanks VBE, It worked.
# 4  
Old 08-31-2017
Hello Theo Score,

You need not to mention python in front of python script to execute it if you have put shebang eg--> #!/usr/bin/env python in your python script, so it will know which complier to call.

Thanks,
R. Singh
# 5  
Old 08-31-2017
A simplification
Code:
for file in *.forces

Variables in command arguments should be in "quotes" to avoid potential problems with special characters, for example
Code:
extname=`echo "$file" | sed 's/[^0-9]*\([0-9]*\)\.\(.*\)/\2\1/'`

echo "Processing file: $basename"

And in the next line, go for
"$basename.forces" or "$basename".forces or "$file".
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Reading from file bash command

Hello, I have a file in the following format id sample platform R1 R2 gene1 gene2 gene3 1 abc llumina R1_001.fastq.gz R2_001.fastq.gz apoe prnpp asp 2 def llumina R1_001.fastq.gz R2_001.fastq.gz apoe prnpp 3 ghi llumina ... (3 Replies)
Discussion started by: nans
3 Replies

2. UNIX for Dummies Questions & Answers

' for reading (No such file or directory)

1.1 Solaris 10 8/07 s10s_u4wos_12b SPARC 1.2 Patch: 127714-03 Obsoletes: Requires: 120011-14 Incompatibles: Packages: SUNWsshcu, SUNWsshdu, SUNWsshu Patch: 128253-01 Obsoletes: Requires: Incompatibles: Packages: SUNWsshcu Patch: 126630-01 Obsoletes: Requires: Incompatibles: Packages: SUNWtcsh 1.3... (3 Replies)
Discussion started by: alvinoo
3 Replies

3. Shell Programming and Scripting

Reading a text file using bash

I've a file in linux with following text: ;ip address hostname put-location alt-put-location tftpserver 192.168.1.1 r01-lab1-net /mnt/nas1/fgbu/ /opt/fgbu/devicebackup 192.168.1.254Now I want to read these values and assign them to particular variables... (6 Replies)
Discussion started by: kashif.live
6 Replies

4. Shell Programming and Scripting

Reading command line options from bash script

I have the following code and I am calling it using ./raytrac.bash -u and getting problems. For some reason opt_usage is still 0. opt_usage=0 iarg=0 narg=$# while (($iarg < $narg)) do (( iarg = $iarg + 1 )) arg=$argv usrInputFlag=`echo $arg | awk '/=/ {print 1}; ! /=/... (22 Replies)
Discussion started by: kristinu
22 Replies

5. Shell Programming and Scripting

Help in reading a cv file in bash

Hi All, I am trying to read a .csv file which has some 6 columns. Eg: samp.csv one, two, three, four six, seven, eight, nine I used the following code, for line in `cat samp.csv` do echo "$line" done It displays every comma seperated values in each line like, one,... (1 Reply)
Discussion started by: johnwilliams.sp
1 Replies

6. Shell Programming and Scripting

Reading output from terminal back into bash script

How can I get a bash script to wait and read and count $i messages that a running program (drbl clonezilla) sends to the console (terminal) and only then move on to the next line in the script when the count is matched (the next line is the last line of the script and is a reboot)? The script... (0 Replies)
Discussion started by: dp123
0 Replies

7. Shell Programming and Scripting

Problem in reading file (bash)

i get a name from user first name : last name, in this format. Now i am saving this to a file. what i want is, I do not want to save any name if I already have one entry o that same name..what should i do for example user give robert fernandez this will save in file as robert:fernandez. if... (5 Replies)
Discussion started by: Learnerabc
5 Replies

8. UNIX for Dummies Questions & Answers

Reading files in script from another directory

Hi I'm trying to call my files from different directories in my script. Can you please help me. Here is my script: #!/bin/bash #---------------------------------------------------------------------------------------------------------------------- #This script allows the user... (1 Reply)
Discussion started by: ladyAnne
1 Replies

9. Shell Programming and Scripting

bash: reading filenames from file

Hi, I'm trying to write a script that reads filenames from a file and use these filenames in a loop. The filenames are all on one line and the problem is that these filenames have wildcards like * and braces like in them. Right now what I'm doing is something like this: echo "reading from... (0 Replies)
Discussion started by: warp17
0 Replies

10. UNIX for Dummies Questions & Answers

reading directory for most recent file?

Dear All, I'm trying to write a script that searches thru a directory looking for a most recent file and then scp that file. I have the scp working, but I don't know how to browse the directory and select the most recent file. The file name includes a date & time stamp (e.g.... (3 Replies)
Discussion started by: duncan_glover
3 Replies
Login or Register to Ask a Question