Sponsored Content
Top Forums UNIX for Beginners Questions & Answers Bash Variable scope - while loop while reading from a file Post 303037381 by bakunin on Wednesday 31st of July 2019 02:39:50 AM
Old 07-31-2019
Quote:
Originally Posted by wisecracker
As is often the case with a little bit of jiggery pokery there is a pseudo-back-door.
This is especially for you:

Code:
#! /bin/bash

pInitPermStor ()
{
if [ ! -d "$fStorage" ] ; then
     return $(mkdir -p "$fStorage")            # create storage dir if not there
fi

return $(rm -f "$fStorage/*")                  # empty it if it already exists
}




pRemovePermStor ()
{
if [ ! -d "$fStorage" ] ; then
     return 1                                  # shouldn't happen
fi

return $(rm -f "$fStorage")                    # clean up
}




pGetVar ()
{
local chVar="$1"

if [ -f "${fStorage}/${chVar}" ] ; then
     printf "%s=%s\n" "$chVar" $( < "${fStorage}/${chVar}" )
else
     printf "$chVar"=\"\"
fi

return 0
}


pPutVar ()
{
local chVar="$1"
local chValue="$2"

if ! printf "%s\n" "$2" > "${fStorage}/${chVar}" ; then
     return 1
fi

return 0
}


# ------------------ main()
declare fStorage="/path/to/dir/$$"

if ! pInitPermStor ; then
     printf "ERROR: cannot initialise permanent variable storage\n" >&2
     exit 1
fi

pPutVar "x" "abcd"             # set value of variable x to "abcd"
eval $( pGetVar "x" )          # retrieve and set variable x
echo var x is: $x

if ! pRemovePermStor ; then
     printf "ERROR: permanent variable storage not found\n" >&2
     exit 1
fi
exit 0

ROFL, i doubt this helps anyone but it was fun to write.

bakunin

/edited following MadeInGermanys suggestion

Last edited by bakunin; 07-31-2019 at 10:19 AM..
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

bash and ksh: variable lost in loop in bash?

Hi, I use AIX (ksh) and Linux (bash) servers. I'm trying to do scripts to will run in both ksh and bash, and most of the time it works. But this time I don't get it in bash (I'm more familar in ksh). The goal of my script if to read a "config file" (like "ini" file), and make various report.... (2 Replies)
Discussion started by: estienne
2 Replies

2. UNIX for Dummies Questions & Answers

reading more than one variable into a for loop

Hi, I have a file (details.txt) with 3 rows of variables ie... name postcode age john D fr25dd 25 mark W ab122aa 22 phil C cd343bb 33 What I want to do is read down the list with a loop and add each field into a one line piece of text... So I have a file (test1) which reads;... (3 Replies)
Discussion started by: starsky
3 Replies

3. Shell Programming and Scripting

Not access variable outside loop when a reading a file

I am writing a shell script using the korn shell. It seems that I am only able to use local variables within a while loop that is reading a file. (I can't access a variable outside a previously used while loop.) It's been a while since I wrote shell scripts. Here is a sample cat file.txt... (4 Replies)
Discussion started by: ricardo.ludwig
4 Replies

4. Shell Programming and Scripting

How to get the modified value of variable outside the while loop reading from a file

Hi Friends , Sorry if this is a repeated question , The input file contains 5 lines , so the the values of the variables i and count should b i=5; count=15 but the variables are not updating , the value of variables showing i=0 and count =0 only.:mad: can any1 help me please. (11 Replies)
Discussion started by: babusek
11 Replies

5. Shell Programming and Scripting

Bourne Shell - Problem with while loop variable scope.

Hello I am having issues with a script I'm working on developing on a Solaris machine. The script is intended to find out how many times a particular user (by given userid) has logged into the local system for more than one hour today. Here is my while loop: last $user | grep -v 'sshd'... (7 Replies)
Discussion started by: DaveRich
7 Replies

6. UNIX for Dummies Questions & Answers

Bash loops and variable scope

Hi All, I've been researching this problem and I am pretty sure that the issue is related to the while loop and the piping. There are plenty of other threads about this issue that recommend removing the pipe and using redirection. However, I haven't been able to get it working using the ssh and... (1 Reply)
Discussion started by: 1skydive
1 Replies

7. Shell Programming and Scripting

(BASH) Using a loop variable to grep something in a file?

Hi, I have a loop running until a variable L that is read previously in the full script. I'd like to grep some information in an input file at a line that contains the value of the loop parameter $i. I've tried to use grep, but the problem is nothing is written in the FILE files. It seems grep... (5 Replies)
Discussion started by: DMini
5 Replies

8. Shell Programming and Scripting

Variable scope in bash

Hello! Before you "bash" me with - Not another post of this kind Please read on and you will understand my problem... I am using the below to extract a sum of the diskIO on a Solaris server. #!/bin/sh PATH=/usr/bin:/usr/sbin:/sbin; export PATH TEMP1="/tmp/raw-sar-output.txt$$"... (3 Replies)
Discussion started by: haaru
3 Replies

9. Shell Programming and Scripting

Detail on For loop for multiple file input and bash variable usage

Dear mentors, I just need little explanation regarding for loop to give input to awk script for file in `ls *.txt |sort -t"_" -k2n,2`; do awk script $file done which sorts file in order, and will input one after another file in order to awk script suppose if I have to input 2 or... (4 Replies)
Discussion started by: Akshay Hegde
4 Replies

10. Shell Programming and Scripting

BASH: variable and function scope and subscripts

Hi, I'm a Delphi developer new to linux, new to this forums and new to BASH programming and got a new task in my work: maintaining an existing set of BASH scripts. First thing I want to do is making the code more reliable as in my opinion it's really bad written. So here's the quest: I'm... (6 Replies)
Discussion started by: rse
6 Replies
explain_printf(3)					     Library Functions Manual						 explain_printf(3)

NAME
explain_printf - explain printf(3) errors SYNOPSIS
#include <libexplain/printf.h> const char *explain_printf(const char *format); const char *explain_errno_printf(int errnum, const char *format); void explain_message_printf(char *message, int message_size, const char *format); void explain_message_errno_printf(char *message, int message_size, int errnum, const char *format); DESCRIPTION
These functions may be used to obtain explanations for errors returned by the printf(3) system call. explain_printf const char *explain_printf(const char *format); The explain_printf function is used to obtain an explanation of an error returned by the printf(3) system call. The least the message will contain is the value of strerror(errno), but usually it will do much better, and indicate the underlying cause in more detail. The errno global variable will be used to obtain the error value to be decoded. format The original format, exactly as passed to the printf(3) system call. Returns: The message explaining the error. This message buffer is shared by all libexplain functions which do not supply a buffer in their argument list. This will be overwritten by the next call to any libexplain function which shares this buffer, including other threads. Note: This function is not thread safe, because it shares a return buffer across all threads, and many other functions in this library. Example: This function is intended to be used in a fashion similar to the following example: errno = 0; int result = printf(format); if (result < 0 && errno != 0) { fprintf(stderr, "%s ", explain_printf(format)); exit(EXIT_FAILURE); } The above code example is available pre-packaged as the explain_printf_or_die(3) function. explain_errno_printf const char *explain_errno_printf(int errnum, const char *format); The explain_errno_printf function is used to obtain an explanation of an error returned by the printf(3) system call. The least the message will contain is the value of strerror(errno), but usually it will do much better, and indicate the underlying cause in more detail. errnum The error value to be decoded, usually obtained from the errno global variable just before this function is called. This is neces- sary if you need to call any code between the system call to be explained and this function, because many libc functions will alter the value of errno. format The original format, exactly as passed to the printf(3) system call. Returns: The message explaining the error. This message buffer is shared by all libexplain functions which do not supply a buffer in their argument list. This will be overwritten by the next call to any libexplain function which shares this buffer, including other threads. Note: This function is not thread safe, because it shares a return buffer across all threads, and many other functions in this library. Example: This function is intended to be used in a fashion similar to the following example: errno = 0; int result = printf(format); if (result < 0 && errno != 0) { int err = errno; fprintf(stderr, "%s ", explain_errno_printf(err, format)); exit(EXIT_FAILURE); } The above code example is available pre-packaged as the explain_printf_or_die(3) function. explain_message_printf void explain_message_printf(char *message, int message_size, const char *format); The explain_message_printf function is used to obtain an explanation of an error returned by the printf(3) system call. The least the mes- sage will contain is the value of strerror(errno), but usually it will do much better, and indicate the underlying cause in more detail. The errno global variable will be used to obtain the error value to be decoded. message The location in which to store the returned message. If a suitable message return buffer is supplied, this function is thread safe. message_size The size in bytes of the location in which to store the returned message. format The original format, exactly as passed to the printf(3) system call. Example: This function is intended to be used in a fashion similar to the following example: errno = 0; int result = printf(format); if (result < 0 && errno != 0) { char message[3000]; explain_message_printf(message, sizeof(message), format); fprintf(stderr, "%s ", message); exit(EXIT_FAILURE); } The above code example is available pre-packaged as the explain_printf_or_die(3) function. explain_message_errno_printf void explain_message_errno_printf(char *message, int message_size, int errnum, const char *format); The explain_message_errno_printf function is used to obtain an explanation of an error returned by the printf(3) system call. The least the message will contain is the value of strerror(errno), but usually it will do much better, and indicate the underlying cause in more detail. message The location in which to store the returned message. If a suitable message return buffer is supplied, this function is thread safe. message_size The size in bytes of the location in which to store the returned message. errnum The error value to be decoded, usually obtained from the errno global variable just before this function is called. This is neces- sary if you need to call any code between the system call to be explained and this function, because many libc functions will alter the value of errno. format The original format, exactly as passed to the printf(3) system call. Example: This function is intended to be used in a fashion similar to the following example: errno = 0; int result = printf(format); if (result < 0 && errno != 0) { int err = errno; char message[3000]; explain_message_errno_printf(message, sizeof(message), err, format); fprintf(stderr, "%s ", message); exit(EXIT_FAILURE); } The above code example is available pre-packaged as the explain_printf_or_die(3) function. SEE ALSO
printf(3) formatted output conversion explain_printf_or_die(3) formatted output conversion and report errors COPYRIGHT
libexplain version 0.52 Copyright (C) 2010 Peter Miller explain_printf(3)
All times are GMT -4. The time now is 04:21 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy