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!

post #1 of 25
Thread Starter 
Hey guys, got an assignment to create a unix shell script (bash), I've got it mostly covered! thumb.gif

Just looking at how to create a prompt asking to delete a file, so far I have:
Code:
echo "Delete file" *.c "? (y/n):"

I have absolutely no idea what to add to make it be able to read user input?

Another thing to add is that if there are many c files in the folder it will list them all. I.E it will say "Delete file test1.c test2.c? (y/n), how do I specify which file the echo states? As like I add another file called F3.c and it will read that to a separate prompt, (after asking for test1.c, test2.c and F3.c to be deleted?

I don't want a script written, but how one would do each part. smile.gif
 
Seven Seven Five
(12 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 DriveHard DriveHard Drive
Samsung 840 Evo Seagate Barracuda 2TB Western Digital Green 2TB Samsung Spinpoint 1TB 
Hard DriveOptical DriveCoolingCooling
Western Digital Green 640GB 2x OEM DVD Drives Heatkiller Rev 3.0 XSPC RX360 
CoolingCoolingCoolingOS
Laing D5 + XSPC Res Primochill Bloodshed Red Tubing Koolance Fullcover R9 290x Block Windows 10 Pro x64 
MonitorMonitorMonitorKeyboard
Asus VEH228H Asus VE228H Aavara TS742 Dual Monitor Stand Ducky Shine II Cherry Blue RED Backlit Mechanic... 
PowerCaseMouseMouse Pad
Silverstone Strider 850W HAF 932 Logitech G400 Gigabyte Ghost 
AudioOther
Microlab Solo6c Logitech C100 Webcam 
  hide details  
Reply
 
Seven Seven Five
(12 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 DriveHard DriveHard Drive
Samsung 840 Evo Seagate Barracuda 2TB Western Digital Green 2TB Samsung Spinpoint 1TB 
Hard DriveOptical DriveCoolingCooling
Western Digital Green 640GB 2x OEM DVD Drives Heatkiller Rev 3.0 XSPC RX360 
CoolingCoolingCoolingOS
Laing D5 + XSPC Res Primochill Bloodshed Red Tubing Koolance Fullcover R9 290x Block Windows 10 Pro x64 
MonitorMonitorMonitorKeyboard
Asus VEH228H Asus VE228H Aavara TS742 Dual Monitor Stand Ducky Shine II Cherry Blue RED Backlit Mechanic... 
PowerCaseMouseMouse Pad
Silverstone Strider 850W HAF 932 Logitech G400 Gigabyte Ghost 
AudioOther
Microlab Solo6c Logitech C100 Webcam 
  hide details  
Reply
post #2 of 25
Quote:
Originally Posted by Matt-Matt View Post

Hey guys, got an assignment to create a unix shell script (bash), I've got it mostly covered! thumb.gif

Just looking at how to create a prompt asking to delete a file, so far I have:
Code:
echo "Delete file" *.c "? (y/n):"

I have absolutely no idea what to add to make it be able to read user input?

Another thing to add is that if there are many c files in the folder it will list them all. I.E it will say "Delete file test1.c test2.c? (y/n), how do I specify which file the echo states? As like I add another file called F3.c and it will read that to a separate prompt, (after asking for test1.c, test2.c and F3.c to be deleted?

I don't want a script written, but how one would do each part. smile.gif

the rm command already has an option for prompting. (search the man page for "interactive"). You're far better off using that inside your script instead of trying to recreate it manually. However just for reference, this is how you'd have written such a script:
  • recursion through a list of files: http://www.cyberciti.biz/faq/bash-loop-over-file/
  • y/n prompting: http://ubuntuforums.org/showthread.php?t=531144
  • and lastly your echo command is wrong because you're doing weird things with the quotation marks. What you really need is something like this:
    Code:
    echo -e "Delete file $filename? (y/n): "
    
    The variable $filename can be named anything and basically gets replaced with the value inside the variable at run time.
    Code:
    var="world"
    echo "hello $var"
    # returns: hello world
    
    and by adding the -e switch to echo, you're telling echo not to add a new line after displaying that prompt (which looks prettier when the user comes to type in their selection)

Edited by Plan9 - 5/27/13 at 11:38am
post #3 of 25
Thread Starter 
Quote:
Originally Posted by Plan9 View Post

the rm command already has an option for prompting. (search the man page for "interactive"). You're far better off using that inside your script instead of trying to recreate it manually. However just for reference, this is how you'd have written such a script:
  • recursion through a list of files: http://www.cyberciti.biz/faq/bash-loop-over-file/
  • y/n prompting: http://ubuntuforums.org/showthread.php?t=531144
  • and lastly your echo command is wrong because you're doing weird things with the quotation marks. What you really need is something like this:
    Code:
    echo -e "Delete file $filename? (y/n): "
    
    The variable $filename can be named anything and basically gets replaced with the value inside the variable at run time.
    Code:
    var="world"
    echo "hello $var"
    # returns: hello world
    
    and by adding the -e switch to echo, you're telling echo not to add a new line after displaying that prompt (which looks prettier when the user comes to type in their selection)

Thanks!
Reps to you kind sir! smile.gif
 
Seven Seven Five
(12 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 DriveHard DriveHard Drive
Samsung 840 Evo Seagate Barracuda 2TB Western Digital Green 2TB Samsung Spinpoint 1TB 
Hard DriveOptical DriveCoolingCooling
Western Digital Green 640GB 2x OEM DVD Drives Heatkiller Rev 3.0 XSPC RX360 
CoolingCoolingCoolingOS
Laing D5 + XSPC Res Primochill Bloodshed Red Tubing Koolance Fullcover R9 290x Block Windows 10 Pro x64 
MonitorMonitorMonitorKeyboard
Asus VEH228H Asus VE228H Aavara TS742 Dual Monitor Stand Ducky Shine II Cherry Blue RED Backlit Mechanic... 
PowerCaseMouseMouse Pad
Silverstone Strider 850W HAF 932 Logitech G400 Gigabyte Ghost 
AudioOther
Microlab Solo6c Logitech C100 Webcam 
  hide details  
Reply
 
Seven Seven Five
(12 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 DriveHard DriveHard Drive
Samsung 840 Evo Seagate Barracuda 2TB Western Digital Green 2TB Samsung Spinpoint 1TB 
Hard DriveOptical DriveCoolingCooling
Western Digital Green 640GB 2x OEM DVD Drives Heatkiller Rev 3.0 XSPC RX360 
CoolingCoolingCoolingOS
Laing D5 + XSPC Res Primochill Bloodshed Red Tubing Koolance Fullcover R9 290x Block Windows 10 Pro x64 
MonitorMonitorMonitorKeyboard
Asus VEH228H Asus VE228H Aavara TS742 Dual Monitor Stand Ducky Shine II Cherry Blue RED Backlit Mechanic... 
PowerCaseMouseMouse Pad
Silverstone Strider 850W HAF 932 Logitech G400 Gigabyte Ghost 
AudioOther
Microlab Solo6c Logitech C100 Webcam 
  hide details  
Reply
post #4 of 25
Thread Starter 
How would one compare the contents of two files (with different names of course)
Code:
if [ file1.pdf == file2.pdf ]

This gives me a constant "not equal" as they bear different names..

EDIT: Also how would one make it so with a diff command that it only displays the text changed in a file rather then all the garble rubbish. E.G
Code:
0a1
> f1.txt
But instead show:
Code:
f1.txt

Edited by Matt-Matt - 5/28/13 at 2:53am
 
Seven Seven Five
(12 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 DriveHard DriveHard Drive
Samsung 840 Evo Seagate Barracuda 2TB Western Digital Green 2TB Samsung Spinpoint 1TB 
Hard DriveOptical DriveCoolingCooling
Western Digital Green 640GB 2x OEM DVD Drives Heatkiller Rev 3.0 XSPC RX360 
CoolingCoolingCoolingOS
Laing D5 + XSPC Res Primochill Bloodshed Red Tubing Koolance Fullcover R9 290x Block Windows 10 Pro x64 
MonitorMonitorMonitorKeyboard
Asus VEH228H Asus VE228H Aavara TS742 Dual Monitor Stand Ducky Shine II Cherry Blue RED Backlit Mechanic... 
PowerCaseMouseMouse Pad
Silverstone Strider 850W HAF 932 Logitech G400 Gigabyte Ghost 
AudioOther
Microlab Solo6c Logitech C100 Webcam 
  hide details  
Reply
 
Seven Seven Five
(12 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 DriveHard DriveHard Drive
Samsung 840 Evo Seagate Barracuda 2TB Western Digital Green 2TB Samsung Spinpoint 1TB 
Hard DriveOptical DriveCoolingCooling
Western Digital Green 640GB 2x OEM DVD Drives Heatkiller Rev 3.0 XSPC RX360 
CoolingCoolingCoolingOS
Laing D5 + XSPC Res Primochill Bloodshed Red Tubing Koolance Fullcover R9 290x Block Windows 10 Pro x64 
MonitorMonitorMonitorKeyboard
Asus VEH228H Asus VE228H Aavara TS742 Dual Monitor Stand Ducky Shine II Cherry Blue RED Backlit Mechanic... 
PowerCaseMouseMouse Pad
Silverstone Strider 850W HAF 932 Logitech G400 Gigabyte Ghost 
AudioOther
Microlab Solo6c Logitech C100 Webcam 
  hide details  
Reply
post #5 of 25
Quote:
Originally Posted by Matt-Matt View Post

How would one compare the contents of two files (with different names of course)
Code:
if [ file1.pdf == file2.pdf ]

This gives me a constant "not equal" as they bear different names..

You're going to have to expand a little on this one. diff is used to compare ASCII files - but it looks like you're wanting something quite different as you're pseud-code is checking whether two binary files are the same. So are you able to explain what you're doing a little more please smile.gif
post #6 of 25
Thread Starter 
Quote:
Originally Posted by Plan9 View Post

You're going to have to expand a little on this one. diff is used to compare ASCII files - but it looks like you're wanting something quite different as you're pseud-code is checking whether two binary files are the same. So are you able to explain what you're doing a little more please smile.gif

Basically i'm trying to check for .PDF files that have been created or removed within the past 5 seconds,

So far I have a GREP storing the .pdf's in a file called pdfBEFORE.tmp,
The shell then sleeps for 5 seconds,
Then the next GREP will store the .pdf's that are in the current directory into pdfAFTER.tmp.

pdfBEFORE and pdfAFTER are both just ASCII files, they just have the extension .tmp to denote that they're temp files.

I am stuck at the point in which to decide to print out "No PDF files have been added or removed" from that information.

I also have to implement a way to store the file names so I can make statements such as:

"The following have been added:
file1.pdf
file2.pdf

The following have been removed:
file3.pdf
file4.pdf"

Like I said I have the basic structure of the program working, it is just displaying this information that I am missing and getting it to compare files in an if statement (E.G if the two files are the same, echo this, if not do nothing)
 
Seven Seven Five
(12 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 DriveHard DriveHard Drive
Samsung 840 Evo Seagate Barracuda 2TB Western Digital Green 2TB Samsung Spinpoint 1TB 
Hard DriveOptical DriveCoolingCooling
Western Digital Green 640GB 2x OEM DVD Drives Heatkiller Rev 3.0 XSPC RX360 
CoolingCoolingCoolingOS
Laing D5 + XSPC Res Primochill Bloodshed Red Tubing Koolance Fullcover R9 290x Block Windows 10 Pro x64 
MonitorMonitorMonitorKeyboard
Asus VEH228H Asus VE228H Aavara TS742 Dual Monitor Stand Ducky Shine II Cherry Blue RED Backlit Mechanic... 
PowerCaseMouseMouse Pad
Silverstone Strider 850W HAF 932 Logitech G400 Gigabyte Ghost 
AudioOther
Microlab Solo6c Logitech C100 Webcam 
  hide details  
Reply
 
Seven Seven Five
(12 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 DriveHard DriveHard Drive
Samsung 840 Evo Seagate Barracuda 2TB Western Digital Green 2TB Samsung Spinpoint 1TB 
Hard DriveOptical DriveCoolingCooling
Western Digital Green 640GB 2x OEM DVD Drives Heatkiller Rev 3.0 XSPC RX360 
CoolingCoolingCoolingOS
Laing D5 + XSPC Res Primochill Bloodshed Red Tubing Koolance Fullcover R9 290x Block Windows 10 Pro x64 
MonitorMonitorMonitorKeyboard
Asus VEH228H Asus VE228H Aavara TS742 Dual Monitor Stand Ducky Shine II Cherry Blue RED Backlit Mechanic... 
PowerCaseMouseMouse Pad
Silverstone Strider 850W HAF 932 Logitech G400 Gigabyte Ghost 
AudioOther
Microlab Solo6c Logitech C100 Webcam 
  hide details  
Reply
post #7 of 25
Quote:
Originally Posted by Matt-Matt View Post

Basically i'm trying to check for .PDF files that have been created or removed within the past 5 seconds,

So far I have a GREP storing the .pdf's in a file called pdfBEFORE.tmp,
The shell then sleeps for 5 seconds,
Then the next GREP will store the .pdf's that are in the current directory into pdfAFTER.tmp.

pdfBEFORE and pdfAFTER are both just ASCII files, they just have the extension .tmp to denote that they're temp files.

I am stuck at the point in which to decide to print out "No PDF files have been added or removed" from that information.

I also have to implement a way to store the file names so I can make statements such as:

"The following have been added:
file1.pdf
file2.pdf

The following have been removed:
file3.pdf
file4.pdf"

Like I said I have the basic structure of the program working, it is just displaying this information that I am missing and getting it to compare files in an if statement (E.G if the two files are the same, echo this, if not do nothing)

hmm, that's quite a tricky problem to solve in purely in a shell script (usually when a program reaches that level of complexity then I hop into Perl).
You could probably work around the problem with looping through each line from the cat output of the first file and then greping out lines from then 2nd file. But that would be a pretty messy way to go about it. It might be worth looking at the man pages for grep, diff, sed and a few others to see if there's some lesser used flags that automate a lot of this. But personally I'd change your approach entirely and use find to only display files newer than n minutes (the problem here is that find can't check seconds - so you'll be having to extend the duration of sleep to make that work.
post #8 of 25
Thread Starter 
Quote:
Originally Posted by Plan9 View Post

hmm, that's quite a tricky problem to solve in purely in a shell script (usually when a program reaches that level of complexity then I hop into Perl).
You could probably work around the problem with looping through each line from the cat output of the first file and then greping out lines from then 2nd file. But that would be a pretty messy way to go about it. It might be worth looking at the man pages for grep, diff, sed and a few others to see if there's some lesser used flags that automate a lot of this. But personally I'd change your approach entirely and use find to only display files newer than n minutes (the problem here is that find can't check seconds - so you'll be having to extend the duration of sleep to make that work.

Thought of a totally different workaround!

If I save the output of the "diff" between both files to a .tmp file it will be nothing if there is no difference, otherwise the file size will be greater then 1 byte.

so IF the filesize of the .tmp file is 0 bytes we can assume that there are no differences.

EDIT: Don't worry, will see if my theory works or not! wink.gif

EDIT2: SHE WORKS! biggrin.gif

Gonna rep you buddy! *forgot to*
Edited by Matt-Matt - 5/28/13 at 4:07am
 
Seven Seven Five
(12 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 DriveHard DriveHard Drive
Samsung 840 Evo Seagate Barracuda 2TB Western Digital Green 2TB Samsung Spinpoint 1TB 
Hard DriveOptical DriveCoolingCooling
Western Digital Green 640GB 2x OEM DVD Drives Heatkiller Rev 3.0 XSPC RX360 
CoolingCoolingCoolingOS
Laing D5 + XSPC Res Primochill Bloodshed Red Tubing Koolance Fullcover R9 290x Block Windows 10 Pro x64 
MonitorMonitorMonitorKeyboard
Asus VEH228H Asus VE228H Aavara TS742 Dual Monitor Stand Ducky Shine II Cherry Blue RED Backlit Mechanic... 
PowerCaseMouseMouse Pad
Silverstone Strider 850W HAF 932 Logitech G400 Gigabyte Ghost 
AudioOther
Microlab Solo6c Logitech C100 Webcam 
  hide details  
Reply
 
Seven Seven Five
(12 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 DriveHard DriveHard Drive
Samsung 840 Evo Seagate Barracuda 2TB Western Digital Green 2TB Samsung Spinpoint 1TB 
Hard DriveOptical DriveCoolingCooling
Western Digital Green 640GB 2x OEM DVD Drives Heatkiller Rev 3.0 XSPC RX360 
CoolingCoolingCoolingOS
Laing D5 + XSPC Res Primochill Bloodshed Red Tubing Koolance Fullcover R9 290x Block Windows 10 Pro x64 
MonitorMonitorMonitorKeyboard
Asus VEH228H Asus VE228H Aavara TS742 Dual Monitor Stand Ducky Shine II Cherry Blue RED Backlit Mechanic... 
PowerCaseMouseMouse Pad
Silverstone Strider 850W HAF 932 Logitech G400 Gigabyte Ghost 
AudioOther
Microlab Solo6c Logitech C100 Webcam 
  hide details  
Reply
post #9 of 25
Quote:
Originally Posted by Matt-Matt View Post

Thought of a totally different workaround!

If I save the output of the "diff" between both files to a .tmp file it will be nothing if there is no difference, otherwise the file size will be greater then 1 byte.

so IF the filesize of the .tmp file is 0 bytes we can assume that there are no differences.
as well as the stdin, stdout and stderr streams that you've no doubt already been taught, applications can have a return code - which is an int value. basically all applications should return 0 if there's no errors or any number > than 0 if there are. The return value of the last executed command is stored in the variable named $?

eg
Code:
touch 1 2
diff 1 2 > /dev/null
if [ "$?" -ne 0 ]; then
    echo "different"
else
    echo "same"
fi
# returns "same"

echo "something" > 1
diff 1 2 > /dev/null
if [ "$?" -ne 0 ]; then
    echo "different"
else
    echo "same"
fi
# returns "different"
post #10 of 25
Thread Starter 
Quote:
Originally Posted by Plan9 View Post

as well as the stdin, stdout and stderr streams that you've no doubt already been taught, applications can have a return code - which is an int value. basically all applications should return 0 if there's no errors or any number > than 0 if there are. The return value of the last executed command is stored in the variable named $?

eg
Code:
touch 1 2
diff 1 2 > /dev/null
if [ "$?" -ne 0 ]; then
    echo "different"
else
    echo "same"
fi
# returns "same"

echo "something" > 1
diff 1 2 > /dev/null
if [ "$?" -ne 0 ]; then
    echo "different"
else
    echo "same"
fi
# returns "different"

You lost me, I haven't learned what stout, stdin etc do... ?

Can I PM you what I have so far? I HAD it working, but now it's stuffing up again. (It keeps thinking that there are no new files)
 
Seven Seven Five
(12 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 DriveHard DriveHard Drive
Samsung 840 Evo Seagate Barracuda 2TB Western Digital Green 2TB Samsung Spinpoint 1TB 
Hard DriveOptical DriveCoolingCooling
Western Digital Green 640GB 2x OEM DVD Drives Heatkiller Rev 3.0 XSPC RX360 
CoolingCoolingCoolingOS
Laing D5 + XSPC Res Primochill Bloodshed Red Tubing Koolance Fullcover R9 290x Block Windows 10 Pro x64 
MonitorMonitorMonitorKeyboard
Asus VEH228H Asus VE228H Aavara TS742 Dual Monitor Stand Ducky Shine II Cherry Blue RED Backlit Mechanic... 
PowerCaseMouseMouse Pad
Silverstone Strider 850W HAF 932 Logitech G400 Gigabyte Ghost 
AudioOther
Microlab Solo6c Logitech C100 Webcam 
  hide details  
Reply
 
Seven Seven Five
(12 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 DriveHard DriveHard Drive
Samsung 840 Evo Seagate Barracuda 2TB Western Digital Green 2TB Samsung Spinpoint 1TB 
Hard DriveOptical DriveCoolingCooling
Western Digital Green 640GB 2x OEM DVD Drives Heatkiller Rev 3.0 XSPC RX360 
CoolingCoolingCoolingOS
Laing D5 + XSPC Res Primochill Bloodshed Red Tubing Koolance Fullcover R9 290x Block Windows 10 Pro x64 
MonitorMonitorMonitorKeyboard
Asus VEH228H Asus VE228H Aavara TS742 Dual Monitor Stand Ducky Shine II Cherry Blue RED Backlit Mechanic... 
PowerCaseMouseMouse Pad
Silverstone Strider 850W HAF 932 Logitech G400 Gigabyte Ghost 
AudioOther
Microlab Solo6c Logitech C100 Webcam 
  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!