Find the answer to your Linux question:
Page 1 of 2 1 2 LastLast
Results 1 to 10 of 12
I need to write a script so that when i call it it runs a few other appz automatically, how is this done, what i the file format and extension, ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Aug 2002
    Posts
    26

    newbie scripting help


    I need to write a script so that when i call it it runs a few other appz automatically, how is this done, what i the file format and extension, and is there a nice tutorial around anywhere???

    smonice

  2. #2
    Linux Guru
    Join Date
    Oct 2001
    Location
    Täby, Sweden
    Posts
    7,578
    If you only need it to run commands in a sequence, it's extremely simple:
    Code:
    #!/bin/sh
    cmd1
    cmd2
    cmd3
    And so on.
    Basically, that's what all shell scripts are about, it's just a matter of what commands you use.
    What you need to do is to make the file executable, ie. run "chmod 755 scriptfile".
    The #!/bin/sh line tells the kernel that /bin/sh is to interpret the file, since it's not a binary executable.
    You seldom have to care about extensions on a UNIX system. They are mainly just for automating some stuff, like file type detection. You can basically name the file anything you want.
    If you want it to be able to run from anywhere, you must put it in a directory that is in your PATH (Run "echo $PATH" to see what directories these are). If you're an ordinary user (which you should be), I recommend creating a bin directory in your home dir and add "PATH=PATH:$HOME/bin" to your .bash_profile file. If you don't want it in your PATH, you'll need to run the file as just something with a slash in, like "/home/user/script", or "./script" if you are currently in your home directory.
    Much could be elaborated on this, but I'll let you ask the questions.

  3. #3
    Just Joined!
    Join Date
    Aug 2002
    Posts
    26
    thanx, that was very informative and im well on my way now to doing som interesting scripting. thanx a ton. any other little pointers i should know before i go any further?

  4. #4
    Just Joined!
    Join Date
    Aug 2002
    Posts
    26
    oh and say u want the output of a program(a file) to be used by the next program to run. Does the first command fully execute and write to the file and then the second command run fully. Cause my output from the second file is blank and im assuming it is because the first file hasnt been created yet. Maybe i need a delay. can that be done????

  5. #5
    Just Joined!
    Join Date
    Aug 2002
    Posts
    26
    oh and how can i output things to the screen and take input from the user in a shell???? nothing fancy just tell the user whats going on (1 line) and take in a y or n, or numbers???

  6. #6
    Linux Engineer
    Join Date
    Jan 2003
    Location
    Lebanon, pa
    Posts
    994
    echo "This is going on right now"

  7. #7
    Linux User
    Join Date
    Jul 2002
    Location
    Daytona Beach, FL
    Posts
    487
    if you want a program to use another output you use a pipe

    for example ls will list all files, and grep will search for a match to the pattern so

    ls | grep foo

    woudl list all files in your current directory, and pass them to grep who would output any files with names with grep in it

    you may find man bash, or man sh helpful in your scripting, you can do if/else and for loops and all those good things... watch your "" on strings though, it will mix you up if you are not careful
    majorwoo

    Quiet brain, or I\'ll stab you with a Q-tip.

  8. #8
    Just Joined!
    Join Date
    Aug 2002
    Posts
    26
    how about input from a user, is that posible???

  9. #9
    Linux Engineer
    Join Date
    Jan 2003
    Location
    Lebanon, pa
    Posts
    994
    ./script value1 value2 value3
    $1 would equal value1, $2 would equal value2 and so on.

    You can also do something like:
    echo "enter some value"
    READ VALUE
    then $VALUE would equal whatever you entered.

    I think those are the only 2 ways to retreive info from the user. Someone correct my syntax if its wrong, I haven't written any extensive bash scripts in quite some time now.

  10. #10
    Linux Guru
    Join Date
    Oct 2001
    Location
    Täby, Sweden
    Posts
    7,578
    Actually, that should be "read VALUE" (lowercase read). There's also the option of using select. See "help select" for more info on that.
    Also, if you want to let the user enter the response on the same line as the prompt instead of on the next line, use echo -n "Prompt:". The -n prevents echo from adding a newline to the end of the prompts.
    I don't know if you know how to test the input then, though. Here's a sample:
    Code:
    #!/bin/sh
    
    echo -n "Do you want to run cmd [y/n]: "
    read response
    if [ "$response" == y]; then
        cmd
    fi
    Some other useful tricks are command substitution (backticks), as in

    grep string `cat listfile`

    That would search for "string" in all files whose names are listed in the file "listfile". The backticks takes the output from the command between them (cat listfile) and puts that output as arguments to grep. I don't know if you know, but cat just outputs the contents of a file (or several files), and grep searches for a regular expression in the files given, or on its standard input if no file names are given. If you don't know what a regular expression is, it's merely a very versatile way of matching text inside a larger text. See regex(7) for more info on them.
    There is still so much more to it, but basically you can do anything you want to.

    Since this is still just the shell, you can run complex commands on the command line as well.
    For example, I have made a client for the direct connect (the file sharing network). If a download gets interrupted for me, I save it in a certain directory, along with a file with the same name but with the extension .info added to its name. In the latter I store information about the file, such as name and total size, so that I can resume the download later. A common thing to do with them is to see what possibilities I have to resume a certain file, and for that I need to know the file size, so that I'm sure that I'm resuming the right file. To do that, I just run (directly on the command line)
    Code:
    for file in *.info; if grep -q "Trigun.*22" $file; then grep -H "^FILESIZE" $file; fi; done
    Basically, that searches all .info files for episode 22 of Trigun (a series I like to watch), and then prints the complete size as stored in that file along with the file name. I'll leave it to you to discover just how it works. It's good practice.
    Remeber, you can always get documentation. For commands that are built in to the shell (such as for and if), you can try eg. "help for", or look in bash(1). Otherwise, for example for grep, just check the manpage (eg. grep(1)).

Page 1 of 2 1 2 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •