background preloader

UNIX

Facebook Twitter

SCP

Bash. E-Mail in Unix. Bourne / Bash shell scripting tutorial - Functions. One often-overlooked feature of Bourne shell script programming is that you can easily write functions for use within your script. This is generally done in one of two ways; with a simple script, the function is simply declared in the same file as it is called. However, when writing a suite of scripts, it is often easier to write a "library" of useful functions, and source that file at the start of the other scripts which use the functions. This will be shown later. The method is the same however it is done; we will primarily be using the first way here.

The second (library) method is basically the same, except that the command . . goes at the start of the script. There could be some confusion about whether to call shell functions procedures or functions; the definition of a function is traditionally that is returns a single value, and does not output anything. A function may return a value in one of four different ways: A simple script using a function would look like this: function.sh #! #! How to format date for display or to use in a shell script. ByVivek GiteonFebruary 27, 2007 last updated March 31, 2016 How do I format the date to display on the screen on for my shell scripts as per my requirements on Linux or Unix like operating systems? You need to use the standard date command to format date or time. You can use the same command with the shell script. Syntax The syntax is as follows for the GNU/date and BSD/date command: date +FORMAT date +"%FORMAT" date +"%FORMAT%FORMAT" date +"%FORMAT-%FORMAT" An operand with a leading plus (+) sign signals a user-defined format string which specifies the format in which to display the date and time.

Task: Display date in mm-dd-yy format Open a terminal and type the following date command: $ date +"%m-%d-%y" Sample outputs: To turn on 4 digit year display: $ date +"%m-%d-%Y" Just display date as mm/dd/yy format: $ date +"%D" Task: Display time only Type the following command: $ date +"%T" Sample outputs: To display locale’s 12-hour clock time, enter: $ date +"%r" Sample outputs: A sample shell script. Unix/Linux Technical Consultant [ steve-parker.org ] UNIX / Linux Bourne / Bash Shell Scripting Tutorial [ steve-parker.org ] Exit codes are a number between 0 and 255, which is returned by any Unix command when it returns control to its parent process.

Other numbers can be used, but these are treated modulo 256, so exit -10 is equivalent to exit 246, and exit 257 is equivalent to exit 1. These can be used within a shell script to change the flow of execution depending on the success or failure of commands executed. This was briefly introduced in Variables - Part II. Here we shall look in more detail in the available interpretations of exit codes. Success is traditionally represented with exit 0; failure is normally indicated with a non-zero exit-code. This value can indicate different reasons for failure. We shall look at three different methods for checking error status, and discuss the pros and cons of each approach. Firstly, the simple approach: echo "USERNAME: $USERNAME"echo "NAME: $NAME"echo "HOMEDIR: $HOMEDIR" This script works fine if you supply a valid username in /etc/passwd.

Why is this? #! #! #! #! Or. UNIX / Linux Bourne / Bash Shell Scripting Tutorial [ steve-parker.org ] UNIX Shell Script Tutorials & Reference. The next few sections are all based on the syntax rules for the Bourne shell as listed in the man pages for sh from my system. Compare these rules with those of your system. There may be slight differences where the specification of the sh is loosely defined. Where there are these minor differences, experiment for yourself to validate that what is written agrees with what happens. Trust no-one and test everything until you are happy you fully understand each point. The Borne Shell is available in three forms on most systems.

Sh The Standard Borne Shell jsh The Job Control Bourne Shell rsh The Restricted Bourne Shell The syntax and usage rules are common across all these types except where noted. Lets look at the standard man page information and interpret what this represents in some real world examples. Invocation Flags: In square brackets following the command name is a list of flag parameters which modify the way the command behaves. Definitions: Blanks: A blank is a tab or space. . #! Advanced Bash-Scripting Guide. UNIX Shell Script Tutorials & Reference. Why Use Shells?

Well, most likely because the are a simple way to string together a bunch of UNIX commands for execution at any time without the need for prior compilation. Also because its generally fast to get a script going. Not forgetting the ease with which other scripters can read the code and understand what is happening. Lastly, they are generally completely portable across the whole UNIX world, as long as they have been written to a common standard. The Shell History: The basic shells come in three main language forms. Bourne Shell: Historically the sh language was the first to be created and goes under the name of The Bourne Shell. C Shell: Next up was The C Shell (csh), so called because of the similar syntactical structures to the C language. Korne Shell: Lastly we come to The Korne Shell (ksh) made famous by IBM's AIX flavour of UNIX. Read That Manual! The other thing you must do is cultivate an ability to read through these man pages and understand the meaning behind the words.

Unix/Linux Technical Consultant [ steve-parker.org ] BigAdmin Shell Commands. Oracle acquired Sun Microsystems in 2010, and since that time Oracle's hardware and software engineers have worked side-by-side to build fully integrated systems and optimized solutions designed to achieve performance levels that are unmatched in the industry. Early examples include the Oracle Exadata Database Machine X2-8, and the first Oracle Exalogic Elastic Cloud, both introduced in late 2010. During 2011, Oracle introduced the SPARC SuperCluster T4-4, a general-purpose, engineered system with Oracle Solaris that delivered record-breaking performance on a series of enterprise benchmarks. Oracle's SPARC-based systems are some of the most scalable, reliable, and secure products available today. Sun's prized software portfolio has continued to develop as well, with new releases of Oracle Solaris, MySQL, and the recent introduction of Java 7.

Oracle invests in innovation by designing hardware and software systems that are engineered to work together. Examples of Usage of Unix Find Command. Unix Shell Tips & Tricks. Unix File Finding Commands.Find File Shell Script. Search File Unix ... Following are some bunch of commands that might be useful if you want to find files in unix/linux. Large Files Find files larger than 10MB in the current directory downwards… Find files larger than 100MB… Old Files Find files last modified over 30days ago… Find files last modified over 365days ago… Find files last accessed over 30days ago… Find files last accessed over 365days ago… Find Recently Updated Files There have been instances where a runaway process is seemingly using up any and all space left on a partition. If the file is being updated at the current time then we can use find to find files modified in the last day… Better still, if we know a file is being written to now, we can touch a file and ask the find command to list any files updated after the timestamp of that file, which will logically then list the rogue file in question.

Finding tar Files A clean up of redundant tar (backup) files, after completing a piece of work say, is sometimes forgotten. Large Directories e.g. Bash While Loop Example. How do I use bash while loop to repeat certain task under Linux / UNIX operating system? How do I set infinite loops using while statement? The bash while loop is a control flow statement that allows code or commands to be executed repeatedly based on a given condition.

For example, run echo command 5 times or read text file line by line or evaluate the options passed on the command line for a script. bash while loop syntax The syntax is as follows: while [ condition ]do command1 command2 command3 done command1 to command3 will be executed repeatedly till condition is true. . #! Here is a sample shell code to calculate factorial using while loop: #! To run just type: $ chmod +x script.sh $ . While loops are frequently used for reading data line by line from file: #! You can easily evaluate the options passed on the command line for a script using while loop: How do I use while as infinite loops? Infinite for while can be created with empty expressions, such as: #! #! Recommended readings: Unix commands. Solaris 10 User Commands.

Bourne shell script need help please ? by dezithug - UNIX for Advanced ... Well, first off, it helped me a lot to look at the code with indents in it: Code: #! /bin/ksh count=1 val=$2 op=$1 ans=0 if [ $op = "-e" -o $op = "-o" ] then if [ $op = "-e" ] then while [ $count -le $val ] do ans=`expr $count % 2` if [ $ans -eq 0 ] then echo "$count \c " count=`expr $count + 1` fi done elif [ $op = "-o" ] then while [ $count -le $val ] do ans=`expr $count % 2` if [ $ans -ne 0 ] then echo "$count \c " count=`expr $count + 1` fi done fi else while [ $count -le $val ] do echo "$count \c " count=`expr $count + 1` done fi Now, the first thing I usually try when trying to figureout what my script is doing is to add a "set -x" line at the top of the script.

That will echo out each step the shell is performing, so you can see what's going on... So, The way I ran the script, (I'll give an example of what I think I saw trying to use the" -e 4 "option) it got stuck in a loop at: + ans=1 + '[' 1 -eq 0 ']' + '[' 1 -le 4 ']' ++ expr 1 % 2 Ok, interesting... let's look at this. BASH Programming - Introduction HOW-TO. By Mike G mikkey at dynamo.com.ar Thu Jul 27 09:36:18 ART 2000 This article intends to help you to start programming basic-intermediate shell scripts. It does not intend to be an advanced document (see the title). I am NOT an expert nor guru shell programmer. I decided to write this because I'll learn a lot and it might be useful to other people. Any feedback will be apreciated, specially in the patch form :) 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 13.1 Ways Calling BASH 14. 1. 1.1 Getting the latest version 1.2 Requisites Familiarity with GNU/Linux command lines, and familiarity with basic programming concepts is helpful. 1.3 Uses of this document This document tries to be useful in the following situations You have an idea about programming and you want to start coding some shell scripts. 2.

This HOW-TO will try to give you some hints about shell script programming strongly based on examples. 2.1 Traditional hello world script #! #! 3. C. Shell Programming Techniques. Oracle Technology Network > Java Software Downloads View All Downloads Top Downloads New Downloads What's New Java in the Cloud: Rapidly develop and deploy Java business applications in the cloud. Essential Links Developer Spotlight Java EE—the Most Lightweight Enterprise Framework? Blogs Technologies Contact Us About Oracle Cloud Events Top Actions News Key Topics Oracle Integrated Cloud Applications & Platform Services.

Bash Shell Loop Over Set of Files. How do I run shell loop over set of files stored in a current directory or specified directory? You can use for loop easily over a set of shell file under bash or any other UNIX shell using wild card character. Syntax The general syntax is as follows: for f in file1 file2 file3 file5 do echo "Processing $f" # do something on $f done You can also use shell variables: FILES="file1 /path/to/file2 /etc/resolv.conf" for f in $FILES do echo "Processing $f" done You can loop through all files such as *.c, enter: $ for f in *.c; do echo "Processing $f file Sample Shell Script To Loop Through All Files #!

Filename Expansion You can do filename expansion in loop such as work on all pdf files in current directory: for f in *.pdf do echo "Removing password for pdf file - $f" done However, there is one problem with the above syntax. . #! Using A Shell Variable And While Loop You can read list of files from a text file. File1 file2 file3 #! Processing Command Line Arguments #!

#! UNIX SQL. How-to-run-a-unix-host-command-SQL. How-call-pl-sql-unix-script. Sample code as following: test_sql(){ #test#echo test_sql str=`$ORACLE_BIN/sqlplus -s $user/$passwd <<EOM set verify off set heading off set feedback off #--------start pl/sql { DECLARE CURSOR pah_cs IS select id from table where letter = 'abcd'; temp number; echo "-------" BEGIN OPEN pah_cs; LOOP FETCH pah_cs INTO temp; EXIT WHEN pah_cs%NOTFOUND; echo $temp > tt.csv 2>&1; ND LOOP; CLOSE pah_cs; END; } exit EOM` } test_sql I want to use call pl/sql in unix script and put the output into another file and I want the file format is saving the output lilne by line. but now it seems my pl/sql script hasnt been called at all.

Dunno what the problem is, anyone could help me?? Many thanks. Tips File. Tips File Last Updated 05-Jul-02 General Application Tips Chane DB Name and File paths A common way to rename all the database and all data files is to rebuild the control file. This can be very labor intensive, however, and possibly dangerous if you have a large number of data files. Step 1 Run the scripts below (change directories for your server) with the target database open. Yes, we will be trying to move the data files more then once, however, Oracle checks whether the data file exists before accepting the command and returns errors if it does not exist. Data File Script select 'alter database rename file '''||file_name||''' to ', '''/ora01/oradata/sid/'|| substr(file_name, instr(file_name,'/',-1)+1) ||''';' from dba_data_Files Redo Log Script select 'alter database rename file '''||member||''' to ', '''/ora01/oradata/sid/'|| substr(member, instr(member,'/',-1)+1) ||''';' from v$logfile; Step 2 SHUTDOWN the target database and STARTUP MOUNT.

Run each of the scripts your have created. Step 3 1. Tips File.