1. Contoh skrip Bash di linux menggunakan perintah {if.. else..} untuk membuat pilihan menu daftar sembako dengan harga, terdapat 2 input, input pertama: nomor sembako, input kedua: jumlah item, hasl output: harga yang harus dibayar.
#!/bin/bash
clear
echo"Daftar Belanja ";
echo "-------------- ";
echo "1. Gulaku 1kg@20.000 ";
echo "2. Garam 150g@2.000 ";
echo "3. Ladaku 10g@1.000 ";
echo "4. Minyak 1liter@80.000 ";
echo "5. Sambel Terasi 200g@4.000 ";
echo "read -p "Pilihan anda [1-6] :" pil;
if [ $pil -eq 1 ];
then
echo "Banyak kilo gram =";
read jum
let bayar=jum*20.000
elif [ $angka -eq 2 ];
then
echo "Banyak gram =";
read jum
let bayar=jum*2000
elif [ $angka -eq 3 ];
then
echo "Banyak gram =";
read jum
let bayar=jum*1.500
elif [ $angka -eq 4 ];
then
echo "Banyak liter =";
read jum
let bayar=jum*80.000
elif [ $angka -eq 5 ];
then
elif [ $angma -eq 6 ];
then
esit 0
else
echo "Sorry, tidak tersedia"
exit 1
fi
echo "Harga bayar = Rp. $bayar"
echo "Terimakasih"
[Sulfiani@linux$]./belanja.sk
Daftar Belanja
------------------
1. Gulaku 1kg@20.000
2. Garam 150g@2.000
3. Ladaku 10g@1.500
4. Minyak 1liter@80.000
5. Sambel Terasi 200g@4.000
6. Exit
input > Pilihan anda :2
input > Banyak barang =3
outpu > Harga bayar = Rp. 6.000
Terimakasih
2. Contoh pembuatan skrip membuat Jendela/Windows.
Contoh :
Window Border example
#include <ncurses.h>
WINDOW *create_newwin(int height, int width, int starty, int startx); void destroy_win(WINDOW *local_win);
int main(int argc, char *argv[])
{ WINDOW *my_win;
int startx, starty, width, height;
int ch;
cbreak(); /* Line buffering disabled, Pass on
* everty thing to me */
keypad(stdscr, TRUE); /* I need that nifty F1 */
height = 3;
width = 10;
starty = (LINES − height) / 2; /* Calculating for a center placement */
startx = (COLS − width) / 2; /* of the window */
printw("Press F1 to exit");
refresh();
my_win = create_newwin(height, width, starty, startx);
Penjelasan
Datatypes
The ncurses library permits manipulation of data structures, called
windows, which can be thought of as two-dimensional arrays of charac-
ters representing all or part of a CRT screen. A default window called
stdscr, which is the size of the terminal screen, is supplied. Others
may be created with newwin.
Note that curses does not handle overlapping windows, that's done by
the panel(3x) library. This means that you can either use stdscr or
divide the screen into tiled windows and not using stdscr at all. Mix-
ing the two will result in unpredictable, and undesired, effects.
Windows are referred to by variables declared as WINDOW *. These data
structures are manipulated with routines described here and elsewhere
in the ncurses manual pages. Among those, the most basic routines are
move and addch. More general versions of these routines are included
with names beginning with w, allowing the user to specify a window.
The routines not beginning with w affect stdscr.
After using routines to manipulate a window, refresh(3x) is called,
telling curses to make the user's CRT screen look like stdscr. The
characters in a window are actually of type chtype, (character and
attribute data) so that other information about the character may also
be stored with each character.
Special windows called pads may also be manipulated. These are windows
which are not constrained to the size of the screen and whose contents
need not be completely displayed. See curs_pad(3x) for more informa-
tion.
In addition to drawing characters on the screen, video attributes and
colors may be supported, causing the characters to show up in such
modes as underlined, in reverse video, or in color on terminals that
support such display enhancements. Line drawing characters may be
specified to be output. On input, curses is also able to translate
arrow and function keys that transmit escape sequences into single val-
ues. The video attributes, line drawing characters, and input values
use names, defined in <curses.h>, such as A_REVERSE, ACS_HLINE, and
KEY_LEFT.
Environment variables
If the environment variables LINES and COLUMNS are set, or if the pro-
gram is executing in a window environment, line and column information
in the environment will override information read by terminfo. This
would affect a program running in an AT&T 630 layer, for example, where
the size of a screen is changeable (see ENVIRONMENT).
If the environment variable TERMINFO is defined, any program using
curses checks for a local terminal definition before checking in the
standard place. For example, if TERM is set to att4424, then the com-
piled terminal definition is found in
/usr/share/terminfo/a/att4424.
(The a is copied from the first letter of att4424 to avoid creation of
huge directories.) However, if TERMINFO is set to $HOME/myterms,
curses first checks
$HOME/myterms/a/att4424,
and if that fails, it then checks
/usr/share/terminfo/a/att4424.
This is useful for developing experimental definitions or when write
permission in /usr/share/terminfo is not available.
The integer variables LINES and COLS are defined in <curses.h> and will
be filled in by initscr with the size of the screen. The constants
TRUE and FALSE have the values 1 and 0, respectively.
The curses routines also define the WINDOW * variable curscr which is
used for certain low-level operations like clearing and redrawing a
screen containing garbage. The curscr can be used in only a few rou-
tines.
Routine and Argument Names
Many curses routines have two or more versions. The routines prefixed
with w require a window argument. The routines prefixed with p require
a pad argument. Those without a prefix generally use stdscr.
The routines prefixed with mv require a y and x coordinate to move to
before performing the appropriate action. The mv routines imply a call
to move before the call to the other routine. The coordinate y always
refers to the row (of the window), and x always refers to the column.
The upper left-hand corner is always (0,0), not (1,1).
The routines prefixed with mvw take both a window argument and x and y
coordinates. The window argument is always specified before the coor-
dinates.
In each case, win is the window affected, and pad is the pad affected;
win and pad are always pointers to type WINDOW.
Option setting routines require a Boolean flag bf with the value TRUE
or FALSE; bf is always of type bool. Most of the data types used in
the library routines, such as WINDOW, SCREEN, bool, and chtype are
defined in <curses.h>. Types used for the terminfo routines such as
TERMINAL are defined in <term.h>.
This manual page describes functions which may appear in any configura-
tion of the library. There are two common configurations of the
library:
ncurses
the "normal" library, which handles 8-bit characters. The nor-
mal (8-bit) library stores characters combined with attributes
in chtype data.
Attributes alone (no corresponding character) may be stored in
chtype or the equivalent attr_t data. In either case, the data
is stored in something like an integer.
Each cell (row and column) in a WINDOW is stored as a chtype.
ncursesw
the so-called "wide" library, which handles multibyte charac-
ters (see the section on ALTERNATE CONFIGURATIONS). The "wide"
library includes all of the calls from the "normal" library.
It adds about one third more calls using data types which store
multibyte characters:
cchar_t
corresponds to chtype. However it is a structure, because
more data is stored than can fit into an integer. The
characters are large enough to require a full integer
value - and there may be more than one character per cell.
The video attributes and color are stored in separate
fields of the structure.
Each cell (row and column) in a WINDOW is stored as a
cchar_t.
The setcchar(3x) and getcchar(3x) functions store and
retrieve the data from a cchar_t structure.
wchar_t
stores a "wide" character. Like chtype, this may be an
integer.
wint_t
stores a wchar_t or WEOF - not the same, though both may
have the same size.
The "wide" library provides new functions which are analogous
to functions in the "normal" library. There is a naming con-
vention which relates many of the normal/wide variants: a "_w"
is inserted into the name. For example, waddch becomes
wadd_wch.
Tidak ada komentar:
Posting Komentar