Visit Our UNIX and Linux User Community


Chunks of bash shell programming


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Chunks of bash shell programming
# 1  
Old 09-11-2013
Chunks of bash shell programming

I am going to provide a chunks of codes that I do not understand. Please help with them in a layman's terms.
Code:
1) ${DEBUG:-0}

--------------------------------------------------------------------------

2) print "${1}"

--------------------------------------------------------------------------

3) function logmsg {
    if [ ! -f ${MAILFILE} ];
    then
        start_mail
    fi
    print "${1}" | ${TEE} -a ${MAILFILE}
    }

--------------------------------------------------------------------------

4) function debugmsg {
    if [ ${DEBUG} -ne 1 ];
    then
        return
    fi
    logmsg "${1}"
    }

--------------------------------------------------------------------------

5) H="How are you"

--------------------------------------------------------------------------

6) if [ ! -z "${msg}" ];

--------------------------------------------------------------------------


Last edited by vbe; 09-11-2013 at 02:04 PM.. Reason: thats no reason to not use code tags!
# 2  
Old 09-11-2013
This looks almost like homework so I'm not gonna go into much details. If you have any specific questions post them back here.

1. Expand the variable "DEBUG" *if, and only if* it's already defined, else use zero.
2. Print the contents of the first script argument.
3. Defines a function that checks if a file does not exist. "tee" is a specific purpose command; Take a look at the man page (man tee).
4. Defines another function that checks if the contents of $DEBUG aren't equal to 1 (numeric value, not string).
5. String assignment.
6. "-z" is a comparison operator used to tell if string has zero length.

In all cases the syntax ${var} is used to "protect" the variables. E.g:
Code:
var1="asdf"
echo "${var1}2"
echo "$var12"

One of the above "echo" statements will work fine and the other will throw an error. Try it for yourself to find out which one fails and why. Smilie
# 3  
Old 09-11-2013
Thanks. I do not have a way to try those codes. In the office, I do not want to try it out in production box. In my personal laptop, I do not even have unix. But with whatever little knowledge I have, I can say that

1. var1="asdf"

2. echo "${var1}2"

3. echo "$var12"

1. will work because we are just assigning some characters to a variable named var1.
2. will work. It will give the result as asdf2.
3. will not work as there is no such variable named var12.

I hope you did not give these codes to show that I don't know anything about shell scripting. Though I myself admit that I know nothing about shell scripting. But I would like to learn it quite fast !

Last edited by Scott; 09-22-2013 at 01:19 AM.. Reason: Code tags
# 4  
Old 09-11-2013
Quote:
Originally Posted by lg123
I hope you did not give these codes to show that I don't know anything about shell scripting. Though I myself admit that I know nothing about shell scripting. But I would like to learn it quite fast !
No one around here will post a code just to show off or make other people feel bad for not being able to write a script or solve X or Y problem.

You're welcome (and encouraged) to post any questions you may have.

Homework is still welcome but some restrictions apply -- usually in those cases people are asked to show some effort on the issue and not just ask "please do my homework".

In any case, you guessed right about the example code. Sorry if I thought this was homework.

Edit: Also, kudos for not running the above in a production box. Although the code is 100% safe, it's good to know that nobody is using a production box as a lab environment. More than once I've seen someone "accidentally" change the hostname to ´ (that's a backtick) or similar because they were learning how to use the "hostname" command in a production server.

Last edited by verdepollo; 09-11-2013 at 05:45 PM..
# 5  
Old 09-11-2013
I think the best progress you can make in *nix scripting is playing around with it (did you try to install "busybox" on your personal laptop?) while reading man pages (fourth item on this forum's top menu) and solving/copying problem examples like in these fora.
# 6  
Old 09-11-2013
Quote:
Originally Posted by lg123
Thanks. I do not have a way to try those codes. In the office, I do not want to try it out in production box. In my personal laptop, I do not even have unix.
I suggest busybox for windows for that problem. Run busybox bash in a CMD prompt and you'll have a fairly complete UNIX shell along with most of the utilities people expect to use with one.

It's not actually UNIX though, so there'll still be some fundamental differences (case-insensitive files, no special device files or named pipes, etc, etc)

Last edited by Corona688; 09-12-2013 at 01:30 PM..
# 7  
Old 09-22-2013
Code:
A=${0##*/}
S=${0%/*}

Please explain me with examples for the above 2 statements.
Moderator's Comments:
Mod Comment Please use CODE tags when posting sample code, input, and output!

Last edited by Don Cragun; 09-22-2013 at 01:39 AM.. Reason: Add CODE tags

Previous Thread | Next Thread
Test Your Knowledge in Computers #352
Difficulty: Easy
Unix was created 25 years before Linux.
True or False?

10 More Discussions You Might Find Interesting

1. Linux

Bash Programming. Could anyone help me?

Hi! I'm new in bash programming and I need to make an script that reads a txt file line by line (every line is like "Name;FirstLastName;SecondLastName;Sex") and has to give every student 3 random marks, its average and add a counter if they are male/female and if they passed. I've been trying with... (4 Replies)
Discussion started by: HeartHacker
4 Replies

2. Homework & Coursework Questions

Bash Shell Programming assignment.

Please take a look I am stuck on step 4 1. The problem statement, all variables and given/known data: #!/bin/bash ### ULI101 - ASSIGNMENT #2 (PART A) - DUE DATE Wed, Aug 3, 2011, before 12 midnight. ###==================================================================================== ###... (13 Replies)
Discussion started by: almirzaee
13 Replies

3. Shell Programming and Scripting

Bash programming help

Hello all; I have little knowledge of bash programming, and I was wondering if you could help me out. I would like to create a bash script, as detailed below. process.sh is the bash script itself list.dl is a text file. In this text file, the first line is the directory from... (1 Reply)
Discussion started by: tehalexf
1 Replies

4. Shell Programming and Scripting

Can I use a shell script for deleting chunks from a watch folder?

Hello I have a unique problem of needing to delete large files slowly off of an XSan. I was wondering if there is a script I could use to delete 100gb chunks of files and folders that get placed in to a watch folder, slowly so as not to disrupt the other users. I would like to use Automator in... (0 Replies)
Discussion started by: ajsoto
0 Replies

5. Shell Programming and Scripting

bash programming

how do you allow a user to input a particular group name as an argument?? thnx for your time. (2 Replies)
Discussion started by: bumdeal2
2 Replies

6. Shell Programming and Scripting

Bash Programming

Write a Shell script to analyse the disk usage of students in a particular class. The program should allow the user running the script to input the particular class name (group name) as an argument to the script. The script should create a file called “logfile” in the home directory of the user... (2 Replies)
Discussion started by: bumdeal2
2 Replies

7. UNIX for Dummies Questions & Answers

Help with date command in bash shell programming

Doubt #1 I have a program that I want the user to input date. When the user inputs the date, is it able to format it to system date dd-mm-yyy and then echo the value into a text file? Doubt#2 If the above is not going to work, I tried to have the system date appear in the user input field and... (6 Replies)
Discussion started by: frossie
6 Replies

8. Shell Programming and Scripting

grep'ing and sed'ing chunks in bash... need help on speeding up a log parser.

I have a file that is 20 - 80+ MB in size that is a certain type of log file. It logs one of our processes and this process is multi-threaded. Therefore the log file is kind of a mess. Here's an example: The logfile looks like: "DATE TIME - THREAD ID - Details", and a new file is created... (4 Replies)
Discussion started by: elinenbe
4 Replies

9. Shell Programming and Scripting

help bash programming

how to i grep a keyword which ignore upper case or lower case. for an example : user enter CoUcH and the grep also can grep couch which in text.txt although upper or loever case is different .. how to do that ? any idea? (1 Reply)
Discussion started by: CheeSen
1 Replies

10. Shell Programming and Scripting

Programming in ksh and not in bash

Hi all.... I was wondering if there is a lot of differences between /bash and ksh programming. Im learning about bash programming, but all what I had read in this forum, points me to learn more about ksh over bash. Is that right and why? Unfortunately, I can’t write ksh progs on my work,... (2 Replies)
Discussion started by: TARFU
2 Replies

Featured Tech Videos