LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 02-19-2013, 06:53 PM   #46
des_a
Member
 
Registered: Sep 2006
Posts: 301

Original Poster
Rep: Reputation: 18

The problem with script #1, was I needed to run dos2unix on it.
 
Old 02-19-2013, 06:54 PM   #47
des_a
Member
 
Registered: Sep 2006
Posts: 301

Original Poster
Rep: Reputation: 18
mknasosnames.sh

Code:
#! /bin/sh


pushd . > /dev/null


echo Making directories for each operating system...


mkdir ALL
cd ALL
mkdir STANDARD
mkdir OPTIONAL
cd STANDARD
mkdir NORMAL
mkdir SHAREWARE
cd ..
cd OPTIONAL
mkdir NORMAL
mkdir SHAREWARE
cd ..
cd ..


while read -a line
do
 echo Creating ${line[0]}...
 if [ -d ${line[0]} ]; then
  echo There\'s already a directory named ${line[0]}
 else
  mkdir ${line[0]}
 fi

 cd ${line[0]}

 while read -a line2
 do
 if [ -d ${line2[0]} ]; then
  echo There\'s already a directory names ${line2[0]}
 else
  mkdir ${line2[0]}

  cd ${line2[0]}

  #echo ${line[0]}
  #echo ${line[1]}
  #echo ${line2[0]}
  #echo ${line2[1]}

  if [ ${line2[1]} == "(none)" ]; then
   pushd . > /dev/null
    . /standard_scripts/mknastypes2.sh
   popd > /dev/null
   cd ..
  else
   pushd . > /dev/null
   . /standard_scripts/mknastypes.sh ${line2[1]}
   popd > /dev/null

   cd ..
  fi
 fi
 done < ${line[1]}

 cd ..
done < /etc/settings/Operating_Systems/osnames.sdr 



popd > /dev/null
 
Old 02-19-2013, 06:56 PM   #48
des_a
Member
 
Registered: Sep 2006
Posts: 301

Original Poster
Rep: Reputation: 18
The problem with the above was that I needed to change the syntax of the files, so it could be unambigous.
 
Old 02-19-2013, 06:58 PM   #49
des_a
Member
 
Registered: Sep 2006
Posts: 301

Original Poster
Rep: Reputation: 18
mktypes.sh

Code:
#! /bin/sh


pushd . > /dev/null


echo Making directories for each operating system...
mkdir ALL
cd ALL
. /standard_scripts/mknames.sh
cd ..


while read -a line
do
 echo Creating ${line[0]}...
 if [ -d ${line[0]} ]; then
  echo There\'s already a directory named ${line[0]}
 else
  mkdir ${line[0]}


  cd ${line[0]}


   if [ ${line[1]} == "(none)" ]; then
    . /standard_scripts/mknames.sh
   else
    . /standard_scripts/mkversions.sh ${line[1]}

    cd ..
   fi
 fi
done < $1


popd > /dev/null


---------- Post added 02-19-13 at 04:59 PM ----------

The problem with this last one was that it had an extra loop.
 
1 members found this post helpful.
Old 02-22-2013, 07:47 PM   #50
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,245
Blog Entries: 15

Rep: Reputation: 233Reputation: 233Reputation: 233
Can I ask what delims=$n was for, or if it does really work as expected? I haven't encountered any syntax form like that. Or perhaps it's a new feature?

dive: I suggest placing strings inside double quotes e.g.
Code:
echo "Making directories for each operating system..."
echo "${line[0]}"
echo "There's already a directory named ${line2[0]}"
 
Old 02-22-2013, 09:59 PM   #51
psionl0
Member
 
Registered: Jan 2011
Distribution: slackware_64 14.0
Posts: 537
Blog Entries: 2

Rep: Reputation: 68
Is there a reason why you want your script to tell you if a subdirectory already exists?

You could simply do mkdir -p /global_dir/subdir_1/subdir_2/subdir_3/ and mkdir will create subdir_3 and all its parent directories if they don't already exist.

from mkdir --help:
Code:
  -p, --parents     no error if existing, make parent directories as needed
 
Old 02-23-2013, 11:50 PM   #52
des_a
Member
 
Registered: Sep 2006
Posts: 301

Original Poster
Rep: Reputation: 18
Quote:
Is there a reason why you want your script to tell you if a subdirectory already exists?
Yep. I wanted it to tell me because it indicates an error condition in my code or in the user's use. For example, running the same script twice in a row. If you want to run the script twice, you should run the delete version of it first, so it's an error to run the code twice in a row.
 
Old 02-23-2013, 11:55 PM   #53
des_a
Member
 
Registered: Sep 2006
Posts: 301

Original Poster
Rep: Reputation: 18
Quote:
Can I ask what delims=$n was for, or if it does really work as expected?
If you are referring to one of the files that had the, "@ECHO OFF" at the top, that was the working corresponding Windows XP DOS BATCH file code. All the DOS files previously worked, my project was to change them into something Linux could understand, but they do the same things that's done in DOS.
 
Old 02-24-2013, 01:20 AM   #54
psionl0
Member
 
Registered: Jan 2011
Distribution: slackware_64 14.0
Posts: 537
Blog Entries: 2

Rep: Reputation: 68
Quote:
Originally Posted by des_a View Post
Yep. I wanted it to tell me because it indicates an error condition in my code or in the user's use. For example, running the same script twice in a row. If you want to run the script twice, you should run the delete version of it first, so it's an error to run the code twice in a row.
If that was the case then your script should have given the user the option to bail out rather than just continue regardless.
Quote:
Code:
 if [ -d ${line[0]} ]; then
  echo There\'s already a directory named ${line[0]}
 else
  mkdir ${line[0]}


  cd ${line[0]}
...

Last edited by psionl0; 02-24-2013 at 01:23 AM.
 
Old 02-24-2013, 05:43 AM   #55
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,245
Blog Entries: 15

Rep: Reputation: 233Reputation: 233Reputation: 233
Quote:
Originally Posted by des_a View Post
If you are referring to one of the files that had the, "@ECHO OFF" at the top, that was the working corresponding Windows XP DOS BATCH file code. All the DOS files previously worked, my project was to change them into something Linux could understand, but they do the same things that's done in DOS.
Only that I don't remember $n as valid even in the "for" version of cmd (new command prompt). But it doesn't really matter.
 
Old 02-24-2013, 11:40 PM   #56
sunnydrake
Member
 
Registered: Jul 2009
Location: Kiev,Ukraine
Distribution: Ubuntu,Slax,RedHat
Posts: 288
Blog Entries: 1

Rep: Reputation: 41
sh is not bash or dash. Actually it's quite similar and almost compliant shell interpreters, but not 100%. So it's probably better to explicitly set first line to your command file to currently linked /bin/sh substitution.
About twice running script: generally i see either /tmp/somename.lock (temporary file created and checked at run,also can tell if script finished it's work and deleted lock file(full cycle)) and/or memory check like ps -A command.. in scripts i poked around.
Generally it's less complex/more safe in long run to make script that work one-step-at-time if failed break and show error then fire-all and check output later type(despite huge performance hit).
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Translating Windows Vista into Linux Ghosting Linux - Newbie 4 02-14-2012 12:50 AM
[SOLVED] Help translating code from Linux to OSX? jdmResearch Programming 9 07-26-2011 10:28 AM
Translating / localising Linux to a new language professorsnapper Linux - General 4 02-23-2011 09:44 AM
newbie needs help translating linux tech speak shadowbox12 Linux - Virtualization and Cloud 2 05-17-2010 10:30 PM
translating C software from Win to Linux altella Programming 7 07-04-2007 03:42 PM


All times are GMT -5. The time now is 06:10 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration