Any command can be a script!


Page 1 of 2 12 LastLast
Results 1 to 15 of 16

Thread: Any command can be a script!

  1. #1
    Join Date
    Oct 2002
    Location
    Binghamton NY
    Posts
    2,435

    Cool Any command can be a script!

    Here's something that just hit me like a brick the other day. I suspect that its going to be received by some who read this as a revelation, and by others as a great big "Well, duh!"

    Any command can be a script! The convolulted BASH command that you can't remember, and find yourself looking up again and again, can be saved as a text file, made executable once with chmod, and activated with a ./ This is the kind of simple scripting that a newbie can put to work right away. It'll make your heart happy that you're not running Windoze anymore.

    Here's three examples, taken from my so-called life:

    1) The other day, I was getting ready to disable X so I could install my nvidia driver. At the console login, there is no cutting and pasting.. and so, from the comfort of the desktop, I was able to take the install command:

    sh NVIDIA-Linux-x86-1.0-7676-pkg1.run
    and save it as a text file "nv". When i was logged onto the console a few minutes later, I could type:

    chmod a+x nv
    ./nv
    and that activated the command, with no need to worry about typing it correctly.

    NOTE: Okay, I must admit, while this is a good example of a simple one-command script, in retrospect, it's probably not the easiest way to do this. There's also wildcards. Depending on what else was in the directory, I could have probably also simply typed:

    sh N*

    and let it go at that. However, considering my inexperience-- and the fact that I was feeling rather intimidated about leaving my accustomed desktop home and installing the driver in the wilds of the console-- having the whole command at my fingertips like that was comforting, and I think I might do it the same way again. If I were to forget or feel uncertain about the correct command, I 'd have to either try to locate the information online with lynx (searching the flash-infested nvidia site with a text browser can't be fun) or boot up the desktop all over again.

    2) As I said before, the is a great way to revisits commands that you are always having to look up. For me, a great example of that would be the command to mount my windows partition:

    mount -t ntfs -r /dev/hda1 /xp
    Sure, I should probably know this by now, but we've already established that I'm not the sharpest knife in the Just Linux drawer, and so I was always always looking this up. Sometimes, I was lucky, and I could find it in the recent command history. Other times, I would have to go to the oreilly website, login, give 'em my password, yadda yadda yadda.

    but now, I just go to my directory of simple scripts:

    cd ~/simple
    and type, as su:

    ./xp
    (having made the script executable with a+x chmod a long time ago)

    and my windows partition is mounted-- viola!

    3) Once you've started doing one-line scripts like this there are probably endless ways of automating. Here's the only one I know so far, but it totally rocks: the "at" command. I know I've mentioned this before, but if I write a script to play an mp3 file:

    mpg321 Bob*Marley:*One*Love.mp3
    (note that the *'s are there because spaces can trip up bash)

    and name my script "alarm", I can play it on a timer by typing:

    at 7:00 am tomorrow <alarm
    NOTE: Technically speaking, I don't know if these are really full-fledged scripts. Someone mentioned to me that there's a command that bash scripts usually begin with to tell the shell to run as bash. I have excluded that from these one-line scripts, because, frankly, I don't know it... and besides, there's little chance that I'll be working from any other shell.
    Last edited by blackbelt_jones; 10-06-2005 at 12:52 PM.

  2. #2
    Join Date
    Sep 2003
    Location
    Rochester, MN
    Posts
    3,607
    Or "sh N[tab]" and let the shell autocomplete the filename for you.

  3. #3
    Join Date
    Dec 1999
    Location
    tx
    Posts
    1,190
    #!/bin/bash

    as first characters of a file tell the file to run via bash. Note that you can also change /bin/bash to other executables if you wish to run it in another executable.

    Of such things do newbies become great geeks! Good job!

  4. #4
    Join Date
    Sep 2002
    Location
    San Antonio, TX
    Posts
    2,610
    Perhaps an easier way to do this is using alias. That way, you don't have mini scripts to look after. If you edit .bashrc in any text editor (back it up first), you can add commands like this to the bottom.

    alias 2185='expect /home/xxxxx/Scripts/2185'

    then, after I open a shell, entering
    2185 will spawn that expect script which will ssh into my workstation at work. You can add anything to alias, even mutliple commands like

    alias marly='mpg321 Bob*Marley:*One*Love.mp3;mpg321 *Disco.mp3'

    then entering marly in a console will play all your bob marleys followed by your extensive disco collection, lol.

    maybe a better example
    alias backhome='tar cf /tmp/`date`.tar /home;cp /tmp/*.zip /pathtoarchive'

    then you enter backhome in a console, you have backed up your home directory to todays data.zip and copied it to an archive location.

    hlrguy
    Were you a Windows expert the VERY first time you looked at a computer with Windows, or did it take a little time.....
    My Linux Blog
    Linux Native Replacements for Windows Programs
    Mandriva One on a "Vista Home Barely" T3640 E-Machine runs great.

  5. #5
    Join Date
    Sep 2003
    Location
    Rochester, MN
    Posts
    3,607
    Good call on alias for the one-liners. I use it for all of mine except my mencoder command-lines because I now have so many of them (some of which are not actually one-liners) that I consolidated them into one big script.

  6. #6
    Join Date
    Oct 2002
    Location
    Republic of Texas
    Posts
    5,908
    $ grep alias .bashrc
    alias d="ls --color"
    alias ls="ls --color=auto"
    alias ll="ls --color -l"
    alias eject="sudo eject"
    alias nano="nano -w"
    alias cdrecord="cdrecord dev=/dev/dvdburner driveropts=burnfree"
    alias namdcvs="cvs -d server:je_fro@cvs.ks.uiuc.edu:/namd/cvsroot"
    alias adt="source /opt/autodock/initPython.bash && adt"
    alias matlab="LD_ASSUME_KERNEL=2.4.1 && export LD_ASSUME_KERNEL && lmstart && matlab"
    Need help in realtime? Visit us at #linuxnewbie on irc.libera.chat

    Few of us will do as much for our fellow man as he has done.
    --Andrew Morton on RMS

  7. #7
    Join Date
    Apr 2001
    Location
    SF Bay Area, CA
    Posts
    14,947
    Quote Originally Posted by blackbelt_jones
    (note that the *'s are there because spaces can trip up bash)
    Not if you put the filename in quotes. Or use tab completion (tab completion will complete the filename, putting \ characters before the spaces, so it doesn't screw up the shell).

  8. #8
    Join Date
    Mar 2003
    Location
    Tampa, FL USA
    Posts
    2,193
    If your windows partition is located in your /etc/fstab file(as it should be), then you would only have to type "mount /xp". (If you need help with that, it belongs in another thread.)

    Though, to play nice I like aliasing, but here's a sort of pro/con view.

    Reasons for aliasing:
    No need to create 'yet another file' on your system.
    No need to access the harddrive when that command is called.
    It's fast and easy. alias ll="ls -l"

    Reasons against aliasing:
    Aliases are erased at reboot, so if you want one permanent, you must put it in a startup file. (like ~/.profile)
    Aliases aren't made to hold complex(3+ lines) commands. (but functions are)
    They take up memory. (though generally a very tiny amount)

    ======================================
    By putting #!/bin/bash at the top of your script, it makes your terminal run those commands in a NEW instance of bash. When the script finishes, it that new instance is invisibly 'logged out'.

    If I may explain the "#!/bin/bash" a little:

    Anywhere # is used in bash, it means a comment. So everything after the # is ignored by the computer. If you enter the command "bash script.sh", and the script has "#!/bin/bash" at the top, then bash will completely ignore that first line.
    BUT, there is an exception to the rule. When a file is executed directly (ie. "./script.sh"), the FIRST line may specify a program to send this file to. The program location will follow the "#!". (I suppose the exclamation point is just to 'make sure' it's not a comment.) Equally, perl scripts start with "#!/usr/bin/perl".

    So executing a file like "/bin/bash script.sh", is generally equivalent to executing a script "./script.sh", if it has "#!/bin/bash" on the first line.
    Last edited by Sepero; 10-06-2005 at 08:03 PM.

  9. #9
    Join Date
    Oct 2002
    Location
    Binghamton NY
    Posts
    2,435
    Gee, it's embrassing that I don't know so much... but excellent to be learning so much. Cool thread, guys!

  10. #10
    Join Date
    Apr 2001
    Location
    SF Bay Area, CA
    Posts
    14,947
    Quote Originally Posted by Sepero
    The program location will follow the "#!". (I suppose the exclamation point is just to 'make sure' it's not a comment.)
    Not exactly.

    It's there because those two byte values (according to "man ascii", they're 0x23 0x21) compose the magic number for an interpreted file. When the kernel's exec() syscall code sees that it's been called on a file whose first two bytes are 0x23 0x21 (#!), it will read the entire first "line" of the file in (up to the first 0x0A character, i.e. the first newline), strip off the first two bytes, and treat whatever it has left over as a command to run, with that command's argv[1] or argv[2] (depending on whether an argument is present after the path) set to the original filename.

    This will work with any filetype that's interpreted, as long as you can start it up by doing a:

    <name of interpreter> <zero or one options> <name of file>

    and as long as the language is line-oriented and uses # characters as comments. If both of those are true, then you can start the file with #!/path/to/interpreter, and if you execute the file directly, it'll actually run under the interpreter.

    So it'd work for shell scripts, Python, Perl, PHP, Lisp, Tcl, and most other scripting languages.

  11. #11
    Join Date
    Mar 2005
    Location
    Singapore
    Posts
    246
    Hey guys , using this method , is it possible to set

    Su - , then Shutdown -h now to a script ? I want to help my dad get accustomed to linux , but even remembering startxfce4 gets him all annoyed . So , i was thinnking of wrting little scripts like "desktop" to open the wm and "shutdown" and "restart" . Would it be possible to combine Su - ( to get to root ) then shutdown ? Its just a weird idea . If theres an easier way , please inform me .

    #98 +(5627)- [X]
    <ikkenai> i don't have hard drives. i just keep 30 chinese teenagers in my basement and force them to memorize numbers
    Courtesy of bash.org

  12. #12
    Join Date
    Oct 2000
    Location
    Calgary, Alberta, Canada
    Posts
    8,130
    not exactly, but "sudo shutdown -h now" would work.

    man sudo, or search the forums/google for more info on it.

  13. #13
    Join Date
    Feb 2004
    Location
    Singapore
    Posts
    2,171
    su - will require a password, so it might not work unless you can get the password.

    well, you can try making it a script --- at most it will ask you for password while running...
    Come under the reign of the Idiot King...
    Come to me ... I love linux!

    Registered Linux user: Idiot King #350544

  14. #14
    Join Date
    Mar 2003
    Location
    Tampa, FL USA
    Posts
    2,193
    Quote Originally Posted by Hayl
    not exactly, but "sudo shutdown -h now" would work.
    Hayl, is correct that sudo would be the better way to go, though you can do it with regular su:
    su -c "shutdown -h now"

    Here's a script suggestion for your dad:
    Code:
    #!/bin/bash
    # /usr/bin/pc - personal commands
    
    function desktop ()
    { startxfce4; }
    
    function powoff ()
    { sudo poweroff; }
    
    function rboot ()
    { sudo reboot; }
    
    function help ()
    {
      echo "Help message. Commands are:"
      echo " -d or --desktop    to start your desktop"
      echo " -p or --poweroff   to turn off your computer"
      echo " -r or --reboot     to restart your computer"
      echo " -h or --help       show this help message"
    }
    
    case "$1" in
      -d|--desktop) desktop; break;;
      -p|--poweroff) powoff; break;;
      -r|--reboot) rboot; break;;
      *) help; break;;
    esac
    Save that in a file at /usr/bin/pc. Make it executable ("chmod 755 /usr/bin/pc"). Then it's easy peasey for your dad.
    pc -d = start desktop
    pc -r = reboot
    pc -p = poweroff computer

    One last thing. Consider putting you dad on a Debian offshoot. (Mepis, Xandros, Linspire, Ubuntu)

    PS.
    I haven't tested that script. If there's a prob, let me know and I'll fix it.
    Last edited by Sepero; 10-08-2005 at 08:03 AM.

  15. #15
    Join Date
    Mar 2005
    Location
    Singapore
    Posts
    246

    Smile Thanks , and my apologies

    Sepero : Care to buy me another box ? Thanks dude , i havent tried it yet , but i will once my exams are done on the 14th . I think the script might work

    i would have put him on Ubuntu if it wasnt for my lack of space ( 25 GB ) , lack of ram ( 128 ) , and my insistence on speed ( thus souce-based and XFCE ) . He's forced to use the linux box when i have to do some graphics designing on the windows OR when my mother has to do something on the windows . so , i just tarballed everything of his ( which amounted to the whopping sum of 5 MB) and emailed it to myself to open on the linux box .

    sorry for hijacking your topic blackbelt_jones ( nice avvie btw )

    #98 +(5627)- [X]
    <ikkenai> i don't have hard drives. i just keep 30 chinese teenagers in my basement and force them to memorize numbers
    Courtesy of bash.org

Posting Permissions

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