LinuxQuestions.org
Register a domain and help support LQ
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 02-13-2009, 08:20 AM   #1
topheraholic
Member
 
Registered: Aug 2008
Location: shanghai
Distribution: ubuntu
Posts: 128

Rep: Reputation: 15
what can i do with shell?


i have learned how to program in shell. but i do not know what should i do with it? how can i program with it? how to practice?
thanks in advance!
 
Old 02-13-2009, 08:23 AM   #2
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974
if you want to learn shell scripting, check out the bash scripting howto at tldp.org
 
Old 02-13-2009, 08:58 AM   #3
topheraholic
Member
 
Registered: Aug 2008
Location: shanghai
Distribution: ubuntu
Posts: 128

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by acid_kewpie View Post
if you want to learn shell scripting, check out the bash scripting howto at tldp.org
i mean i already read a classic book called "advanced bash-scripting"and i know how to write shell script. but i do not know what can i do with it? how to practice it? how to practice programming with shell?
thanks!!!!!
 
Old 02-13-2009, 09:17 AM   #4
jstephens84
Senior Member
 
Registered: Sep 2004
Location: Nashville
Distribution: Manjaro, RHEL, CentOS
Posts: 2,098

Rep: Reputation: 102Reputation: 102
I would start by making some utilities for monitoring your system, parsing log files, automating backups with rsync, or tar, and anything else. That should be a good start. Oh and you might also want to try and create install scripts for files.
 
Old 02-13-2009, 09:27 AM   #5
topheraholic
Member
 
Registered: Aug 2008
Location: shanghai
Distribution: ubuntu
Posts: 128

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by jstephens84 View Post
I would start by making some utilities for monitoring your system, parsing log files, automating backups with rsync, or tar, and anything else. That should be a good start. Oh and you might also want to try and create install scripts for files.
oh yeah! that's it! but i do not know how to write it? i donot konw much about my system. could you give me yours to learn? shall we be friends? do you have msn or icq or AIM? or email? maybe you can guide me. thanks
 
Old 02-13-2009, 09:47 AM   #6
jstephens84
Senior Member
 
Registered: Sep 2004
Location: Nashville
Distribution: Manjaro, RHEL, CentOS
Posts: 2,098

Rep: Reputation: 102Reputation: 102
Quote:
Originally Posted by topheraholic View Post
oh yeah! that's it! but i do not know how to write it? i donot konw much about my system. could you give me yours to learn? shall we be friends? do you have msn or icq or AIM? or email? maybe you can guide me. thanks
Don't do msn, or icq or aim. But I am always glad to help. So lets start out by doing a simple backup.
1) I would decide what you want to backup.
2) Decide when to backup.
3) Where are you going to backup to. (My preference has always been to get a 500gb hard drive.)

4) Lets break our backups into folders. Say Mon, Tue, Wed, Thr, Fri. Week1, Week2. I use week1 and week2 so that I can go back as far as two weeks in my full backups.

5) Lets do some sanity checks. First part of your script will be to see if rysnc is installed. so you will want to check for that and if it fails then stop and create a log with the error.

This should get you started. What I would do is then use your book for reference. As for a system to start out with I would use debian because to me it is simple. Someother good ones are Slackware, Fedora, Ubuntu, CentOS, my favorite right now is arch. If all you are trying to do is a little bash scripting I would say Fedora, Ubuntu, CentOS. Debian, Slackware, and Arch normally have some burn in time where you have to setup some things.

Last edited by jstephens84; 02-13-2009 at 09:50 AM.
 
Old 02-13-2009, 10:17 AM   #7
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
You know, people don't usually start with "I want to do some scripting" and then search for something to script. They usually begin with a problem or need; something like, "I want to be able to take a folder full of mp3 files, convert them to .ogg format with metadata intact, rename them according to the track metadata, then sort them all into separate folders based on the artist and filename". They then go out and script a series of commands to do just that.

All a script really is is a sequence of commands designed to automate some process. You want practice? Take any cli command you know how to use (using ffmpeg to convert audio files to ogg vorbis, for example) and simply see if you can automate it to process a whole directory of files at once. Then you can start replacing the hard-coded commands with user-input choices (such as selecting ogg vs mp3 output). You can get gradually more and more complicated as you go on (e.g. how do I vary the bitrate? How can I have user-selected filenames?, having an option to move/remove the original file, etc.). The sky's the limit. It all comes down to your imagination and scripting experience.

So really, you just need to get down and dirty. You can't learn scripting by reading a book. Just decide on something you want to accomplish and start writing. It'll be slow going at first, but that's how you learn.

The first script I ever created was a "remove directory" script that asked me if I wanted to either remove the whole thing, including contents, at once, or to confirm the deletion of each file separately. I made it because I was paranoid about deleting a directory with "rm -r" and losing important stuff inside. But you know something? Even though I made my script ask me some questions first, at the heart of it there's still just the plain old "rm -r" command. I still use it today, but I probably should go back and rewrite it soon, because I've learned so much more about scripting since then that I'm almost embarrassed to look at the code for it.
 
Old 02-13-2009, 10:26 AM   #8
topheraholic
Member
 
Registered: Aug 2008
Location: shanghai
Distribution: ubuntu
Posts: 128

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by jstephens84 View Post
Don't do msn, or icq or aim. But I am always glad to help. So lets start out by doing a simple backup.
1) I would decide what you want to backup.
2) Decide when to backup.
3) Where are you going to backup to. (My preference has always been to get a 500gb hard drive.)

4) Lets break our backups into folders. Say Mon, Tue, Wed, Thr, Fri. Week1, Week2. I use week1 and week2 so that I can go back as far as two weeks in my full backups.

5) Lets do some sanity checks. First part of your script will be to see if rysnc is installed. so you will want to check for that and if it fails then stop and create a log with the error.

This should get you started. What I would do is then use your book for reference. As for a system to start out with I would use debian because to me it is simple. Someother good ones are Slackware, Fedora, Ubuntu, CentOS, my favorite right now is arch. If all you are trying to do is a little bash scripting I would say Fedora, Ubuntu, CentOS. Debian, Slackware, and Arch normally have some burn in time where you have to setup some things.
sorry, i did not exactly understand this. this is a five step of make a back up script?
thanks
what should i do now?

i want to write a script to change all the file names of one folder. but i faild. could you guide me this?
thanks

Last edited by topheraholic; 02-13-2009 at 10:33 AM.
 
Old 02-13-2009, 10:33 AM   #9
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974
if you don't know why you're doing something, don't do it! This approach makes no sense at all...
 
Old 02-13-2009, 10:39 AM   #10
topheraholic
Member
 
Registered: Aug 2008
Location: shanghai
Distribution: ubuntu
Posts: 128

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by David the H. View Post
You know, people don't usually start with "I want to do some scripting" and then search for something to script. They usually begin with a problem or need; something like, "I want to be able to take a folder full of mp3 files, convert them to .ogg format with metadata intact, rename them according to the track metadata, then sort them all into separate folders based on the artist and filename". They then go out and script a series of commands to do just that.

All a script really is is a sequence of commands designed to automate some process. You want practice? Take any cli command you know how to use (using ffmpeg to convert audio files to ogg vorbis, for example) and simply see if you can automate it to process a whole directory of files at once. Then you can start replacing the hard-coded commands with user-input choices (such as selecting ogg vs mp3 output). You can get gradually more and more complicated as you go on (e.g. how do I vary the bitrate? How can I have user-selected filenames?, having an option to move/remove the original file, etc.). The sky's the limit. It all comes down to your imagination and scripting experience.

So really, you just need to get down and dirty. You can't learn scripting by reading a book. Just decide on something you want to accomplish and start writing. It'll be slow going at first, but that's how you learn.

The first script I ever created was a "remove directory" script that asked me if I wanted to either remove the whole thing, including contents, at once, or to confirm the deletion of each file separately. I made it because I was paranoid about deleting a directory with "rm -r" and losing important stuff inside. But you know something? Even though I made my script ask me some questions first, at the heart of it there's still just the plain old "rm -r" command. I still use it today, but I probably should go back and rewrite it soon, because I've learned so much more about scripting since then that I'm almost embarrassed to look at the code for it.
oh thanks very much for telling me this! once i want to write a script to change all the file names of one folder, but i faild.and i do not know how to wirte a right one. could you guide me?
thanks in advance.
 
Old 02-13-2009, 10:41 AM   #11
topheraholic
Member
 
Registered: Aug 2008
Location: shanghai
Distribution: ubuntu
Posts: 128

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by acid_kewpie View Post
if you don't know why you're doing something, don't do it! This approach makes no sense at all...
i know why i am doing this, i just do not know how to make it right at first.
 
Old 02-13-2009, 10:46 AM   #12
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 670Reputation: 670Reputation: 670Reputation: 670Reputation: 670Reputation: 670
First get used to using the console. Cd to different directories; list files; use less & cat to examine text files. Get comfortable in the shell environment. Use the * and ? wildcards with ls. Become familiar with using a text editor. There are several to choose from.

The most common commands are provided by the coreutils package. It contains commands such as cp, mv, ls, cat. Scan through the info manual for coreutils. It may be easier reading it in the konqueror browser. You can do this by entering info:coreutils in the address bar.
You can read manpages the same way, e.g. man:mv

Last edited by jschiwal; 02-13-2009 at 10:47 AM.
 
Old 02-13-2009, 11:07 AM   #13
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
That's what the texts and tutorials are for. You don't just jump into big scripts, you start small and learn the basics.

You first have to know how to use individual commands on their own. Only when you're familiar with the tools can you start using scripting functions to combine them. For your "rename files" script, you need a few things: First you need a renaming program. mv will do nicely. If you don't know how to use mv to rename a file, then you have to stop right there and learn that first. Second, you need a way to run the command multiple times on different data input. The "for loop" scripting process is probably the one you'll want to use. Third, you need to be able to input the name changes you want, and for that you'll need to use variables, particularly the command positional parameters ($1, $2) and perhaps the read command if you want interactive input. You may also wish to do some arithmetical processing if you want to generate sequentially-numbered files, the output of which will be stored in a variable. Finally you'll want to use the "echo" command to provide some user-readable feedback. Again, if you don't know what all of these things are, you have to do more studying.

Your final script will probably be something that takes an input string from the command line (when starting the script or interactively), stores it in a variable, then uses that variable in the output of the mv command. And the mv command will be sitting in a for loop that takes each filename in the directory as input and runs the command on it.

I often recommend LinuxCommand.org as a good place to start learning the shell and scripting. Don't just read it, go through each page step-by-step and actually do the things he shows you in the examples. Try the commands out yourself in a console of your own. Play with them until you really understand the concepts behind them. After a while you should start to naturally recognize how you can combine them to do things that are not in the tutorials.

Last edited by David the H.; 02-13-2009 at 11:15 AM. Reason: fixed a broken tag & some typos
 
Old 02-13-2009, 11:14 AM   #14
jstephens84
Senior Member
 
Registered: Sep 2004
Location: Nashville
Distribution: Manjaro, RHEL, CentOS
Posts: 2,098

Rep: Reputation: 102Reputation: 102
That is a pretty good site. I really like the flow and layout. Course I started out with this book and reference it a lot. http://www.amazon.com/Linux-Shell-Sc...4541634&sr=8-6 but then moved to perl. Only problem with perl is that when you stop using it, you seem to forget alot of the things it can do.
 
Old 02-14-2009, 09:22 AM   #15
topheraholic
Member
 
Registered: Aug 2008
Location: shanghai
Distribution: ubuntu
Posts: 128

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by David the H. View Post
That's what the texts and tutorials are for. You don't just jump into big scripts, you start small and learn the basics.

You first have to know how to use individual commands on their own. Only when you're familiar with the tools can you start using scripting functions to combine them. For your "rename files" script, you need a few things: First you need a renaming program. mv will do nicely. If you don't know how to use mv to rename a file, then you have to stop right there and learn that first. Second, you need a way to run the command multiple times on different data input. The "for loop" scripting process is probably the one you'll want to use. Third, you need to be able to input the name changes you want, and for that you'll need to use variables, particularly the command positional parameters ($1, $2) and perhaps the read command if you want interactive input. You may also wish to do some arithmetical processing if you want to generate sequentially-numbered files, the output of which will be stored in a variable. Finally you'll want to use the "echo" command to provide some user-readable feedback. Again, if you don't know what all of these things are, you have to do more studying.

Your final script will probably be something that takes an input string from the command line (when starting the script or interactively), stores it in a variable, then uses that variable in the output of the mv command. And the mv command will be sitting in a for loop that takes each filename in the directory as input and runs the command on it.

I often recommend LinuxCommand.org as a good place to start learning the shell and scripting. Don't just read it, go through each page step-by-step and actually do the things he shows you in the examples. Try the commands out yourself in a console of your own. Play with them until you really understand the concepts behind them. After a while you should start to naturally recognize how you can combine them to do things that are not in the tutorials.
thanks so much! i will take it seriously.

this is the script i have wrote, and do not know what should i do next:

#!/bin/bash

directory=/home/zsf/pictures
for i in $directory/*


thanks
 
  


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
How to run root privileged Linux command as normal user via shell shell tcegrid Linux - Newbie 1 06-23-2008 04:38 PM
LXer: Terminal functions for shell scripting with Shell Curses LXer Syndicated Linux News 0 03-27-2008 12:50 AM
LXer: Shell tip: Set the shell prompt and themes in Linux Terminal LXer Syndicated Linux News 0 06-12-2007 04:02 AM
Alias or shell script to confirm 'exit' commands from a shell rose_bud4201 Programming 2 03-08-2006 03:34 PM
'sh' shell - Actually calls legacy Bourne shell, or uses system default? Dtsazza Linux - Software 1 10-28-2005 10:20 AM


All times are GMT -5. The time now is 02:39 AM.

Main Menu
Advertisement
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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration