473,386 Members | 1,706 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Shell Script awk problem , please advice

20
Hi, i am currently writing a script to delete the directory named with number.

Example:
in /usr/
i have
/usr/101
/usr/102 ...... /usr/200

This is the command i type and it works, i got the directory listed where the number is <150

Expand|Select|Wrap|Line Numbers
  1. ls -l /usr/ | grep ^d | awk '{ if ( $9 < 150) print $9 }'
So i applied it to the script, and it is not working especially at awk if else comparison, screen printed ALL number (101 -200). I am suspecting the error is in awk command there. Please help.

Expand|Select|Wrap|Line Numbers
  1. #!/bin/bash -xv
  2. COPYKEEP="10"
  3. LATEST="200"
  4. KEEPFROM=`expr $LATEST - $COPYKEEP`
  5.  
  6. if [ $KEEPFROM -gt 0 ]
  7. then
  8.  
  9. # Find which to delete.
  10. ls -l /usr/ | grep ^d |  awk '{ if ( $9 < $KEEPFROM ) print $9 }'
  11. fi
Aug 7 '08 #1
6 3464
ashitpro
542 Expert 512MB
Expand|Select|Wrap|Line Numbers
  1. #!/bin/bash -xv
  2. COPYKEEP="10"
  3. LATEST="200"
  4. KEEPFROM=`expr $LATEST - $COPYKEEP`
  5. echo $KEEPFROM
  6. if [ $KEEPFROM -gt 0 ]
  7. then
  8. # Find which to delete.
  9. ls -l /usr/ | grep ^d |  awk -v p=$KEEPFROM '{ if ( $9 < p ) print $9 }'
  10. fi
  11.  
check the above script...
you can not use shell variables in awk directly..
it needs to be assigned to awk variable and then use it...
in Example we'r assigning it to 'p'
Aug 7 '08 #2
DannyMc
20
It works!!

Thank you so much!
Aug 11 '08 #3
DannyMc
20
Hi again,

i have tried to delete the file in awk using method found via google.

Expand|Select|Wrap|Line Numbers
  1. ls -l /usr1/ | grep ^d | grep -v current | awk -v p=103 '{ if ( $9 < p ) system("rm -rf " "\""$9"\"") }'
This is working , however when i apply this to the script

Expand|Select|Wrap|Line Numbers
  1. echo " ls -l /usr1/ | grep ^d | grep -v current | awk -v p=103 '{ if ( $9 < p ) system("rm -rf " "\""$9"\"") }' "
  2.  
The quoting is very confusing, can anyone help?
Aug 11 '08 #4
ashitpro
542 Expert 512MB
Hi again,

i have tried to delete the file in awk using method found via google.

Expand|Select|Wrap|Line Numbers
  1. ls -l /usr1/ | grep ^d | grep -v current | awk -v p=103 '{ if ( $9 < p ) system("rm -rf " "\""$9"\"") }'
This is working , however when i apply this to the script

Expand|Select|Wrap|Line Numbers
  1. echo " ls -l /usr1/ | grep ^d | grep -v current | awk -v p=103 '{ if ( $9 < p ) system("rm -rf " "\""$9"\"") }' "
  2.  
The quoting is very confusing, can anyone help?


I didn't need any complex quoting...
Next statement worked for me in shell script as well as on command line...

ls -l /usr/ | grep ^d | grep -v current | awk -v p=103 '{ if ( $9 < p ) system("rm -rf /usr/" $9) }'
Aug 11 '08 #5
DannyMc
20
Again, Thank you. It works :)
Aug 12 '08 #6
ghostdog74
511 Expert 256MB
there's actually another less cumbersome way to do it.
Expand|Select|Wrap|Line Numbers
  1. rm -rf /usr/[0-1][0-9][0-9]
  2. rm -rf /usr/200
  3.  
Aug 16 '08 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: new guy | last post by:
Hello I have a problem running following shell command from php exec or system call: exec("/bin/cat $(/bin/cat list.txt) > concatenated.txt"). This works when i run it directly from the shell as...
0
by: Aashif | last post by:
I want to call Unix Shell script which is available in other Server (Unix server) from windows application using C#. Currently the shell script runs the C program but the GUI is not good, So I want...
7
by: Frank Potter | last post by:
I learned some python in windows. And now I've turned to linux. I read a book and it teaches how to write shell script with bash, but I don't feel like the grammar of bash. Since I know about...
2
by: Gerard Flanagan | last post by:
Hello, I have a third party shell script which updates multiple environment values, and I want to investigate (and ultimately capture to python) the environment state after the script has run....
9
by: niteck07 | last post by:
I am using following shell script to ftp files to another server but this is failing as the shell script changes the user name for the ftp login the correct user name is 'ag\invprint' which the...
3
by: telduivel | last post by:
Can someone please help me with this: I have a python script, that at some point calls a linux bash script (.sh). Starting the shell script is the last thing my python script needs to do, so I...
1
by: Svenn Are Bjerkem | last post by:
Hi, as a user on a linux system I am member of the groups "users" and "design" with users as my default group. To controll the accessibility of some parts of the file system, creation of files and...
3
by: mmm | last post by:
I am looking for advice on Python Editors and IDEs I have read other posts and threads on the subject and my two questions at this time are mainly about the IDLE-like F5-run facilities. While I...
7
by: Samuel A. Falvo II | last post by:
I have a shell script script.sh that launches a Java process in the background using the &-operator, like so: #!/bin/bash java ... arguments here ... & In my Python code, I want to invoke...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.