Difficulty with CAT redirection in script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Difficulty with CAT redirection in script
# 8  
Old 01-24-2017
Not sure if I read your script correctly, but if its purpose is to concatenate all LocalConfig.pri files into one master file file and generate a contents file in parallel, would this simplified command do:
Code:
cat $(locate -b LocalConfig.pri | tee contentsfile) > ./LocalConfigMaster.pri

The index number could, if need be, calculated from the contentsfile's lines...
# 9  
Old 01-24-2017
Quote:
Originally Posted by Don Cragun
Note that the command:
Code:
cat ${outputarray[*]} > /some/file

may fail if any of the filenames in the array contain whitespace characters. To protect against this case, the following is usually safer:
Code:
cat "${outputarray[@]}" > /some/file

Great tip and I am already doing that. The locate command retrieves 108 file names, none of which contain any whitespace in their name. The problem is definitely perplexing.

---------- Post updated at 09:44 AM ---------- Previous update was at 09:05 AM ----------

Quote:
Originally Posted by RudiC
Not sure if I read your script correctly, but if its purpose is to concatenate all LocalConfig.pri files into one master file file and generate a contents file in parallel, would this simplified command do:
Code:
cat $(locate -b LocalConfig.pri | tee contentsfile) > ./LocalConfigMaster.pri

The index number could, if need be, calculated from the contentsfile's lines...
You almost have it. No contents file. My contents file is the LocalConfigMaster.pri file (it will contain the contents of all the LocalConfig.pri files).
# 10  
Old 01-24-2017
Well, read: Table of Contents; file containing all the file names/-paths found which will be lost when just catting the files.
# 11  
Old 01-24-2017
Quote:
Originally Posted by bakunin
In general it is usually a good idea to just post the information instead of telling us about it. The reason is: being a newcomer (as per your own admission) you might lack the knowledge of what is vital information and what is not. I hear you but this debug output is very simplistic. I am new to bash but not to software development/script writing. But I will generate and post some output below. The output is filenames - that's it (no errors, no mysterious codes, just names of files, 108 of them) - and I'm throwing them up against cat and cat is not doing anything with them. Show the scripts code (you did that), then run it and copy its output from start to end (ideally along with the return code it produced) here, enclosed in CODEtags, like this:

Code:
# ./myscript.sh
bla
foo
error: unable to make the flurbishes grommicking

# echo $?
2

Here is one more such suggestion: are you sure you want to add always to the file in question? Wouldn't you rather want to start an empty file for every run of the script and then fill the contents of all the other files to this one? Good suggestion but at this moment I am not concerned about it. Once I understand how I am not using cat correctly then I may come back to tweak the process.

If so, supposing outputarray is an array variable containing path names AND there are no special characters (blanks, tabs, ...) in the file names you can do:

Code:
cat ${outputarray[*]} > /some/file

${outputarray[*]} will expand to a list of all the array elements and cat takes an (open-ended) list of filenames to process as arguments. There are some restrictions on this and if the array has several hundreds of entries this method might break eventually (the exact point depending on your systems configuration), but for a handful (anything less than hundred for sure) of filenames this works well.

I hope this helps. I definitely appreciate your input and help!

bakunin

[code]
Here is the code ...
Code:
#!/bin/bash
echo ""
echo "==============================================================================="
echo "This script copies all of the LocalConfig.pri files contents into another file."
echo "==============================================================================="
echo ""
cd /
echo "Changed directory to ..."
pwd

# Assign 'locate' results to a variable.
output=$(locate -b "LocalConfig.pri")

# Parse the $output variable into an array. Each line is a full path.
while read -r line; do outputArray+=("$line"); done <<<"$output"

# Loop through the array.
for idx in "${!outputArray[@]}"; do
    # Print array element index and contents.
    printf ' Output number %d is %s' "$idx" "${outputArray[idx]}"
    printf '\n'
    # Cat each file into one 'master' file.
    echo -e "\tconcatenating\n\t\t ${outputArray[idx]} \n\tinto \n\t\t./LocalConfigMaster.pri ..."
    cat "${outputArray[idx]}" >> ./LocalConfigMaster.pri
done

I highlighted the code above that is not doing anything as best as I can tell.

Here is the output (I abbreviated it because there are 108 files returned from the locate command)...
Code:
my.name@host:~/bash_scripts$ sudo ./CopyLocalConfig.sh
[sudo] password for my.name: 

===============================================================================
This script copies all of the LocalConfig.pri files contents into another file.
===============================================================================

Changed directory to ...
/
 Output number 0 is /fouo/project-release-develop/project/LocalConfig.pri
	concatenating
		 /fouo/project-release-develop/project/LocalConfig.pri 
	into 
		./LocalConfigMaster.pri ...

The output highlighted above in bold, repeats for every occurrence of LocalConfig.pri (107 more times).

The script terminates without any errors. I execute ls -la and there is no LocalConfigMaster.pri file.
Moderator's Comments:
Mod Comment Please use CODE tags (not QUOTE tags) when displaying sample output.

Last edited by Don Cragun; 01-24-2017 at 12:51 PM.. Reason: Change QUOTE tags to CODE tags.
# 12  
Old 01-24-2017
Just to be sure we cover the obvious case, what output do you get from executing the command:
Code:
ls -l /LocalConfigMaster.pri

instead of the command:
Code:
ls -la

?

Just because your script changes directory to the system's root directory does not mean that the directory of your interactive shell has moved to that location after you run your script...
This User Gave Thanks to Don Cragun For This Post:
# 13  
Old 01-24-2017
Quote:
Originally Posted by Don Cragun
Just to be sure we cover the obvious case, what output do you get from executing the command:
Code:
ls -l /LocalConfigMaster.pri

instead of the command:
Code:
ls -la

?

Just because your script changes directory to the system's root directory does not mean that the directory of your interactive shell has moved to that location after you run your script...
Code:
-rw-r--r--    1 root root 16226292 Jan 24 10:01 LocalConfigMaster.pri

Oh my God, I am such an idiot! LOL! cat is working fine. Talk about a stupid mistake. LocalConfigMaster.pri is getting written to the directory that I had the script cd to. I kept looking for it in the directory that I executed the script from. I can't believe how many hours I blew on this. Thank you for pointing out what should've been obvious to me Don!
# 14  
Old 01-24-2017
I'm glad we worked it out. Sometimes the obvious possibility just seems to unlikely to ask about. Smilie
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