Sponsored Content
Top Forums Shell Programming and Scripting [KSH] Creating automatic variable from read input Post 302713191 by kshji on Wednesday 10th of October 2012 10:50:30 AM
Old 10-10-2012
Ksh93, bash, ... give also little nicer syntax version for the arithmetic expansion .
You can put everything inside (( )) and use variables without dollar sign $.
This is not Posix compatible, but look easier to read. Works only in the arithmetic expansion using (( )).

In this example input values has saved to the array and to the string (inputstr).
Code:
inputstr=""
while true
do
 echo -e -n "Input a number (just press ENTER to stop): "
 read input
 [ "$input" = "" ] && break
 array+=( "$input" )              # new element to the array
 inputstr="$inputstr $input"    # join string
done

echo ${array[@]}

sum=0
# take all values from the list, in this case all values from array
for val in ${array[@]}
do
    (( sum+=val ))   # works in ksh93, bash, ...
    # sum=$(( $sum + $val ))  # posix compatible, you can use also let command
done

echo $sum

# or using inputstr
sum=0
for val in $inputstr
do
    (( sum+=val ))   
done

echo $sum

 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

creating a fixed length output from a variable length input

Is there a command that sets a variable length? I have a input of a variable length field but my output for that field needs to be set to 32 char. Is there such a command? I am on a sun box running ksh Thanks (2 Replies)
Discussion started by: r1500
2 Replies

2. Shell Programming and Scripting

Automatic Arrays in ksh

Given a line of text in ksh: string1 string2 string3 .....stringn is there a way of automatically assigning each string to an array element? Or just different variables would do. Thanks, Jon (1 Reply)
Discussion started by: Jonny2Vests
1 Replies

3. UNIX for Advanced & Expert Users

Automatic input to commands

Hi, In a shell script i am running a command which is asking for input. How can i give that automatically. I have done this before but for the time being can't recall. Was something like <| Thanks (6 Replies)
Discussion started by: vibhor_agarwali
6 Replies

4. Shell Programming and Scripting

make a variable read the value from the standard input

Hi, In Perl, how can we define a variable make it read the value from the standard input? Meaning, how can have the user type in the value that will be assigned to the variable? Thanks, (2 Replies)
Discussion started by: Pouchie1
2 Replies

5. Shell Programming and Scripting

KSH: Compare variable to $1 in an input file

Hello, I am working with KSH on AIX and I have 2 files generated from different sources... as seen below: FILE1 FILE2 AAA AAA@ABS0001C BBB BBB@ABS0003D CCC CCC@ABS0023A DDD DDD@ABC0145D EEE EEE@ABS0090A FFF FFF@ABS0002A GGG GGG@ABC0150D HHH FILE1 is main main data source,... (4 Replies)
Discussion started by: right_coaster
4 Replies

6. Shell Programming and Scripting

Automatic Input to a command

Hi, I have an issue where i run an command in a shell script. command >/dev/null ret=$? echo ret If the command returns an error i'm redirecting it to /dev/null. The prob is if an error comes it expects the user to press return to continue. And hence the return is not echoed. and the end... (4 Replies)
Discussion started by: subhrap.das
4 Replies

7. Shell Programming and Scripting

Read multiple input from CLI :ksh

Hi folks.. i got a requirement to red multiple directories from STDIN and store them to a variable. ex:- echo "Enter directory to add:" echo " Enter directory to add:" read value till there is input and when there is no input close the read loop and store variable into an array ... (1 Reply)
Discussion started by: bangaram
1 Replies

8. Shell Programming and Scripting

ksh loop to read input until QUIT

Hi I'm looking to write a simple ksh loop reading user input (and write it to a file) until the user enters QUIT at which point I want it to continue. Does anyone have an example of this type of loop? Any help much appreciated Cheers (2 Replies)
Discussion started by: Grueben
2 Replies

9. Shell Programming and Scripting

ksh to read input until QUI

Hello all I'm looking to write a simple script (ksh/sh/bsh) to read user input and write it to a file (adding each time) until the user enters QUIT at which point I'm hoping to ask some more questions. Any help much apprecited (2 Replies)
Discussion started by: Grueben
2 Replies

10. Shell Programming and Scripting

ksh - Read input from file and output CSV i same row

Hello I have the following output and want the output to look: FROM: GigabitEthernet0/0 is up, line protocol is up 1 input errors, 0 CRC, 0 frame, 1 overrun, 0 ignored 275 output errors, 0 collisions, 3 interface resets GigabitEthernet0/1 is up, line protocol is up 0... (4 Replies)
Discussion started by: JayJay2018
4 Replies
sum(n)							     Cyclic Redundancy Checks							    sum(n)

__________________________________________________________________________________________________________________________________________________

NAME
sum - Calculate a sum(1) compatible checksum SYNOPSIS
package require Tcl 8.2 package require sum ?1.1.0? ::crc::sum ?-bsd | -sysv? ?-format fmt? ?-chunksize size? [ -filename file | -channel chan | string ] _________________________________________________________________ DESCRIPTION
This package provides a Tcl-only implementation of the sum(1) command which calculates a 16 bit checksum value from the input data. The BSD sum algorithm is used by default but the SysV algorithm is also available. COMMANDS
::crc::sum ?-bsd | -sysv? ?-format fmt? ?-chunksize size? [ -filename file | -channel chan | string ] The command takes string data or a file name or a channel and returns a checksum value calculated using the sum(1) algorithm. The result is formatted using the format(n) specifier provided or as an unsigned integer (%u) by default. OPTIONS
-sysv The SysV algorithm is fairly naive. The byte values are summed and any overflow is discarded. The lowest 16 bits are returned as the checksum. Input with the same content but different ordering will give the same result. -bsd This algorithm is similar to the SysV version but includes a bit rotation step which provides a dependency on the order of the data values. -filename name Return a checksum for the file contents instead of for parameter data. -channel chan Return a checksum for the contents of the specified channel. The channel must be open for reading and should be configured for binary translation. The channel will no be closed on completion. -chunksize size Set the block size used when reading data from either files or channels. This value defaults to 4096. -format string Return the checksum using an alternative format template. EXAMPLES
% crc::sum "Hello, World!" 37287 % crc::sum -format 0x%X "Hello, World!" 0x91A7 % crc::sum -file sum.tcl 13392 AUTHORS
Pat Thoyts BUGS, IDEAS, FEEDBACK This document, and the package it describes, will undoubtedly contain bugs and other problems. Please report such in the category crc of the Tcllib SF Trackers [http://sourceforge.net/tracker/?group_id=12883]. Please also report any ideas for enhancements you may have for either package and/or documentation. SEE ALSO
cksum(n), crc32(n), sum(1) KEYWORDS
checksum, cksum, crc, crc32, cyclic redundancy check, data integrity, security, sum CATEGORY
Hashes, checksums, and encryption COPYRIGHT
Copyright (c) 2002, Pat Thoyts <patthoyts@users.sourceforge.net> crc 1.1.0 sum(n)
All times are GMT -4. The time now is 12:30 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy