Help with taking variable of for loop in textfile


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help with taking variable of for loop in textfile
# 1  
Old 11-08-2017
Help with taking variable of for loop in textfile

Hi,

I am new to unix/linux scripting.
I have a text file, listlib.txt where the content:
Code:
lib1_23
lib34_a
ab_li_lab

I need to generate a file (.log) of each cell. I am planning to create a (.csh) script that will have for loop with variable taken from listlib.txt. As for now, i have no automation script for this, generate .log one by one. Problem rises when i have many lib more than 30 libs.
Moderator's Comments:
Mod Comment Please use CODE tags when displaying sample input, output, and code segments.

Last edited by Don Cragun; 11-08-2017 at 02:48 AM.. Reason: Add CODE and ICODE tags.
# 2  
Old 11-08-2017
The csh shell is notorious for behaving differently in scripts than it does interactively, is not standardized, and is usually not suggested as the best shell to use on UNIX and UNIX-like systems.

You don't say what is involved when you "generate a file", but maybe the following (which will work with any shell that uses the syntax described by the standards for shell behavior and works with many non-csh derived shells (such as ksh, bash, and several other shells based on Bourne shell syntax) will give you an idea of how to start:
Code:
#!/bin/ksh
while read -r log
do	touch "$log.log"
done < listlib.txt

# 3  
Old 11-08-2017
Hi, I am sorry but I don't quite understand on the code. I tried the code but it stated:

Code:
while: Expression Syntax.

directory (liblist.txt):
user/mmaz/liblist.txt

directory (test3.csh):
user/mmaz/lib/test.csh


My rough idea for test3.csh:
Code:
   
for each lib in liblist.txt
gen_lib_log $lib
end for

So, the expected output will be in user/mmaz/log/, where the command will generate the log automatically.
e.g:
Code:
user/mmaz/log/lib1_23.log

# 4  
Old 11-08-2017
Save this as test3.sh
Code:
#!/bin/sh
while read lib
do
  echo "lib $lib, logfile $lib.log"
  {
  # now output is redirected to the logfile
  echo  "$lib.log created `date`"
  } > "$lib.log"
done < listlib.txt

Make this executable with the command
Code:
chmod +x test3.sh

And run it with
Code:
./test3.sh

You will find the logfiles created for each library.
# 5  
Old 11-08-2017
If your working command line shell is already csh then make sure that what you may think is a comment line starting with #! is included as the very first line without being indented. This is a special location and the #! described the code/shell to be using. That might make the suggestions work.

If not, can you show us the following output:-
Code:
uname -a
ps
echo $SHELL
pwd
ls -l test3.sh     # .... or whatever name you have given your code
cat test3.sh       # .... or whatever name you have given your code

Please wrap each in CODE tags which makes it much easier to read and preserves spacing for indenting or fixed-width data.




Thanks, in advance,
Robin
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

sed not taking variable value

Hi All, Could you please help me, why sed is not able to take variable value when I try to replace using sed. I want to replace 2nd column (time) and keeping intact others. cur="09:30" CODE="VL" new="09:35" sed s/'\(.*def.monitor."${CODE}".qStart.*\)"${cur}"\(.*read\)/\1"${new}"\2'/g... (3 Replies)
Discussion started by: sdosanjh
3 Replies

2. Shell Programming and Scripting

How to separate sorte different characters from one textfile and copy them in a new textfile?

My first post, so don't kill me :) Say i open some textfile with some example like this. on the table are handy, bread and wine Now i know exactly what is in and i want to separate and sorted it in terminal to an existing file with another 2 existing lines in like this: table plane ... (3 Replies)
Discussion started by: schwatter
3 Replies

3. Shell Programming and Scripting

While loop problem taking too long

while read myhosts do while read discovered do echo "$discovered" done < $LOGFILE | grep -Pi "|" | egrep... (7 Replies)
Discussion started by: SkySmart
7 Replies

4. Shell Programming and Scripting

Get line of textfile and store it in variable

Hi! I need to do the following: (1) I wan't to extract a line of a textfile (defined by a numer) and store it into a variable... (2) ...but I want to cut out a part of the line which is between two tokens and store just this to the variable Example: BlaBlaBla Bla2Bla2Bla2 *pPointerOne;... (4 Replies)
Discussion started by: Michi21609
4 Replies

5. Shell Programming and Scripting

Taking sed result in a variable

Hi All, How can i take the following code having three seds into a variable : echo "$DateFileFormat" | sed 's/\./\\\\./g' | sed 's/\$/+/g' | sed 's/\#/'$job_date'/g' I want to get the result stored in a script variable i tried var2=`echo "$DateFileFormat" | sed 's/\./\\\\./g' |... (4 Replies)
Discussion started by: abhinav192
4 Replies

6. UNIX for Advanced & Expert Users

Comparison and For Loop Taking Too Long

I'd like to 1. Check and compare the 10,000 pnt files contains single record from the /$ROOTDIR/scp/inbox/string1 directory against 39 bad pnt files from the /$ROOTDIR/output/tma/pnt/bad/string1 directory based on the fam_id column value start at position 38 to 47 from the record below. Here is... (1 Reply)
Discussion started by: hanie123
1 Replies

7. Shell Programming and Scripting

For Loop Taking Too Long

I'm new from UNIX scripting. Please help. I have about 10,000 files from the $ROOTDIR/scp/inbox/string1 directory to compare with the 50 files from /$ROOTDIR/output/tma/pnt/bad/string1/ directory and it takes about 2 hours plus to complete the for loop. Is there a better way to re-write the... (5 Replies)
Discussion started by: hanie123
5 Replies

8. Solaris

Infinite for loop is taking too much CPU usage %

Hi All, I wrote one simple for loop shell script which prints number..but this loop is infinite...but its taking lot of CPU (15.7) %. if i am using sleep cmd then cpu usage become 0.4 %. Is there anyway to reduce this CPU usage without using sleep cmd? i dont want 2 use sleep cmd... (7 Replies)
Discussion started by: pa.chidhambaram
7 Replies

9. Shell Programming and Scripting

Problem taking input from file with for loop

I am trying to take input from a file and direct it into a bash script. This script is meant to be a foreach loop. I would like the script to process each item in the list one by one and direct the output to a file. # cat 1loop #!/bin/bash # this 2>&1 to redirect STDERR & STDOUT to file... (4 Replies)
Discussion started by: bash_in_my_head
4 Replies

10. UNIX for Dummies Questions & Answers

while loop not taking the not equal to condition

Hi I am trying to write a code like this echo "enter a type" read a_type while || || || || || do echo "invalid engine type Please enter correct a_type" read engine_type < /dev/tty done My problem is that even if i give the a_type as the correct one that i have listed above that... (9 Replies)
Discussion started by: ssuresh1999
9 Replies
Login or Register to Ask a Question