473,503 Members | 1,787 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Shell Script awk problem , please advice

20 New Member
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 3466
ashitpro
542 Recognized Expert Contributor
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 New Member
It works!!

Thank you so much!
Aug 11 '08 #3
DannyMc
20 New Member
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 Recognized Expert Contributor
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 New Member
Again, Thank you. It works :)
Aug 12 '08 #6
ghostdog74
511 Recognized Expert Contributor
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
1736
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
3029
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
2227
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
2829
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
14182
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
5437
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
2354
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
3931
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
6204
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
7199
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7074
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...
1
6982
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5572
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5000
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4667
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3161
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1501
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
731
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.