LinuxQuestions.org
Go Job Hunting at the LQ Job Marketplace
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-04-2013, 12:10 AM   #1
des_a
Member
 
Registered: Sep 2006
Posts: 303

Rep: Reputation: 18
Translating Standard Scripts to Linux


I've been translating my code from Windows XP DOS BATCH files to Linux BASH shell scripts. I've run into a problem from this script. I'm having trouble parsing the database I've created, which tells Linux how to create a set of directories in this case.

The I'm on mknasosnames2.sh, which it's job is to create standard directories for each known operating system. It's code in Windows XP:

Code:
@ECHO OFF


ECHO Making Directories for Each Operating System...


MKDIR OS
FOR /F "tokens=* delims=$n" %%A IN (C:\SETTINGS\Operating_Systems\osnames.sdr) DO (
 FOR /F "tokens=1,* delims=	" %%B IN ("%%A") DO (
  ECHO Creating %%B...
  IF EXIST "%%B" ECHO There's already a directory named %%B!
  MKDIR ".\%%B"

  CD ".\%%B"
  CALL :mktypes "%%C"
  CD ..
 )
)
GOTO done


:mktypes
 IF "%~1" == "(none)" GOTO mktypes_done


 CALL mknastypes3 %1
 GOTO mktypes_done


 :mktypes_done
  EXIT /B 0


:done
It's Linux code so far:

Code:
#! /bin/sh



echo Making directories for each operating system...
mkdir OS


cat /etc/settings/Operating_Systems/osnames.sdr | while read -a line
do
 echo ${line[0]}
 echo ${line[1]}
 cat ${line[1]} | while read -a line2
 do
  echo Creating ${line[0]}...
#  if [ -f $line2[0] ]; then   
#   echo There\'s already a directory named $line2[0]
#  else
#   mkdir $line2[0]

#   cd $line2[0]
#   cd ..
#  fi
 done
done
It fails on the second while loop, currently.
 
Old 02-04-2013, 12:19 AM   #2
des_a
Member
 
Registered: Sep 2006
Posts: 303

Original Poster
Rep: Reputation: 18
By the way, what my standard scripts are is this: Any OS that can support the standard scripts can be used on this network, I know this will work in Linux, I just don't know how yet.
 
Old 02-04-2013, 03:38 AM   #3
dive
Senior Member
 
Registered: Aug 2003
Location: UK
Distribution: Slackware
Posts: 3,211

Rep: Reputation: 292Reputation: 292Reputation: 292
If you're testing for a directory, use -d not -f in your test.

# if [ -d $line2[0] ]; then
 
Old 02-04-2013, 05:35 AM   #4
des_a
Member
 
Registered: Sep 2006
Posts: 303

Original Poster
Rep: Reputation: 18
This is it's output:

Code:
[root@c-des-main1-rec standard_scripts]# . ./mknasosnames2.sh
Making directories for each operating system...
mkdir: cannot create directory `OS': File exists
DOS
/etc/settings/Operating_Systems/dos.sdr
: No such file or directoryg_Systems/dos.sdr
LINUX
/etc/settings/Operating_Systems/linux/types.sdr
: No such file or directoryg_Systems/linux/types.sdr
WINDOWS
/etc/settings/Operating_Systems/windows/types.sdr
: No such file or directoryg_Systems/windows/types.sdr
[root@c-des-main1-rec standard_scripts]#

Last edited by des_a; 02-04-2013 at 05:36 AM. Reason: Bad copy and paste.
 
Old 02-04-2013, 05:38 AM   #5
des_a
Member
 
Registered: Sep 2006
Posts: 303

Original Poster
Rep: Reputation: 18
Thanks. I'll use -d instead of -f. But with the output, now you see the problem. I'd tried a for loop, and it does the same thing. Appearently, it's comming from the cat command.

---------- Post added 02-04-13 at 03:38 AM ----------

If it really sees that shortened version of what it reports, it has reason to complain, but I want it to see the whole path, which I put in the file.
 
Old 02-04-2013, 12:44 PM   #6
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947
This page has some tips about converting DOS batch files to bash.

http://www.tldp.org/LDP/abs/html/dosbatch.html

But if you really want to code "standard" scripts, then you have to restrict all of your code to what's available in the POSIX standard. When you run a script with "#!/bin/sh" at the top, it will be interpreted as such, even if the underlying interpreter is actually bash or another shell.

Conversely, to get the full set of bash features, you have to use #!/bin/bash.


POSIX doesn't have many of the useful modern features that are available to bash, such as arrays. See this page for bash-specific commands and their equivalents.

http://mywiki.wooledge.org/Bashism


Note: If your script does include things like arrays, and they seem to work, that's only because the system is still set to use bash (or a similar shell) to do the interpreting. Since arrays are not counter to POSIX, but just undefined by it, there's nothing keeping a shell from interpreting them if it wants to. But if, for example, the system was using dash instead (a strictly complying shell), it would break. All /bin/sh says is that, if a script is coded according to POSIX, it will run. It doesn't try to force compliance to it.

In fact, one of the best ways to test that your script is POSIX-compliant is to use #!/bin/dash as your shebang. If it runs in dash, it should run anywhere.


Incidentally, I see several errors in the above script. Unquoted variables, improperly formatted array expansions, Useless Use Of Cat. But maybe I should wait for you to clarify whether you actually need POSIX or not, or whether a bash-compatible script would do (it is available standard on pretty much every Linux distro, after all). The latter would certainly be much more convenient.

PS: You also really need to supply us with a sample of the input. We need to see what the files that it's processing hold.

Last edited by David the H.; 02-04-2013 at 12:57 PM. Reason: minor rewording
 
Old 02-04-2013, 12:51 PM   #7
sunnydrake
Member
 
Registered: Jul 2009
Location: Kiev,Ukraine
Distribution: Ubuntu,Slax,RedHat
Posts: 288
Blog Entries: 1

Rep: Reputation: 41
really.. it's hard to say how to fix it if you don't say what's actually wrong.. second loop is wrong it like my pc is broken fix it.. for file read i use
for f in $(cat -s ~/bin/BabyPhotoStarGalleryGetImagesAslist.sh); do echo "LINE".$f;done;
but there is a problem f is each word(1) separated by spaces
so to read lines is better to use readarray f < ~/bin/BabyPhotoStarGalleryGetImagesAslist.sh where f[N] is lines from file
 
Old 02-04-2013, 01:44 PM   #8
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947
@Sunnydrake

Please Do not Read Lines With For. Always use a while+read loop instead.

How can I read a file (data stream, variable) line-by-line (and/or field-by-field)?
http://mywiki.wooledge.org/BashFAQ/001

readarray/mapfile is another safe option, but probably not suitable if the input file is very large. It's also a bash-specific extension (available since 4.0), and not at all portable.


And do please use ***[code][/code]*** tags around your code and data, to preserve the original formatting and to improve readability.
 
Old 02-04-2013, 03:46 PM   #9
dive
Senior Member
 
Registered: Aug 2003
Location: UK
Distribution: Slackware
Posts: 3,211

Rep: Reputation: 292Reputation: 292Reputation: 292
For some reason it isn't reading line2 correctly when it cats ${line[1]}. Cat you post a bit of /etc/settings/Operating_Systems/dos.sdr ?

I would tend to use something like:

while read -a line2
do
...
done < ${line[1]}

in these situations.

Also, if you use mkdir with the -p (make parent) flag it will surpress the already exists warnings.
 
Old 02-04-2013, 03:54 PM   #10
schneidz
Senior Member
 
Registered: May 2005
Location: boston, usa
Distribution: fc-15/ fc-20-live-usb/ aix
Posts: 3,956

Rep: Reputation: 604Reputation: 604Reputation: 604Reputation: 604Reputation: 604Reputation: 604
can the op please post a few lines of the input file and what they are expecting the output to be ?
 
Old 02-04-2013, 07:44 PM   #11
des_a
Member
 
Registered: Sep 2006
Posts: 303

Original Poster
Rep: Reputation: 18
I'll post the files.
 
Old 02-04-2013, 08:00 PM   #12
des_a
Member
 
Registered: Sep 2006
Posts: 303

Original Poster
Rep: Reputation: 18
/etc/settings/Operating_Systems/osnames.sdr
Code:
DOS	/etc/settings/Operating_Systems/dos.sdr
LINUX   /etc/settings/Operating_Systems/linux/types.sdr
WINDOWS	/etc/settings/Operating_Systems/windows/types.sdr

/etc/settings/Operating_Systems/dos.sdr
Code:
FreeDOS	(none)
PC-DOS	(none)
MS-DOS	(none)
/etc/settings/Operating_Systems/linux/types.sdr
Code:
Mandrake	/etc/settings/Operating_Systems/linux/Mandrake.sdr
Mandriva	/etc/settings/Operating_Systems/linux/Mandriva.sdr
/etc/settings/Operating_Systems/linux/Mandrake.sdr
Code:
10.1
/etc/settings/Operating_Systems/linux/Mandriva.sdr
Code:
2008.0
2009.1
/etc/settings/Operating_Systems/windows/types.sdr
Code:
95	(none)
98	(none)
XP	/etc/settings/Operating_Systems/windows/XP.sdr
/etc/settings/Operating_Systems/windows/XP.sdr
Code:
HOME
PRO
MCE
These are the files I'm trying to parse. I did try it with the following format:

Code:
while read -a line2
do
...
done < ${line[1]}
...and this had exactly the same result.

Let me know if I need to explain the syntax of any of these files. Also note that they probably need to be updated to contain my latest information, however, that was not my first priority. It's the same syntax I have on Windows XP, except with a Linux path name instead of a Windows path name. The Windows code works.
 
Old 02-04-2013, 08:01 PM   #13
des_a
Member
 
Registered: Sep 2006
Posts: 303

Original Poster
Rep: Reputation: 18
You can also see that the code is still not complete yet, when it gets to the second loop working, but that's what I'm stuck on for now.
 
Old 02-04-2013, 10:42 PM   #14
sundialsvcs
Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 5,358

Rep: Reputation: 1106Reputation: 1106Reputation: 1106Reputation: 1106Reputation: 1106Reputation: 1106Reputation: 1106Reputation: 1106Reputation: 1106
Interesting thought ...

... has no one in Linux-land ever tackled the ("if I on-ly had a brain...") problem of dealing with DOS batch-files? Is it really true that no one ever wrote a command that could "gracefully and graciously" (of course...) accept DOS batch-files as input, and "have a good college try" at running them?

Casually googling, I didn't find one (yet), but ... it seems so obvious. Really?

You mean, I can't do: #!/bin/ihavenobrain ??
 
Old 02-04-2013, 10:46 PM   #15
dive
Senior Member
 
Registered: Aug 2003
Location: UK
Distribution: Slackware
Posts: 3,211

Rep: Reputation: 292Reputation: 292Reputation: 292
This works for me.

Code:
#!/bin/sh

echo Making directories for each operating system...
mkdir -p OS
cd OS # did you want this?

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

done < /etc/settings/Operating_Systems/osnames.sdr
ls OS
95/ 98/ FreeDOS/ MS-DOS/ Mandrake/ Mandriva/ PC-DOS/ XP/

Note that I've changed all the $line2[0] to ${line2[0]}

Last edited by dive; 02-05-2013 at 05:11 AM.
 
1 members found this post helpful.
  


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 01:49 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