Some questions re a rather complicated script

For discussions about programming, programming questions/advice, and projects that don't really have anything to do with Puppy.
Post Reply
Message
Author
scsijon
Posts: 1596
Joined: Thu 24 May 2007, 03:59
Location: the australian mallee
Contact:

Some questions re a rather complicated script

#1 Post by scsijon »

Hi, I need a scriptor or two to give me some code hints for a couple of "somewhat complicated" scripts.

I'll give a warning that I can't write a script for nuts, but can usually work out what one does (unless your talking things like
cut -f 1-8 -d '|'`, and cut -f 10-13 -d '|'`, and sed -e 's%^,%%' | tr ',' '\n' | sort -u | tr '\n' ',' | sed -e 's%,$%%', where you loose me as soon as you start!).


thanks
scsijon

What I need to start with is create a script or two to do these functions> :?:

Code: Select all

#!/bin/sh

#script1
# I NEED TO CHECK IF A "FILE" IS A LINK OR NOT
# if /opt/packagenamedirectory/fileorlinkname ISNOT a LINK
# OR IF IT IS AN XML FILE
#   then if /opt/packagenamedirectory/fileorlinkname ISNOT a XMLFILE
# OR IF IT'S NOT A TEXT FILE
#     then if /opt/packagenamedirectory/fileorlink ISNOT a TEXTFILE
#       then "GIVE_A_SCREEN_ERROR_SPLASH" with /opt/packagenamedirectory/fileorlink 
#          AND the SCRIPT_NAME
#          AND await close of splash screen before continuing
# IF IT'S A TEXT FILE IT SHOULDN'T BE IT SHOULD BE A LINK SO FIX IT
#       else delete /opt/packagenamedirectory/fileorlink
#          AND ln -s /opt/packagenamedirectory2/filename /opt/packagenamedirectory/linkname
#       fi 
#     fi
# IF IT IS A XML FILE IT MAY NEED WORK
#     else goto script3
#   fi
# IF IT'S A LINK CHECK IT'S THE RIGHT ONE
#   else goto script2
# fi

Code: Select all

#!/bin/sh

#script2
# if /opt/packagenamedirectory/fileorlinkname ISNOT_LINKED_TO /opt/packagenamedirectory2/a_particular_linkname
#   then  DELETE /opt/packagenamedirectory/fileorlinkname
#      AND ln -s /opt/packagenamedirectory2/filename /opt/packagenamedirectory/linkname
#   if /opt/packagenamedirectory/fileorlinkname ISNOT_LINKED_TO /opt/packagenamedirectory2/a_particular_linkname
#     then  DELETE /opt/packagenamedirectory/fileorlinkname
#      AND ln -s /opt/packagenamedirectory2/filename /opt/packagenamedirectory/linkname
#     if /opt/packagenamedirectory/fileorlinkname ISNOT_LINKED_TO /opt/packagenamedirectory2/a_particular_linkname
#       then  DELETE /opt/packagenamedirectory/fileorlinkname
#        AND ln -s /opt/packagenamedirectory2/filename /opt/packagenamedirectory/linkname
#       if /opt/packagenamedirectory/fileorlinkname ISNOT_LINKED_TO /opt/packagenamedirectory2/a_particular_linkname
#        then  DELETE /opt/packagenamedirectory/fileorlinkname
#        AND ln -s /opt/packagenamedirectory2/filename /opt/packagenamedirectory/linkname
#       fi
#     fi
#   fi
# fi 

Code: Select all

#!/bin/sh

#script3
# this script will contain a number of additions/changes to the xml filesets
# it is not yet worked out what will be in each of them! 
# if_the_filename_is/opt/packagenamedirectory/xmlfilename1 
#    then goto DO_THESE_CHANGES1
#   if_the_filename_is/opt/packagenamedirectory/xmlfilename2 
#     then DO_THESE_CHANGES2
#     if_the_filename_is/opt/packagenamedirectory/xmlfilename3 
#       then DO_THESE_CHANGES3
#       if_the_filename_is/opt/packagenamedirectory/xmlfilename4 
#         then DO_THESE_CHANGES4
#         if_the_filename_is/opt/packagenamedirectory/xmlfilename5 
#           then DO_THESE_CHANGES5
# none or a mixed number of changes may be needed
#         fi
#       fi
#     fi
#   fi
# fi
#
# DO_THESE_CHANGES1
# the changes??
# return
#
# DO_THESE_CHANGES2
# the changes??
# return
#
# DO_THESE_CHANGES3
# the changes??
# return
#
# DO_THESE_CHANGES4
# the changes??
# return
#
# DO_THESE_CHANGES5
# the changes??
# return

User avatar
L18L
Posts: 3479
Joined: Sat 19 Jun 2010, 18:56
Location: www.eussenheim.de/

Re: Some questions re a rather complicated script

#2 Post by L18L »

Code: Select all

#!/bin/sh

f=/opt/packagenamedirectory/fileorlinkname

if [ "`file $f | grep ' link '`" ]; then
 echo "CHECK IT'S THE RIGHT ONE "
else
 echo "Hope that helps"
fi 
You want to do something with an xml file that is not a link or what?
Hope I have not lost you because grep was not in your list.

seaside
Posts: 934
Joined: Thu 12 Apr 2007, 00:19

#3 Post by seaside »

scsijon,

Code: Select all

if [ -L FILENAME ] # if FILENAME is a link
readlink FILENAME # what filename is FILENAME linked to
file FILENAME # what type/kind of file (binary,text,etc) 
Bad news- using the command "file" might require some grep/cut action. If it's just the extension you want, you can do this

Code: Select all

ext="${FILENAME##*.}"
Cheers,
s

scsijon
Posts: 1596
Joined: Thu 24 May 2007, 03:59
Location: the australian mallee
Contact:

#4 Post by scsijon »

thanks folks but what I wrote was an explanation of what I wanted to do not script, no code is implied at this stage! :lol:

User avatar
L18L
Posts: 3479
Joined: Sat 19 Jun 2010, 18:56
Location: www.eussenheim.de/

#5 Post by L18L »

scsijon wrote:thanks folks but what I wrote was an explanation of what I wanted to do not script, no code is implied at this stage! :lol:
A very simple script could result from a description of what you really want in simple plain English. :)
Sorry, but I did not get want you want.

User avatar
technosaurus
Posts: 4853
Joined: Mon 19 May 2008, 01:24
Location: Blue Springs, MO
Contact:

#6 Post by technosaurus »

you definitely want to learn "case"

Code: Select all

case "$FILENAME" in
  *.xml)xml_func "$FILENAME";;
  /root/*)root_files_func "$FILENAME";;
  *doc*)doc_func "$FILENAME";;
  *)default_file_func "$FILENAME";;
esac
this can be used on the results of file

FILERES=$(file ... file_name)
case "$FILERES" in .....
Check out my [url=https://github.com/technosaurus]github repositories[/url]. I may eventually get around to updating my [url=http://bashismal.blogspot.com]blogspot[/url].

Post Reply