Overclock.net › Forums › Software, Programming and Coding › Operating Systems › Linux, Unix › Help with Shell Scripting!
New Posts  All Forums:Forum Nav:

Help with Shell Scripting! - Page 3

post #21 of 25
Thread Starter 
Quote:
Originally Posted by Plan9 View Post

You entered the commend wrong then (or didn't have any *.pdf files in your current directory). I promise you, ls does work, I use it every day and encourage you to try my example I posted.

ls is the correct way of doing what you want to do. I mean, sure grep works for you, and with shell scripting there's almost always multiple ways of writing the same code - but ls is the "correct" way because:
  1. you're using the command that was designed to output directory listings and using it in a method that it was designed to be used
  2. the ls method is standard POSIX, thus it will work across all Linux distros, BSD, Solaris and so on. While the -l switch for grep is also part of POSIX, some OS's (eg Solaris) don't like a zero length string as their pattern matching
  3. ls runs quicker - particularly with larger files (grep opens files and then reads the file line by line. ls only lists the files in the directory
  4. your grep method produces unwanted text when handling directories (grep: xxx.pdf: Is a directory) - albeit you're not redirecting the stderr in your code, but it's still very messy.
  5. and lastly, it's really bad practice to use grep against binary files (such as PDFs) as it's a text line matching command (ie it's only supposed to be run against ASCII / Unicode files). Case in point, run the same grep command without the -l and you'll get the following output: Binary file xxx.pdf matches

I appreciate you're new to this, that other students are giving you mixed advice and that your code seems to be working, so my advice might seem confusing, but I'm a UNIX and Linux administrator by trade so I write shell scripts professionally smile.gif

Well I just found that "find" works.. So yeah thumb.gif
 
Seven Seven Five
(12 items)
 
NASSY
(5 items)
 
CPUMotherboardGraphicsRAM
Convertible i5 3570k (De-Lidded) Gigabyte Z77x-D3H XFX R9 290 With Koolance Block (1179/1450) 16GB G-Skill Ares 1866 
Hard DriveHard DriveOptical DriveCooling
Samsung 840 Evo Seagate Barracuda 2TB 1x OEM DVD Drives Heatkiller Rev 3.0 
CoolingCoolingCoolingCooling
XSPC RX360 Laing D5 + XSPC Res Primochill Bloodshed Red Tubing Koolance Fullcover R9 290x Block 
CoolingOSMonitorMonitor
XSPC EX360 Windows 10 Pro x64 Asus VEH228H Asus VE228H 
MonitorKeyboardPowerCase
Aavara TS742 Dual Monitor Stand Ducky Shine II Cherry Blue RED Backlit Mechanic... Silverstone Strider 850W Thermaltake Core x71 Tempered Glass Riing Edition 
MouseMouse PadAudioOther
Zowie FK1 House of Stealth Microlab Solo6c Logitech C100 Webcam 
CPUGraphicsRAMHard Drive
Celeron N3160 HD Graphics 400 4GB DDR3 4TB WD Red 
Cooling
Some 120mm fan 
  hide details  
Reply
 
Seven Seven Five
(12 items)
 
NASSY
(5 items)
 
CPUMotherboardGraphicsRAM
Convertible i5 3570k (De-Lidded) Gigabyte Z77x-D3H XFX R9 290 With Koolance Block (1179/1450) 16GB G-Skill Ares 1866 
Hard DriveHard DriveOptical DriveCooling
Samsung 840 Evo Seagate Barracuda 2TB 1x OEM DVD Drives Heatkiller Rev 3.0 
CoolingCoolingCoolingCooling
XSPC RX360 Laing D5 + XSPC Res Primochill Bloodshed Red Tubing Koolance Fullcover R9 290x Block 
CoolingOSMonitorMonitor
XSPC EX360 Windows 10 Pro x64 Asus VEH228H Asus VE228H 
MonitorKeyboardPowerCase
Aavara TS742 Dual Monitor Stand Ducky Shine II Cherry Blue RED Backlit Mechanic... Silverstone Strider 850W Thermaltake Core x71 Tempered Glass Riing Edition 
MouseMouse PadAudioOther
Zowie FK1 House of Stealth Microlab Solo6c Logitech C100 Webcam 
CPUGraphicsRAMHard Drive
Celeron N3160 HD Graphics 400 4GB DDR3 4TB WD Red 
Cooling
Some 120mm fan 
  hide details  
Reply
post #22 of 25
Quote:
Originally Posted by Matt-Matt View Post

Well I just found that "find" works.. So yeah thumb.gif

find is a different tool again.
  • find is used for searching for files within a directory hierarchy (like with the Windows find file tools, you can search for files with the find command based on file size and age of the file as well as the file name). In fact I think back on page 1 I suggested using find with the creation time matching as a possible alternative approach to writing your shell script.
  • ls is there just to display the content of a directory.

Those differences might seem subtle, but it makes a lot of difference when you start working with the command line every day smile.gif
post #23 of 25
Thread Starter 
Quote:
Originally Posted by Plan9 View Post

find is a different tool again.
  • find is used for searching for files within a directory hierarchy (like with the Windows find file tools, you can search for files with the find command based on file size and age of the file as well as the file name). In fact I think back on page 1 I suggested using find with the creation time matching as a possible alternative approach to writing your shell script.
  • ls is there just to display the content of a directory.

Those differences might seem subtle, but it makes a lot of difference when you start working with the command line every day smile.gif

Yeah I do understand the differences, I'll try 'ls' because find throws out an error when no *.pdf is found and I can't send that to /dev/null 2>&1 as it won't save the contents of the file and then it will always display that f1.pdf or whatever has been created until it is destroyed.

EDIT: ls throws out the same error.. Hmm
Anyway that one is done for now, I still have two more unix assignments that are due at 3 tomorrow! wink.gif
Got a mate getting up at like 3:30AM and we are gonna skype thumb.gif He's alright with Linux
Edited by Matt-Matt - 5/29/13 at 7:48am
 
Seven Seven Five
(12 items)
 
NASSY
(5 items)
 
CPUMotherboardGraphicsRAM
Convertible i5 3570k (De-Lidded) Gigabyte Z77x-D3H XFX R9 290 With Koolance Block (1179/1450) 16GB G-Skill Ares 1866 
Hard DriveHard DriveOptical DriveCooling
Samsung 840 Evo Seagate Barracuda 2TB 1x OEM DVD Drives Heatkiller Rev 3.0 
CoolingCoolingCoolingCooling
XSPC RX360 Laing D5 + XSPC Res Primochill Bloodshed Red Tubing Koolance Fullcover R9 290x Block 
CoolingOSMonitorMonitor
XSPC EX360 Windows 10 Pro x64 Asus VEH228H Asus VE228H 
MonitorKeyboardPowerCase
Aavara TS742 Dual Monitor Stand Ducky Shine II Cherry Blue RED Backlit Mechanic... Silverstone Strider 850W Thermaltake Core x71 Tempered Glass Riing Edition 
MouseMouse PadAudioOther
Zowie FK1 House of Stealth Microlab Solo6c Logitech C100 Webcam 
CPUGraphicsRAMHard Drive
Celeron N3160 HD Graphics 400 4GB DDR3 4TB WD Red 
Cooling
Some 120mm fan 
  hide details  
Reply
 
Seven Seven Five
(12 items)
 
NASSY
(5 items)
 
CPUMotherboardGraphicsRAM
Convertible i5 3570k (De-Lidded) Gigabyte Z77x-D3H XFX R9 290 With Koolance Block (1179/1450) 16GB G-Skill Ares 1866 
Hard DriveHard DriveOptical DriveCooling
Samsung 840 Evo Seagate Barracuda 2TB 1x OEM DVD Drives Heatkiller Rev 3.0 
CoolingCoolingCoolingCooling
XSPC RX360 Laing D5 + XSPC Res Primochill Bloodshed Red Tubing Koolance Fullcover R9 290x Block 
CoolingOSMonitorMonitor
XSPC EX360 Windows 10 Pro x64 Asus VEH228H Asus VE228H 
MonitorKeyboardPowerCase
Aavara TS742 Dual Monitor Stand Ducky Shine II Cherry Blue RED Backlit Mechanic... Silverstone Strider 850W Thermaltake Core x71 Tempered Glass Riing Edition 
MouseMouse PadAudioOther
Zowie FK1 House of Stealth Microlab Solo6c Logitech C100 Webcam 
CPUGraphicsRAMHard Drive
Celeron N3160 HD Graphics 400 4GB DDR3 4TB WD Red 
Cooling
Some 120mm fan 
  hide details  
Reply
post #24 of 25
Quote:
Originally Posted by Matt-Matt View Post

Yeah I do understand the differences, I'll try 'ls' because find throws out an error when no *.pdf is found and I can't send that to /dev/null 2>&1 as it won't save the contents of the file and then it will always display that f1.pdf or whatever has been created until it is destroyed.

with regards to the error, you can send the output to a file and the error to devnull:
Code:
command > /output.filename 2> /dev/null

Basically going back to when i was talking about stdout and stderr; > sends stdout somewhere and 2> send stderr somewhere. stdout is the normal console output. stderr is error messages (this is decided by the developer writing the commands, so you will find exceptions where the developer decides he wants errors to goto stdout - which is very annoying).

so with your command it would be:
Code:
find . -iname "*.pdf" > before.tmp 2> /dev/null

What 2>&1 does is it redirects the stderr output to the same stream as stdout. Which is why you were having problems directing errors one way and standard output another when using 2>&1
post #25 of 25
Thread Starter 
Quote:
Originally Posted by Plan9 View Post

with regards to the error, you can send the output to a file and the error to devnull:
Code:
command > /output.filename 2> /dev/null

Basically going back to when i was talking about stdout and stderr; > sends stdout somewhere and 2> send stderr somewhere. stdout is the normal console output. stderr is error messages (this is decided by the developer writing the commands, so you will find exceptions where the developer decides he wants errors to goto stdout - which is very annoying).

so with your command it would be:
Code:
find . -iname "*.pdf" > before.tmp 2> /dev/null

What 2>&1 does is it redirects the stderr output to the same stream as stdout. Which is why you were having problems directing errors one way and standard output another when using 2>&1

Okay, that makes sense now. I won't remember that for a while but I'll get it one day! thumb.gif
+Repppitty rep

EDIT: oh what does iname do? I forgot to ask tongue.gif
Edited by Matt-Matt - 5/29/13 at 7:52am
 
Seven Seven Five
(12 items)
 
NASSY
(5 items)
 
CPUMotherboardGraphicsRAM
Convertible i5 3570k (De-Lidded) Gigabyte Z77x-D3H XFX R9 290 With Koolance Block (1179/1450) 16GB G-Skill Ares 1866 
Hard DriveHard DriveOptical DriveCooling
Samsung 840 Evo Seagate Barracuda 2TB 1x OEM DVD Drives Heatkiller Rev 3.0 
CoolingCoolingCoolingCooling
XSPC RX360 Laing D5 + XSPC Res Primochill Bloodshed Red Tubing Koolance Fullcover R9 290x Block 
CoolingOSMonitorMonitor
XSPC EX360 Windows 10 Pro x64 Asus VEH228H Asus VE228H 
MonitorKeyboardPowerCase
Aavara TS742 Dual Monitor Stand Ducky Shine II Cherry Blue RED Backlit Mechanic... Silverstone Strider 850W Thermaltake Core x71 Tempered Glass Riing Edition 
MouseMouse PadAudioOther
Zowie FK1 House of Stealth Microlab Solo6c Logitech C100 Webcam 
CPUGraphicsRAMHard Drive
Celeron N3160 HD Graphics 400 4GB DDR3 4TB WD Red 
Cooling
Some 120mm fan 
  hide details  
Reply
 
Seven Seven Five
(12 items)
 
NASSY
(5 items)
 
CPUMotherboardGraphicsRAM
Convertible i5 3570k (De-Lidded) Gigabyte Z77x-D3H XFX R9 290 With Koolance Block (1179/1450) 16GB G-Skill Ares 1866 
Hard DriveHard DriveOptical DriveCooling
Samsung 840 Evo Seagate Barracuda 2TB 1x OEM DVD Drives Heatkiller Rev 3.0 
CoolingCoolingCoolingCooling
XSPC RX360 Laing D5 + XSPC Res Primochill Bloodshed Red Tubing Koolance Fullcover R9 290x Block 
CoolingOSMonitorMonitor
XSPC EX360 Windows 10 Pro x64 Asus VEH228H Asus VE228H 
MonitorKeyboardPowerCase
Aavara TS742 Dual Monitor Stand Ducky Shine II Cherry Blue RED Backlit Mechanic... Silverstone Strider 850W Thermaltake Core x71 Tempered Glass Riing Edition 
MouseMouse PadAudioOther
Zowie FK1 House of Stealth Microlab Solo6c Logitech C100 Webcam 
CPUGraphicsRAMHard Drive
Celeron N3160 HD Graphics 400 4GB DDR3 4TB WD Red 
Cooling
Some 120mm fan 
  hide details  
Reply
New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Linux, Unix
Overclock.net › Forums › Software, Programming and Coding › Operating Systems › Linux, Unix › Help with Shell Scripting!