Sponsored Content
Top Forums Shell Programming and Scripting how to get the variable in while loop outside of it? Post 302571231 by BMDan on Sunday 6th of November 2011 11:38:05 PM
Old 11-07-2011
Code:
bash-3.00$ cat compare.sh
#NOTE : 1.Filename must not have space for the script to work.
#       2.Script wont work if symlinks are there instead of proper filenames.

#!/bin/bash
#set -x

#############################VERIFYING THE EXISTENCE OF DUPLICATE FILENAMES IN THE SUPPLIED FILENAMES ###############

if [[ $# -lt 1 ]];then
echo "Usage : `basename $0` filename" 1>&2
exit 2
fi
awk '{f=$9;
        for(i=10;i<=NF;i++)
           f=f&quot; &quot;$i;
           d[f]++;
     }
END{
        for(f in d)
                if(d[f] > 1)
                 printf(&quot;file %s occurs %d times\n&quot;,f,d[f]);
                                                    }' `basename $1`
echo -e  &quot;Please remove the dupliacte entries,\
\nkeep the file with the latest timestamp and check again&quot;

################################CHECKING SIZE,DATE,TIME,FILENAME WITH THE EXISTING FILES############################

status=0
cat $1 | awk '{print  $(NF-4),&quot;\t&quot;,$(NF-3),&quot;\t&quot;,$(NF-2),&quot;\t&quot;,$(NF-1),&quot;\t&quot;,$(NF)}'  | \
while read size mon day time filename; do
    if [[ ! -f &quot;$filename&quot; ]]; then
        echo &quot;ERROR: no such file: $filename&quot; >&2
        status=1
    else
        filesize=$(ls -lrt &quot;$filename&quot; | awk '{print $5}')
        if [[ &quot;$size&quot; != &quot;$filesize&quot; ]]; then
            echo &quot;ERROR: size mismatch: $filename&quot; >&2
            status=1
        else
           # filetime=$(ls -lrt &quot;$filename&quot; | awk  '{print $6,&quot; &quot;,$7,&quot; &quot;,$8}')
                actual_mon=$(ls -lrt &quot;$filename&quot; | awk '{print $6}')
                actual_day=$(ls -lrt &quot;$filename&quot; | awk '{print $7}')
                actual_time=$(ls -lrt &quot;$filename&quot; | awk '{print $8}')
            if [[ &quot;$mon $day $time&quot; != &quot;$actual_mon $actual_day $actual_time&quot; ]]; then
                echo &quot;ERROR: date mismatch: $filename&quot; >&2
                status=1
            fi
        fi
    fi
done
echo $status
if [[ &quot;$status&quot; -eq 0 ]];then
echo -e &quot;\t\n\nSuccess! All the files are found and their size  and timestamp is also matching with the existing files.&quot;
else
echo -e &quot;\t\n\nFailure! Non-matching files are found or file doesn't exist.&quot;
fi

Check out Variable Scope in Bash (NuclearDonkey.net) for one of several possible solutions here. Essentially, the issue is that you're modifying variables in a sub-process. You'll either need some inter-process trickery, or you'll have to modify your code to eliminate the direct use of the pipeline.

Also, please use [code] tags, as they make things much, much easier to read.
This User Gave Thanks to BMDan For This Post:
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

variable in a for loop

Hi... i am trying to ping my servers .The hostnames are present in a file .They are separated by spaces in the file . i am doing the following : a=1 for name in $(cat host2 |cut -d" " -f$a) do echo Pinging server $name ping -c5 $name a=$a+1 done It is... (3 Replies)
Discussion started by: sars
3 Replies

2. Shell Programming and Scripting

loop through variable

i have these data in a variable "$test" BUNGA TERATAI 3 5055 ITH 1 1 JADE TRADER 143W ITH 4 4 MOL SPLENDOR 0307A ITH 3 3 the red coloured are the ones that i want to take and compare what I need to do is to take out the red coloured and then compare to another variable, if same then... (0 Replies)
Discussion started by: finalight
0 Replies

3. Shell Programming and Scripting

For loop Variable

Hi, Is it possible to assign one variable to other. eg. v1="table1" v2="20000" I want table1 to assign 20000 table1=20000 Is there any way? Thanks (2 Replies)
Discussion started by: SushilM
2 Replies

4. Shell Programming and Scripting

Help with variable using loop

Hi I have webserver that I do read data from. Data are stored like this: Huston |1 Portland |2 Hazen |1 Minneapolis |4 Albany |1 Pittsburg |1 Albany |1 Huston |1 Portland|1 Hazen |2 Albany |2 Huston |1 Hazen |1 Script #!/bin/sh user="admin" (1 Reply)
Discussion started by: Jotne
1 Replies

5. Shell Programming and Scripting

For loop with one variable as the name of other

Hello all, I find it hard to explain what I need so I will post the code OVZINCLUDE="16810 16811 1689" PLUS_16810="test" PLUS_16811="test" for VPS in $OVZINCLUDE do echo "Dumping VPSes: $OVZINCLUDE " vzdump --compress --snapshot ${PLUS_$VPS} $VPS done ... (2 Replies)
Discussion started by: click
2 Replies

6. Shell Programming and Scripting

[SHELL: /bin/sh] For loop using variable variable names

Simple enough problem I think, I just can't seem to get it right. The below doesn't work as intended, it's just a function defined in a much larger script: CheckValues() { for field in \ Group_ID \ Group_Title \ Rule_ID \ Rule_Severity \ ... (2 Replies)
Discussion started by: Vryali
2 Replies

7. Shell Programming and Scripting

printing variable with variable suffix through loop

I have a group of variables myLINEcnt1 - myLINEcnt10. I'm trying to printout the values using a for loop. I am at the head banging stage since i'm sure it has to be a basic syntax issue that i can't figure out. For myIPgrp in 1 2 3 4 5 6 7 8 9 10; do here i want to output the value of... (4 Replies)
Discussion started by: oly_r
4 Replies

8. UNIX for Dummies Questions & Answers

Loop and variable not exactly variable: what's wrong

Hello guys, This truly is a newbie question. I'm trying to make a loop to execute simultaneous commands indefinitely while using variable. Here is how my mess looks like (this is just an example): #!/bin/bash IP=`shuf -n 1 IP.txt` # I figured this would be easier to select random lines... (4 Replies)
Discussion started by: bobylapointe
4 Replies

9. Shell Programming and Scripting

Array Variable being Assigned Values in Loop, But Gone when Loop Completes???

Hello All, Maybe I'm Missing something here but I have NOOO idea what the heck is going on with this....? I have a Variable that contains a PATTERN of what I'm considering "Illegal Characters". So what I'm doing is looping through a string containing some of these "Illegal Characters". Now... (5 Replies)
Discussion started by: mrm5102
5 Replies

10. Shell Programming and Scripting

[Solved] How to increment and add variable length numbers to a variable in a loop?

Hi All, I have a file which has hundred of records with fixed number of fields. In each record there is set of 8 characters which represent the duration of that activity. I want to sum up the duration present in all the records for a report. The problem is the duration changes per record so I... (5 Replies)
Discussion started by: danish0909
5 Replies
SYSPROFILE(8)						      System Manager's Manual						     SYSPROFILE(8)

NAME
sysprofile - modular centralized shell configuration DESCRIPTION
sysprofile is a generic approach to configure shell settings in a modular and centralized way mostly aimed at avoiding work for lazy sysad- mins. It has only been tested to work with the bash shell. It basically consists of the small /etc/sysprofile shell script which invokes other small shell scripts having a .bash suffix which are contained in the /etc/sysprofile.d/ directory. The system administrator can drop in any script he wants without any naming convention other than that the scripts need to have a .bash suffix to enable automagic sourcing by /etc/sysprofile. This mechanism is set up by inserting a small shell routine into /etc/profile for login shells and optionally into /etc/bashrc and/or /etc/bash.bashrc for non-login shells from where the actual /etc/sysprofile script is invoked: if [ -f /etc/sysprofile ]; then . /etc/sysprofile fi For using "sysprofile" under X11, one can source it in a similar way from /etc/X11/Xsession or your X display manager's Xsession file to provide the same shell environment as under the console in X11. See the example files in /usr/share/doc/sysprofile/ for illustration. For usage of terminal emulators with a non-login bash shell under X11, take care to enable sysprofile via /etc/bash.bashrc. If not set this way, your terminal emulators won't come up with the environment defined by the scripts in /etc/sysprofile.d/. Users not wanting /etc/sysprofile to be sourced for their environment can easily disable it's automatic mechanism. It can be disabled by simply creating an empty file called $HOME/.nosysprofile in the user's home directory using e.g. the touch(1) command. Any single configuration file in /etc/sysprofile.d/ can be overridden by any user by creating a private $HOME/.sysprofile.d/ directory which may contain a user's own version of any configuration file to be sourced instead of the system default. It's names have just to match exactly the system's default /etc/sysprofile.d/ configuration files. Empty versions of these files contained in the $HOME/.syspro- file.d/ directory automatically disable sourcing of the system wide version. Naturally, users can add and include their own private script inventions to be automagically executed by /etc/sysprofile at login time. OPTIONS
There are no options other than those dictated by shell conventions. Anything is defined within the configuration scripts themselves. SEE ALSO
The README files and configuration examples contained in /etc/sysprofile.d/ and the manual pages bash(1), xdm(1x), xdm.options(5), and wdm(1x). Recommended further reading is everything related with shell programming. If you need a similar mechanism for executing code at logout time check out the related package syslogout(8) which is a very close compan- ion to sysprofile. BUGS
sysprofile in its current form is mainly restricted to bash(1) syntax. In fact it is actually a rather embarrassing quick and dirty hack than anything else - but it works. It serves the practical need to enable a centralized bash configuration until something better becomes available. Your constructive criticism in making this into something better" is very welcome. Before i forget to mention it: we take patches... ;-) AUTHOR
sysprofile was developed by Paul Seelig <pseelig@debian.org> specifically for the Debian GNU/Linux system. Feel free to port it to and use it anywhere else under the conditions of either the GNU public license or the BSD license or both. Better yet, please help to make it into something more worthwhile than it currently is. SYSPROFILE(8)
All times are GMT -4. The time now is 11:02 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy