ksh - building a var


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting ksh - building a var
# 1  
Old 02-20-2014
ksh - building a var

This works
Code:
#!/bin/ksh
FILE="file.txt"
dosumtin () {
date >> FILE
}

for i in {1..5}
do
   dosumtin
done
cat $FILE

But instead of building a file, I want to do the same with a var or an array. That is, to build one that saves all 5 of the subs execution responses in a var or an array available to print at the end of the script.

This doesnt work ..given for illustration.

Code:
#!/bin/ksh
dosumtin () {
date >> $FILE
}

for i in {1..5}
do
   dosumtin
done
echo "This is my cheap file." > file.txt
cat $FILE > file.txt

Thanks in advance !
# 2  
Old 02-20-2014
After appending 5 times the current date-string, you're overwriting the $FILE with "this is my cheapfile" and then again overwrite it.. with itself...

I'd assume you wanted something like:

Code:
#!/bin/ksh

# Vars
FILE=$HOME/file.txt
VAR=""
i=0

# Function
dosumtin () {
  date
}

# Generation of variable
while [ $i -lt 5 ] 
   VAR+="$(dosumtin)\n"
   ((i++))
done

# Save it
printf "This is my cheap file.\n$VAR" > $FILE

Untested (i'm on windows)..
Hope this helps

Last edited by sea; 02-20-2014 at 04:55 PM..
This User Gave Thanks to sea For This Post:
# 3  
Old 02-20-2014
Hi popeye,
'>>' redirect may can not used to a variable instead of a file.
If need to saves all 5 of the subs execution responses in a var or an array.

This codes may works.

Array:
Code:
#!/bin/ksh
dosumtin () {
#date >> $FILE
FILE[$1-1]=$(date)
}

for i in $(seq 1 5)
do
   dosumtin $i
done
echo "This is my cheap file." > file.txt
IFS=\\
printf "${FILE[*]}"|tr '\\' '\n' >> file.txt

A var:
Code:
#!/bin/ksh
dosumtin () {
#date >> $FILE
[ $i -ne 1 ]&&FILE=$FILE"\n"
FILE=$FILE$(date)
}

for i in $(seq 1 5)
do
   dosumtin $i
done
echo "This is my cheap file." > file.txt
printf "$FILE" >> file.txt

Hope this help for you.
This User Gave Thanks to Lucas_0418 For This Post:
# 4  
Old 02-20-2014
You don't need to open the same file 9 times to do 9 writes.

Code:
( echo "header"

for X in 1 2 3 4 5
do
        echo "something $X"
done

echo "footer" ) > filename

To get the output of a statement in a variable: VAR=$( something )
This User Gave Thanks to Corona688 For This Post:
# 5  
Old 02-20-2014
@Lucas_0418: It is better to not leave the format argument of printf unspecified. Also if you use [@] instead of [*] you do not need to use tr and you could just do this:
Code:
printf "%s\n" "${DATE[@]}" > file.txt

---
At Corona: Using grouping operators {} instead of () would save a subshell.
These 2 Users Gave Thanks to Scrutinizer For This Post:
# 6  
Old 02-20-2014
Is this homework?
# 7  
Old 02-20-2014
Code:
#!/bin/ksh
dosumtin () {
date
}

# store stdout of the following code in var
var=$(
for i in {1..5}
do
   dosumtin
done
)

echo "$var"

This User Gave Thanks to MadeInGermany For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Transfer the logs being thrown into /var/log/messages into another file example /var/log/volumelog

I have been searching and reading about syslog. I would like to know how to Transfer the logs being thrown into /var/log/messages into another file example /var/log/volumelog. tail -f /var/log/messages dblogger: msg_to_dbrow: no logtype using missing dblogger: msg_to_dbrow_str: val ==... (2 Replies)
Discussion started by: kenshinhimura
2 Replies

2. Shell Programming and Scripting

Csh , how to set var value into new var, in short string concatenation

i try to find way to make string concatenation in csh ( sorry this is what i have ) so i found out i can't do : set string_buff = "" foreach line("`cat $source_dir/$f`") $string_buff = string_buff $line end how can i do string concatenation? (1 Reply)
Discussion started by: umen
1 Replies

3. Shell Programming and Scripting

ksh : Building an array based on condition result

I want to build an Errorlog. I would like to build an array as I move through the if statements and print the array once all error conditions have been defined. The results need to be comma delimited. tsver will be static "1.9.6(2)" other vars $prit $lt $rt can have the same or a different... (1 Reply)
Discussion started by: popeye
1 Replies

4. Solaris

Difference between /var/log/syslog and /var/adm/messages

Hi, Is the contents in /var/log/syslog and /var/adm/messages are same?? Regards (3 Replies)
Discussion started by: vks47
3 Replies

5. Solaris

/var/adm & /var/sadm

what is the difference between tha /var/adm and /var/sadm files in solaris 10 Os please can any one respond quickly thanking you (2 Replies)
Discussion started by: wkbn86
2 Replies

6. Shell Programming and Scripting

ksh: what does var=$(command) mean?

hi, i can see in a script it contains var=$( myFile | grep -i err ) why has this person done it like this? why not just var=`myFile | grep -i err` thanks (9 Replies)
Discussion started by: JamesByars
9 Replies

7. Shell Programming and Scripting

${!var} does not work in ksh

Anyone knows why the following function does not work in ksh (it does in bash)? var() # Displays var value; case insensitive { _var="$1" if ; then echo ${!_var} else _var=$(echo "$_var" | tr 'a-z' 'A-Z') echo ${!_var} fi unset _var }$ var home ksh:... (4 Replies)
Discussion started by: victorbrca
4 Replies

8. Shell Programming and Scripting

How to use shell var for pattern string at KSH

Hi there, In the following test, how to use shell var for pattern, regular expression. I need to accept pattern at argument, use it to pattern matching at shell script. Test: #!/bin/ksh # name t.sh exp="a@(a|b)" touch aa ab ac echo "\nTest without variable" echo "---------------------"... (2 Replies)
Discussion started by: tkang007
2 Replies

9. Solaris

diff b/w /var/log/syslog and /var/adm/messages

hi sirs can u tell the difference between /var/log/syslogs and /var/adm/messages in my working place i am having two servers. in one servers messages file is empty and syslog file is going on increasing.. and in another servers message file is going on increasing but syslog file is... (2 Replies)
Discussion started by: tv.praveenkumar
2 Replies

10. Shell Programming and Scripting

import var and function from ksh script to another ksh script

Ih all, i have multiples ksh scripts for crontab's unix jobs they all have same variables declarations and some similar functions i would have a only single script file to declare my variables, like: var1= "aaa" var2= "bbb" var3= "ccc" ... function ab { ...} function bc { ... }... (2 Replies)
Discussion started by: wolfhurt
2 Replies
Login or Register to Ask a Question