Newbie needs Help with reading files into..


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Newbie needs Help with reading files into..
# 8  
Old 06-23-2018
Quote:
Originally Posted by granatelbart
Code:
         
if check_file test.sh; then
         echo "File exists"
         else
         echo "
         #!/bin/bash

         SOURCEDIR=${manifest}
         if [ -z "$STY" ]; then exec screen -dm -S 'test Import' /bin/bash "$0"; fi
         for i in *; do id=$(echo ${i} | cut -d'_' -f1 | cut -c 2- ); extid=00${id}; /root/test_dir/other_test_file.pl -b ${SOURCEDIR} -m ${extid} -v 2>&1 | tee /root/test_dir/${extid}.log ; done " >> test.sh
         echo created test-Importer
         fi

A little background: "${...}" is not just any string for the shell but a sort-of command: it means to replace the string ${...} with the value assigned to the variable named .... If there was nothing assigned before this value is "" (the empty string).

If you want the shell to interpret any character or string literally instead of using the aforementioned interpretation on it there are two ways: quoting and escaping. Quoting means to surround a string by double or single quotes. double quotes only turn off some interpretation mechanisms (i.e. field splitting), but single quoting turns off all of them, also the "variable expansion". Try this to see the difference (wc counts the number of words) :

Code:
x="abc 123"
echo ${x} ; echo ${x} | wc
echo "${x}" ; echo "${x}" | wc
echo '${x}' ; echo '${x}' | wc

The other mechanism is escaping: by prepending a character with a backslash you can remove its meaning for the shell and turn it back into an ordinary character. Try this and notice the difference:

Code:
x="abc 123"
echo ${x}
echo \${x}

In your case it could look like this (notice that i use ksh syntax here). Btw., i have straightened out your code, because you don't want such lines not eve in your generated code, much less in your source. Such long lines are hard to read. As a rule of thumb: no line should be longer than 80 characters.

Another thing is: you used echo "..." >> test.sh. This would append the content rather than create a new file when the script runs. I changed that to create/overwrite the file. change the first exec-statement if i assumed wrongly:

Code:
if check_file test.sh; then
         echo "File exists"
else
         exec 3>test.sh               # exec 3>>test.sh
         print -u3 - '#!/bin/bash'
         print -u3 - 'SOURCEDIR=${manifest}'
         print -u3 - 'if [ -z "$STY" ] ; then'
         print -u3 - '     exec screen -dm -S 'test Import' /bin/bash "$0"'
         print -u3 - 'fi'
         print -u3 - 'for i in * ; do'
         print -u3 - '     extid="00"$(echo ${i} | cut -d'_' -f1 | cut -c 2- )'
         print -u3 - '     /root/test_dir/other_test_file.pl -b ${SOURCEDIR} -m ${extid} -v 2>&1 | tee /root/test_dir/${extid}.log'
         print -u3 - 'done'
         exec 3>&-
         echo created test-Importer
fi

Another observation: the command exec screen -d... will leave the script because exec replaces the running process (here the script) with the newly called one (here the screen command. Is this intentional?

I hope this helps.

bakunin
# 9  
Old 06-23-2018
Thanks a lot for helping.

Script is working now well


Closed
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Need Help in reading N days files from a Directory & combining the files

Hi All, Request your expertise in tackling one requirement in my project,(i dont have much expertise in Shell Scripting). The requirement is as below, 1) We store the last run date of a process in a file. When the batch run the next time, it should read this file, get the last run date from... (1 Reply)
Discussion started by: dsfreddie
1 Replies

2. Programming

need help with shell script filtering files and sort! newbie question?

Hi folks, I would like to get familiar with shell script programing. The first task is: write a shell script that: scans your home-folder + sub-directory for all txt-files that all users of your group are allowed to read and write then output these files sorted by date of last... (4 Replies)
Discussion started by: rollinator
4 Replies

3. Programming

help with opening files and reading them in C

In C, given a name or path to a file, how do I check if the path is valid, and how can I check its permisions will let me read from it, and how do i check if its an empty file? (15 Replies)
Discussion started by: omega666
15 Replies

4. Solaris

Newbie questions about HOME directory files

Hi, I am newbie to Solaris and system administration in general, and I have a couple of questions about files in my HOME directory. When I perform ls -la, I get the following list of files: drwxr-xr-x 7 XXXYYY staff 17 Aug 24 07:31 . drwxr-xr-x 7 root root 7... (2 Replies)
Discussion started by: JVerstry
2 Replies

5. Shell Programming and Scripting

reading from 2 files using awk

hi, Is it possible to read and compare 2 files which have different Field separators at the same time using awk??? file1: 1,dayal,maruti,Z-234,bangalore,KA,........ 2,yash,esteem,Y-007,delhi,DL,........... . . . fill 2: Z-234|Registered|Bangalore Y-007|Registered|Bangalore . . . ... (2 Replies)
Discussion started by: VGR
2 Replies

6. UNIX for Dummies Questions & Answers

UNIX newbie NEWBIE question!

Hello everyone, Just started UNIX today! In our school we use solaris. I just want to know how do I setup Solaris 10 not the GUI one, the one where you have to type the commands like ECHO, ls, pwd, etc... I have windows xp and I also have vmware. I hope I am not missing anything! :p (4 Replies)
Discussion started by: Hanamachi
4 Replies

7. Shell Programming and Scripting

reading two files

Hi, I have 2 files file 1 ---- 10013 This is a text 10014 This is a test 10015 This is a temp file 2 ---- 10013 010014737 ADSM 1300004 E 10014 020012323 ABCD 1718888 E 10015 030121212 ADSW 1290991 E need to compare column1 in... (2 Replies)
Discussion started by: injeti
2 Replies

8. Solaris

Reading Files In

I'm looking to write a script where i look in a file called files.txt. This file has a list of other files in there. I want the script to be able to look at this files.txt and go off and find the files that are listed in another directory. I am doing it this way as filenames change week in... (1 Reply)
Discussion started by: Pablo_beezo
1 Replies

9. UNIX for Dummies Questions & Answers

Reading Files

Hi I need to perform certain tests on information contained in a file, a lot of what is contained in the file is duplicate data, so what I want to do as it is a failrly substantial file size is to start and finish reading from a specifed line in the file. Can someone tell me how to indicate... (3 Replies)
Discussion started by: oop
3 Replies

10. Shell Programming and Scripting

reading gz files

Hi, I have a simple perl script where I am passing array of gziped files to the while loop and trying to read content of each field one line at the time using gzcat. Yet, I can not get it to work Here is what I am doing ... while ($filename=shift) { open(MYFILE, "| gzcat $filename"); ... (2 Replies)
Discussion started by: arushunter
2 Replies
Login or Register to Ask a Question