Difficulty with CAT redirection in script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Difficulty with CAT redirection in script
# 15  
Old 01-24-2017
Quote:
Originally Posted by Don Cragun
I'm glad we worked it out. Sometimes the obvious possibility just seems to unlikely to ask about. Smilie
The 2nd set of eyes is a valuable programming tool. :-) Thanks again for you help Don. Hopefully I won't pull too many more bonehead moves like that again. :-)
# 16  
Old 01-25-2017
Quote:
Originally Posted by demmith
Oh my God, I am such an idiot! LOL!
Don't worry, it happens to the best of us.

As a general rule: do not change directories inside a script at all (for exactly this reason, to avoid this confusion). Whenever you work on files make sure you always use absolute pathes like this:

Code:
# cat wrong.sh

cd /some/where
command > ./output.file

# cat correct.sh

outdir="/some/where"
command > "${outdir}/output.file"

The same goes for all other filenames. This way your script will work regardless of where you started it. If you work on several files using a variable for the directory part ensures they all land in the same place. The absolute worst you can do, though, is to use relative pathes:


Code:
# cat worst-of-worst.sh

command1 > outfile1
cd ..
command2 > outfile2

If you ever find that in anyones code: have them promise never to write any shell script again. This is a surefire recipe for disaster because the script will (maybe) work if you call it from one directory and fail if you call it from another.

In general the script you call inherits the environment from its calling process - the command shell you used to call it. "Envrionent" means not only the values for variables (all that have been "export"ed before) like PATH, TZ (timezone), LANG, etc.. but also the current directory and similar things. It is good practice to make your script independent from this environment by setting it to a certain state except for the few variables where you explicitly want this effect to take place. This includes (but is not limited to) making it independent of the current path it was called from.

I hope this helps.

bakunin
This User Gave Thanks to bakunin For This Post:
# 17  
Old 01-25-2017
Quote:
Originally Posted by bakunin
As a general rule: do not change directories inside a script at all (for exactly this reason, to avoid this confusion). ... bakunin
Absolutely! Great tips! Thank you.
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Input redirection within bash script

Hi, when I try to redirect input and the command is described as a string within an array redirection does not work. why? #!/bin/bash dir=("tail < ./hello.txt") tail < ./hello.txt #works ${dir} #does not work (2 Replies)
Discussion started by: heinzel
2 Replies

2. Shell Programming and Scripting

Input redirection script

Hi, #!/bin/bash while ; do rm -f /tmp/pipe mkfifo /tmp/pipe ./yuv4mpeg_to_v4l2 < /tmp/pipe & mplayer tom_and_jerry.mp4 -vf scale=480:360 -vo yuv4mpeg:file=/tmp/pipe sleep 65; done When I run this - after mplayer finishes playing video it says - Exiting... (End of... (2 Replies)
Discussion started by: ashokvpp
2 Replies

3. Shell Programming and Scripting

STDOUT and STDERR redirection within a script

Hello all, I have a for loop executing in a script that I want to redirect STDOUT to screen and to file, while directing STDERR to the bit bucket. Here is the general sentax of what I'm doing: for i in thingy do some_command ${i} done 1>&1 | tee ${LOGFILE} 2> /dev/null What I am... (2 Replies)
Discussion started by: LinuxRacr
2 Replies

4. Shell Programming and Scripting

How to check for Input Redirection in my script?

All, I have a requirement to write a script where I check for Input redirection when the script was executed, based on which I handle my logic. Below is the example: my.script #! /bin/ksh # Not sure how to frame the if condition below if ; then echo "Input Redirected from a file" ... (7 Replies)
Discussion started by: bharath.gct
7 Replies

5. Shell Programming and Scripting

Difficulty using "execute immediate" in shell - Sql script

Hello members, I get an unexpected "end of file" error while trying to execute the following piece of code in the bash / ksh shell. I'm assuming the problem is with using the "execute immediate statement" #! /bin/bash tname="table" for i in * do sqlstr="create table $tname$i as select... (3 Replies)
Discussion started by: novice82
3 Replies

6. Shell Programming and Scripting

cat in the command line doesn't match cat in the script

Hello, So I sorted my file as I was supposed to: sort -n -r -k 2 -k 1 file1 | uniq > file2 and when I wrote > cat file2 in the command line, I got what I was expecting, but in the script itself ... sort -n -r -k 2 -k 1 averages | uniq > temp cat file2 It wrote a whole... (21 Replies)
Discussion started by: shira
21 Replies

7. UNIX for Dummies Questions & Answers

Regarding redirection using cat.

The behavior of the following 2 operations is unexpected. K1 and K2 are files here :- 1) cat < K1 K2 The above operation should actually display contents of the both files. But it gives the contents of K2 only. How is that ? 2) cat > K1 K2 Above operation takes the contents of... (2 Replies)
Discussion started by: marconi
2 Replies

8. UNIX for Dummies Questions & Answers

Difference between cat , cat > , cat >> and touch !!!

Hi Can anybody tell the difference between Difference between cat , cat > , cat >> and touch command in UNIX? Thanks (6 Replies)
Discussion started by: skyineyes
6 Replies

9. UNIX for Dummies Questions & Answers

redirection to tty** with cat

I tried to cat a file to another user that was logged in, but I received an error message that displayed something like: %cat funny > /dev/ttyp3 zsh: permission denied: /dev/ttyp3 Thank you all for your help (1 Reply)
Discussion started by: zorro
1 Replies
Login or Register to Ask a Question