LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   shell scripting (https://www.linuxquestions.org/questions/linux-newbie-8/shell-scripting-117369/)

pcdebb 11-18-2003 07:02 AM

shell scripting
 
Ok I now am learning shell scripting. I feel a little held back with how the text is teaching me, and my instructor is basically going from the same book. does anyone have any useful links I can use to get more comfy with shell scripting? if it matters it is/will be in bash.

trickykid 11-18-2003 07:59 AM

Always one of the sites and howto's I use when I need some answers and examples: http://www.tldp.org/LDP/abs/html/

Its the one howto that has tons of examples to use and go from to help out in understanding what the different parts of scripts do, etc.

pcdebb 11-18-2003 08:12 AM

great i'll give it a shot. gonna try to write my first "hello world" tonite ;-)

tiger3 11-18-2003 08:40 AM

Try this site, look at "Contents and Navigation" there you will find the modules look through them to find what you need to help. http://www.upscale.utoronto.ca/Gener...nux/index.html

pcdebb 11-18-2003 12:36 PM

oh geesh all of those links are terribly overwhelming to me. right now i'm looking for simple stuff. for example i'm working on taking user input ,and the input is filenames. i use a command (find or grep) to find the file and if it exists then show the last mod time of the file. when i use grep nothing happens, if i use find i get a hit, so i'm sort of stuck there. i think i'm not using the right options or something? i accept the input fine because right now i echo the input so i know i'm doing that right.

unSpawn 11-18-2003 12:38 PM

Just post what you've got?

pcdebb 11-18-2003 12:47 PM

#accept filename as argument, and show last modification time of file
#if it exists, or show message if it doesnt exist.

echo "enter filename: "
read file1
find $HOME -name $file1 -print


this seems to list the path and file, but i'm confused at getting all the info (the last modified time is what i'm after). reading the chapters seem so easy but getting the script to work is entirely different :(

pcdebb 11-18-2003 12:48 PM

i tried the if then fi statement but that didnt seem to work. at first i had this:

#accept filename as argument, and show last modification time of file
#if it exists, or show message if it doesnt exist.

echo "enter filename: "
read file1

if grep "$1"; then
echo "file found" #here will list mod time
else
echo "that file does not exist"
fi
echo "Last modification time is " #just filler, will get here

Skyline 11-18-2003 12:53 PM

If you've got a list of file names then maybe something like ??

Code:

#!/bin/bash

echo "Please type a list of filenames :"

read -a filenames

for i in ${filenames[*]}

do
............
............
done


pcdebb 11-18-2003 01:14 PM

ok i find reference to find a file that was done x days ago, can i just show when the last time when it was modified or accessed instead of x days? Skyline, that loop didnt work for me :(

I did however just figure out what was wrong with my loop. apparently the book is a misprint as it wasnt listing the syntax correctly

unSpawn 11-18-2003 01:17 PM

# Ask for name, test if var is empty, exit if, else search. The "iname" is case insensitive, the printf prints out the M-time local to you with the full filename behind it. I made it print quotes so it's easy to spot misplaced spaces and if you feed it to something else it won't break on spaces. Null termination (-fprint0 or -print0) do the same:

echo "Enter filename: (will search in "$HOME")"; read file
test -z file && exit 1 || find "$HOME" -type f -iname "${file}" -printf "%t \"%p\"\n"

pcdebb 11-18-2003 01:36 PM

ok now i'm going crazy, i entered the test line in a file and did it and it worked, i insert in in my script and it DOEST wrk and i entered it exact. WHY?!!?!?!?!?!?!?!?

NM. guess it required verbal foul language to make it work :confused:

tiger3 11-18-2003 04:52 PM

Check to make sure your script file is executable.....

ls -l script_name # will tell you if it is -rwxr-xr-x

chmod 755 script_name # will change the parameters

This should fix it if it worked from the command line. Once you create a file you must make it executable then to execute it type

./script_name

pcdebb 11-18-2003 04:53 PM

yes i made it executable by typing

chmod u+x script_name

tiger3 11-18-2003 05:34 PM

Are you trying to allow the user to enter the file name at the prompt?
If so I think you need to remove the newline from the end of echo with the -n option.

echo -n "Enter filename: (will search in "$HOME")"
read file
test -z file && exit 1 || find "$HOME" -type f -iname "${file}" -printf "%t \"%p\"\n"


All times are GMT -5. The time now is 09:21 PM.