Showing posts with label Shell Script. Show all posts
Showing posts with label Shell Script. Show all posts

Tuesday, October 23, 2012

Bash script Varaible scope not working in pipe


I found this thing when I try to cat a file and making string for grep

Example i have a file
test.txt
--------------
a
b
c
d
a
b
c
a
b
c

cat test.txt|sort|uniq -c|while read line; do echo $line; done

Now problem arise when i try to assign some values to already defined variable inside while loop in attempt of using this command in bash script

#!/bin/bash
founded=""

cat test.txt|sort|uniq -c|while read line
do
        if [ $(echo $founded|wc -m) -gt 1 ]
        then
                founded=$founded"\|"$(echo $line|cut -d" " -f2)
        else
                founded=$(echo $line|cut -d" " -f2)
        fi
done

echo $founded

------
My expected output is:a\|b\|c\|d
I got : (blank)


I tried to found out the answer and find this interesting fact from the article http://www.kilala.nl/Sysadmin/index.php?id=741

"Bash spawns a new sub-shell when piping commands together. Since Bash is picky about scoping variables to sub-shells, my script doesn't work like I expected it to."



-----------


To making my script working i applied this trick

#!/bin/bash
founded=$(cat test.txt|sort|uniq -c|while read line
do
        if [ $(echo $founded|wc -m) -gt 1 ]
        then
                founded=$founded"\|"$(echo $line|cut -d" " -f2)
                echo $founded
        else
                founded=$(echo $line|cut -d" " -f2)
                echo $founded
        fi
done|tail -1)

echo $founded


And i got my expected output

Saturday, April 23, 2011

Finding anything for any day other than current(--date parameter of date command)

In Linux when you want to know date you just write "date" command and get current date and by passing parameter you can get any information of today in any format.

But the problem arise when you want to know specific details about any date other than today

For example Today is 23-04-2011 and you want to know the weekday on 05-01-2011.And weekday count on 05-01-2011 like (sun-7,mon-1,tue-2 etc.)

Then you can also know that by date command :)


You just need to use --date parameter of date command

date +%u --date="2011-01-05"

Weekday

date +%A --date="2011-01-05"

Friday, March 18, 2011

Finding count of files according to modification month yearwise

To find count of files in a folder according to their month,i write down a script(i.e. not so awesome script but that do my task easily).

For ex- A folder abcd has 80000 and you know you created files in that folder at 2009 and current year is 2011 then finding count of files according to their month is so easy.

#!/bin/bash
loc=”/home/abcd” #—-This is a location this can be any location
year=”2009|2010|2011″
mon=”Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec”

for((i=1;i<=3;i++))
do
for((j=1;j<=12;j++))
do
year1=$(echo $year|cut -d”|” -f$i)
mon1=$(echo $mon|cut -d”|” -f$j)
echo “$mon1-$year1: $(ls -al –time-style=+%b-%Y $loc|grep “$mon1-$year1″|wc -l)”
done

done


And output like

Jan-2009:200

Feb-2009:488

and so on…