Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

Uber simple question for ye folks

  • 22-10-2004 9:00am
    #1
    Registered Users, Registered Users 2 Posts: 15


    in windows batch files, u open up textpad and load in commands like such

    ping 192.168.0.4 >C:\1.txt

    save the file as a .bat
    open command prompt and type in proggyname.bat

    hey presto

    Now can someone please translate this to unix, I googled it but what I typed in didn't work. Unix% or somethin... please, ta

    Eire007 (n00b @ unix, L337 @ life)


Comments

  • Closed Accounts Posts: 96 ✭✭krinDar


    machine%
    machine% echo "ping -c 5 [url]www.google.com[/url] > google_ping" > prog.bat
    machine% 
    machine% chmod 755 prog.bat 
    machine% ./prog.bat 
    machine% ls -la google_ping
    -rw-r--r--  1 user  user  484 Oct 22 10:14 google_ping
    machine% 
    


  • Closed Accounts Posts: 6,718 ✭✭✭SkepticOne


    You can also go

    sh filename

    Where filename contains the commands.


  • Registered Users, Registered Users 2 Posts: 15 Eire007


    thanks, worked a charm, rep all round.


  • Closed Accounts Posts: 153 ✭✭Dustpuppy


    Don't use .bat in unix/linux. use the extention .sh, like myfile.sh. it's better because then you see that it is a shell script. and u need inside as the first line #!/bin/sh or u have to use sh myfile.sh to start it.


  • Closed Accounts Posts: 96 ✭✭krinDar


    dustpuppy wrote:
    u need inside as the first line #!/bin/sh or u have to use sh myfile.sh to start it.

    That is incorrect.


  • Advertisement
  • Closed Accounts Posts: 153 ✭✭Dustpuppy


    krinDar wrote:
    machine%
    machine% echo "ping -c 5 [url]www.google.com[/url] > google_ping" > prog.bat
    machine% 
    machine% chmod 755 prog.bat 
    machine% ./prog.bat 
    machine% ls -la google_ping
    -rw-r--r--  1 user  user  484 Oct 22 10:14 google_ping
    machine% 
    

    And -rw-r--r-- also is wrong. After chmod 755 the rights will be -rwx-xr-x. And u need the line with the full path to your shell. If u don't think u need it. Try it on another unix then linux and u will lern something about shell programing.


  • Closed Accounts Posts: 120 ✭✭test999


    It's a good idea to put the shell that you want your scripte to be interpreted by at the first line of your file,i.e.
    #!/usr/local/bin/bash
    Not everyone uses your shell, i.e. bash, csh, bsh, sh. So, for your script to work everywhere, you must put the shell required at the top of the file.

    chmod 755 will set the file permissions to -rwx r-x r-x
    Basically
    421
    rwx
    and you add them up.

    It's bad practise to call these types of file foo.bat on unix.
    They should be called foo.sh

    HTH


  • Closed Accounts Posts: 96 ✭✭krinDar


    Dustpuppy wrote:
    And -rw-r--r-- also is wrong. After chmod 755 the rights will be -rwx-xr-x.

    Let me read this again. After I 'chmod' prog.bat, the permissions of the output
    file, google_ping will be rwxr-xr-x ?
    I must be missing something here, could you explain it to me ?
    Dustpuppy wrote:
    And u need the line with the full path to your shell. If u don't think u need it. Try it on another unix then linux and u will lern something about shell programing.
    I am really curious, have you even tried to do this at all ?

    If you are having trouble with it, perhaps you should post
    up your problems. I am sure there is someone here who
    would be able to help you.


  • Closed Accounts Posts: 96 ✭✭krinDar


    test999 wrote:
    It's a good idea to put the shell that you want your scripte to be interpreted by at the first line of your file

    I agree it is good practise, but it is not completey necessary as others
    would have you believe. Leaving it out of the example above was perfectly
    reasonable, as the command was simple and would run in (almost) any
    shell, it would definately run in the shells most people would use.
    It's bad practise to call these types of file foo.bat on unix.

    I agree, it is bad practise. But it is not wrong, just bad practise.


  • Closed Accounts Posts: 120 ✭✭test999


    krinDar wrote:
    I agree it is good practise, but it is not completey necessary as others would have you believe.

    I work on a variety of unix platforms. My perspective is, why not simply put the shell on the first line, instead of waiting for it to possibly fail (usually obscurely) on another platform?
    I consider following good practise to be a part of good coding standards.
    It's the difference between okay coding and great coding (not that writting shell scripts is coding, but saying that will start a flame...)

    krinDar wrote:
    I agree, it is bad practise. But it is not wrong, just bad practise.
    I don't see why you think it's necessary to make that part of your post? are you trying to make a point?
    I didn't say it was wrong did I?


  • Advertisement
  • Closed Accounts Posts: 96 ✭✭krinDar


    test999 wrote:
    I work on a variety of unix platforms. My perspective is, why not simply put the shell on the first line, instead of waiting for it to possibly fail (usually obscurely) on another platform?

    I am discussing the validity of what I wrote, (i.e you can execute
    commands that are in a file with invoking a shell at the start) rather
    than practise. I am talking syntax, you are talking semantics. I am
    talking about a specific example, you are talking in general. We are
    both correct and have valid points.

    In general it is perhaps better to invoke a shell, but for something as
    simple as a single command it is pointless and wasteful.

    I am sure that a huge thread could come out of what is good practise
    and what is not. I am not interested in partaking in such a thread.
    test999 wrote:
    I don't see why you think it's necessary to make that part of your post? are you trying to make a point? I didn't say it was wrong did I?

    I am just clarifying the distinction between what is bad practise and
    what is wrong.


  • Closed Accounts Posts: 153 ✭✭Dustpuppy


    Two things i have for u.
    krinDar wrote:
    Let me read this again. After I 'chmod' prog.bat, the permissions of the output
    file, google_ping will be rwxr-xr-x ?
    I must be missing something here, could you explain it to me ?

    1. man chmod
    krinDar wrote:
    I am really curious, have you even tried to do this at all ?

    If you are having trouble with it, perhaps you should post
    up your problems. I am sure there is someone here who
    would be able to help you.

    2. man sh

    :D


  • Closed Accounts Posts: 96 ✭✭krinDar


    Dustpuppy wrote:
    1. man chmod

    Ok - Just for you I will provide a nice little walk through.

    First - I echo a command into a file called prog.bat
    machine% echo "ping -c 5 www.google.com > google_ping" > prog.bat
    

    At this point, the contents of prog.bat is:
    ping -c 5 www.google.com > google_ping
    

    All this does it ping google and wait for 5 echos. The output of the
    command is redirected to a file called google_ping.
    machine% chmod 755 prog.bat 
    machine% ./prog.bat
    
    I change the mode of the prog.bat file to make it executable,
    and execute it. This will (as mentioned before) put the output
    into the file google_ping.

    Then, to prove that it creates output, I list the output file:
    machine% ls -la google_ping
    -rw-r--r--  1 user  user  484 Oct 22 10:14 google_ping
    

    Now - I repeat (and rephrase) my question: If I change the mode
    of the file prog.bat to 755, why would I expect a completely seperate
    output file called google_ping to pick up the same mode ?
    I do linux and unix since 12 years. Please don't tell me, what's right and what's wrong.

    Again, I repeat my question: Have you tried what I suggested ?
    I mean, all the commands, exactly as I ran them are there ready
    to be run.

    With your unwillingness to try the simpliest of things and making
    obiviously incorrect statements I am seriously beginning to wonder
    whether you are a troll.


  • Closed Accounts Posts: 120 ✭✭test999


    @KrinDar, You might want to change your redirect so it works, for error conditions...

    echo "ping -c 5 www.google.com 2>&1 google_ping" > prog.bat


  • Closed Accounts Posts: 96 ✭✭krinDar


    test999 wrote:
    @KrinDar, You might want to change your redirect so it works, for error conditions...

    echo "ping -c 5 www.google.com 2>&1 google_ping" > prog.bat

    The error redirection you are using is Bourne specific (Bourne type
    shells include ksh,bash,zsh and sh), the minimum redirection
    that was present would work in most shells. This is probably a
    good reason to invoke a Bourne shell at that point.

    Also, while we are analysing it, the '-c' argument is not in posix
    ping and if you do not get an echo for any reason (firewalls, remote
    host unavailible) the ping command will not return.


  • Closed Accounts Posts: 120 ✭✭test999


    krinDar wrote:
    The error redirection you are using is Bourne specific (Bourne type shells include ksh,bash,zsh and sh), the minimum redirection
    that was present would work in most shells. This is probably a
    good reason to invoke a Bourne shell at that point.

    So you are saying that without line zero being
    #!/usr/blah/blah/bash
    this script would't work as expected?

    How is specifying which interpreter to use at the start of a script wasteful?


  • Closed Accounts Posts: 96 ✭✭krinDar


    test999 wrote:
    So you are saying that without line zero being
    #!/usr/blah/blah/bash
    this script would't work as expected?

    No. What I am saying is that it will not work in shells that are not
    based on the original Bourne shell e.g csh, tcsh etc. It will work
    if you are using anyone of the shells mentioned (zsh, bash, ksh, sh).
    A potential way around that is to invoke a Bourne type shell to run
    the command.

    Even if you are going to invoke a Bourne shell everytime, it (the general
    'it', not the specific 'it') is not guaranteed to work all the time on
    all variants of UNIX without some forethought.
    How is specifying which interpreter to use at the start of a script wasteful?

    I don't believe that I said that specifying which interpreter to use
    was wasteful. What I said was that it was not always necessary
    to invoke a shell to run a command.

    If you do invoke a shell at the start of your script you have a
    situation where you have a shell, which forks a shell, which forks
    a command.

    Why bother with the extra shell in the middle.


  • Closed Accounts Posts: 120 ✭✭test999


    krinDar wrote:
    I don't believe that I said that

    I couldn't believe it either ;)

    And no, what you wrote wouldn't catch an error condition using the bash shell. It would work under bash if everything was perfect, which is rarely the case.


  • Closed Accounts Posts: 96 ✭✭krinDar


    test999 wrote:
    And no, what you wrote wouldn't catch an error condition using the bash shell. It would work under bash if everything was perfect, which is rarely the case.

    Indeed it wouldn't catch an error, it was never intended to. I would probably
    never write it like that myself. But neither would I ever write a script with
    just one command.


Advertisement