Sponsored Content
Top Forums UNIX for Beginners Questions & Answers Need a Bash script for iterating thru an array and running a command Post 303026955 by Corona688 on Wednesday 5th of December 2018 01:32:59 PM
Old 12-05-2018
I'm not sure what you're trying to do with your code, you're way overthinking it. All you need is text and a basic loop.

Version which is just text:
Code:
VAR="2567538 2356789"
for x in $VAR # Note VAR is not quoted here
do
        echo "x=$x"
done

People jump straight into bash arrays convinced that everything needs to be an array when much of the time it's not really need at all.

Note how much more complicated the array use is:

Code:
ARR=( 1234 5678 )

for x in "${ARR[@]}" # Note, requires special syntax and quoting
do
        echo "x=$x"
done

This User Gave Thanks to Corona688 For This Post:
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Running function or command concurrently in a bash script

I currently run a script over a vpnc tunnel to back-up my data to a remote server. However for a number of reasons the tunnel often collapses. If I manually restore the tunnel then the whole thing can continue, but I want to add into my script a section whereby while the transfer is taking place,... (8 Replies)
Discussion started by: dj_bridges
8 Replies

2. Shell Programming and Scripting

running bash command inside awk

Org file 192.168.1.10 d:\adir\xdir 192.168.1.11 d:\bdir\ydir want to covert it into robocopy \\192.168.1.10\d$\adir\xdir\log* some_localdir\adir robocopy \\192.168.1.10\d$\adir\ydir\log* some_localdir\bbdir (5 Replies)
Discussion started by: ydk
5 Replies

3. Shell Programming and Scripting

bash Script: Issue with iterating Directory and store into array

Hi all, I am working on a backup based script, in which it enters to a directory and check the sub-directories and copy the names into an array. cd $CPFs k=0 for i in * do if then ARRs="$i" k=$(($k+1)) #echo "$i" ... (19 Replies)
Discussion started by: canishk
19 Replies

4. UNIX for Dummies Questions & Answers

Running set options from the command line and bash command

I'm reading about debugging aids in bash and have come across the set command. It says in my little book that an addition to typing set you can also use them "on the command line when running a script..." and it lists this in a small table: set -o option Command Line... (5 Replies)
Discussion started by: Straitsfan
5 Replies

5. Shell Programming and Scripting

Bash Command To Delete Number from Array

Hi, I am writing a script to split a log file - the log could contain multiple days worth of logs. The second line of the log contains the string "Version ". In my test log which comprises of two days worth of logs, this string appears twice - once each day. Essentially I would like to split... (7 Replies)
Discussion started by: pmurray21
7 Replies

6. Shell Programming and Scripting

Bash Script Iterating Question

I am trying to look through one of my directories to remove certain files. I am pretty new to Unix and bash so I just need a little help in starting this. I know I would have to write two loops one to iterate the directories and one to iterate the files. How would I write the loops? (3 Replies)
Discussion started by: totoro125
3 Replies

7. Shell Programming and Scripting

Question on iterating array elements

Hi, I am trying to do something similar to the for loop example from KSH For Loop Array: Iterate Through Array Values $: cat y.ksh #!/bin/ksh # set array called nameservers set -A nameservers 192.168.1.1 192.168.1.5 202.54.1.5 # print all name servers for i in ${nameservers} do ... (3 Replies)
Discussion started by: newbie_01
3 Replies

8. UNIX for Beginners Questions & Answers

Array problem in Bash Script

I am trying to write a Bash Script using a couple of arrays. I need to perform a countdown of sorts on an array done once daily, but each day would start with the numbers from the previous day. This is what I'm starting with : #!/bin/bash days=(9 8 7 6 5) for (( i = 0 ; i < ${#days} ; i++... (4 Replies)
Discussion started by: cogiz
4 Replies

9. Shell Programming and Scripting

Array compare bash script

Hello, i have a script that should compare between ${ARRAY} that contains all fstab record like this : >>echo ${ARRAY} / /boot between all mountpoints in my df that is stord in ${ARRAY2} >>echo ${ARRAY2} / /boot /dev/shm /var/spool/asterisk/monitor now i have this loop: for i in... (6 Replies)
Discussion started by: batchenr
6 Replies

10. Shell Programming and Scripting

Problem running plsql using printf command on bash shell

I am running plsql using printf on a shell, but i am getting some strange error, can someone point what exactly am i missing, $ echo $SHELL /bin/bash $ printf " > SET serveroutput ON trimspool on feed off echo off > declare > p_val number; > d_val varchar2(10); > begin > SELECT... (1 Reply)
Discussion started by: kamauv234
1 Replies
MIB2C.CONF(5)							     Net-SNMP							     MIB2C.CONF(5)

NAME
mib2c.conf -- How to write mib2c.conf files to do ANYTHING based on MIB input. SYNOPSIS
% cat > mib2c.test.conf << EOF @foreach $t table@ Starting table $t @foreach $c column@ echo $t has column $c which has a syntax of $c.syntax @end@ @end@ EOF % mib2c -c mib2c.test.conf internet DESCRIPTION
The mib2c.conf script language is a MIB-particular language designed to easily process MIB nodes in ways that you want. mib2c is a mis- nomer (for historical purposes), because you can produce anything (not just C code). Look in the Net-SNMP "local" directory for a bunch of example mib2c.*.conf files and behold the power before you. COMMANDS
All commands within mib2c.conf files are embraced by @ signs. Anything with an @ sign at the front and back of the line is generally sup- posed to be a mib2c specific command. These are detailed here: @open FILE@ writes generated output to FILE note that for file specifications, opening '-' will print to stdout. @append FILE@ appends the given FILE @close FILE@ closes the given FILE @push@ save the current outputs, then clear outputs. Use with @open@ and @pop@ to write to a new file without interfering with current out- puts. @pop@ pop up the process() stack one level. Use after a @push@ to return to the previous set of open files. @foreach $VAR scalar@ repeat iterate over code until @end@ setting $VAR to all known scalars @foreach $VAR table@ repeat iterate over code until @end@ setting $VAR to all known tables @foreach $VAR column@ repeat iterate over code until @end@ setting $VAR to all known columns within a given table. Obviously this must be called within a foreach-table clause. @foreach $VAR nonindex@ repeat iterate over code until @end@ setting $VAR to all known non-index columns within a given table. Obviously this must be called within a foreach-table clause. @foreach $VAR internalindex@ repeat iterate over code until @end@ setting $VAR to all known internal index columns within a given table. Obviously this must be called within a foreach-table clause. @foreach $VAR externalindex@ repeat iterate over code until @end@ setting $VAR to all known external index columns within a given table. Obviously this must be called within a foreach-table clause. @foreach $VAR index@ repeat iterate over code until @end@ setting $VAR to all known indexes within a given table. Obviously this must be called within a foreach-table clause. @foreach $VAR notifications@ repeat iterate over code until @end@ setting $VAR to all known notifications @foreach $VAR varbinds@ repeat iterate over code until @end@ setting $VAR to all known varbinds Obviously this must be called within a foreach-notifications clause. @foreach $LABEL, $VALUE enum@ repeat iterate over code until @end@ setting $LABEL and $VALUE to the label and values from the enum list. @foreach $RANGE_START, $RANGE_END range NODE@ repeat iterate over code until @end@ setting $RANGE_START and $RANGE_END to the legal accepted range set for a given mib NODE. @foreach $var stuff a b c d@ repeat iterate over values a, b, c, d as assigned generically (ie, the values are taken straight from the list with no mib-expan- sion, etc). @while expression@ repeat iterate over code until the expression is false @eval $VAR = expression@ evaluates expression and assigns the results to $VAR. This is not a full perl eval, but sort of a ""psuedo"" eval useful for simple expressions while keeping the same variable name space. See below for a full-blown export to perl. @perleval STUFF@ evaluates STUFF directly in perl. Note that all mib2c variables interpereted within .conf files are in $vars{NAME} and that a warn- ing will be printed if STUFF does not return 0. (adding a 'return 0;' at the end of STUFF is a workaround. @startperl@ @endperl@ treats everything between these tags as perl code, and evaluates it. @next@ restart foreach; should only be used inside a conditional. skips out of current conditional, then continues to skip to end for the current foreach clause. @if expression@ evaluates expression, and if expression is true processes contained part until appropriate @end@ is reached. If the expression is false, the next @elsif expression@ expression (if it exists) will be evaluated, until an expression is true. If no such expression exists and an @else@ clause is found, it will be evaluated. @ifconf file@ If the specified file can be found in the conf file search path, and if found processes contained part until an appropriate @end@ is found. As with a regular @if expression@, @elsif expression@ and @else@ can be used. @ifdir dir@ If the specified directory exists, process contained part until an appropriate @end@ is found. As with a regular @if expression@, @elsif expression@ and @else@ can be used. @define NAME@ @enddefine@ Memorizes ""stuff"" between the define and enddefine tags for later calling as NAME by @calldefine NAME@. @calldefine NAME@ Executes stuff previously memorized as NAME. @printf "expression" stuff1, stuff2, ...@ Like all the other printf's you know and love. @run FILE@ Sources the contents of FILE as a mib2c file, but does not affect current files opened. @include FILE@ Sources the contents of FILE as a mib2c file and appends its output to the current output. @prompt $var QUESTION@ Presents the user with QUESTION, expects a response and puts it in $var @print STUFF@ Prints stuff directly to the users screen (ie, not to where normal mib2c output goes) @quit@ Bail out (silently) @exit@ Bail out! VARIABLES
Variables in the mib2c language look very similar to perl variables, in that they start with a "$". They can be used for anything you want, but most typically they'll hold mib node names being processed by @foreach ...@ clauses. They also have a special properties when they are a mib node, such that adding special suffixes to them will allow them to be interpreted in some fashion. The easiest way to understand this is through an example. If the variable 'x' contained the word 'ifType' then some mag- ical things happen. In mib2c output, anytime $x is seen it is replaced with "ifType". Additional suffixes can be used to get other aspects of that mib node though. If $x.objectID is seen, it'll be replaced by the OID for ifType: ".1.3.6.1.2.1.2.2.1.3". Other suffixes that can appear after a dot are listed below. One last thing: you can use things like $vartext immediately ending in some other text, you can use {}s to get proper expansion of only part of the mib2c input. IE, $xtext will produce "$xtext", but ${x}text will produce "ifTypetext" instead. $var.uc all upper case version of $var $var.objectID dotted, fully-qualified, and numeric OID $var.commaoid comma separated numeric OID for array initialization $var.oidlength length of the oid $var.subid last number component of oid $var.module MIB name that the object comes from $var.parent contains the label of the parent node of $var. $var.isscalar returns 1 if var contains the name of a scalar $var.iscolumn returns 1 if var contains the name of a column $var.children returns 1 if var has children $var.perltype node's perl SYNTAX ($SNMP::MIB{node}{'syntax'}) $var.type node's ASN_XXX type (Net-SNMP specific #define) $var.decl C data type (char, u_long, ...) $var.readable 1 if an object is readable, 0 if not $var.settable 1 if an object is writable, 0 if not $var.creatable 1 if a column object can be created as part of a new row, 0 if not $var.noaccess 1 if not-accessible, 0 if not $var.accessible 1 if accessible, 0 if not $var.storagetype 1 if an object is a StorageType object, 0 if not $var.rowstatus 1 if an object is a RowStatus object, 0 if not 'settable', 'creatable', 'lastchange', 'storagetype' and 'rowstatus' can also be used with table variables to indicate whether it contains writable, creatable, LastChange, StorageType or RowStatus column objects $var.hasdefval returns 1 if var has a DEFVAL clause $var.defval node's DEFVAL $var.hashint returns 1 if var has a HINT clause $var.hint node's HINT $var.ranges returns 1 if var has a value range defined $var.enums returns 1 if var has enums defined for it. $var.access node's access type $var.status node's status $var.syntax node's syntax $var.reference node's reference $var.description node's description SEE ALSO
mib2c(1) VVERSIONINFO
28 Apr 2004 MIB2C.CONF(5)
All times are GMT -4. The time now is 11:17 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy