0% found this document useful (0 votes)
91 views

Shell Scripting - Username/Password

The document discusses several shell scripting examples involving username/password input, fork(), exec(), file descriptors, signals, and process execution. Key points include: 1. A shell script that prompts for username and password input without displaying the password. 2. Examples of fork() to create child processes and demonstrate process IDs. 3. Using exec() to launch a new program from within a child process after forking. 4. Examples manipulating file descriptors and using them for inter-process communication. 5. Examples handling SIGCHLD signals from child processes. 6. Demonstrating how a child process exits immediately using _exit() while the parent continues.

Uploaded by

Nataraj Bhargava
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
91 views

Shell Scripting - Username/Password

The document discusses several shell scripting examples involving username/password input, fork(), exec(), file descriptors, signals, and process execution. Key points include: 1. A shell script that prompts for username and password input without displaying the password. 2. Examples of fork() to create child processes and demonstrate process IDs. 3. Using exec() to launch a new program from within a child process after forking. 4. Examples manipulating file descriptors and using them for inter-process communication. 5. Examples handling SIGCHLD signals from child processes. 6. Demonstrating how a child process exits immediately using _exit() while the parent continues.

Uploaded by

Nataraj Bhargava
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Shell Scripting - Username/Password

#! /bin/sh echo -n "Username: " read username echo -n "Password: " while [ 1 ] do read -s -n1 key echo -n "*" if [[ $key == "" ]]; # checks whether enter is pressed then exit 0 # exit with return value 0 fi exit 0 # bash interpreter. Tell the OS that it is bash interpreter # prompt it on the console; -n means no new line # reads the input from the console and assign it to username

# reads the input; -n 1 means read only one character at a time; -s means silent, it means don't display what is read

1. fork1.c #include<unistd.h> #include<stdio.h> int main (void) { pid_tp; p=fork(); printf(fork returned%d\n,p); } $gcc fork1.c-o fork1 $./fork1 fork returned 0 fork returned 698 1A. #include <stdio.h> #include <unistd.h> int main (void){ printf(Hello World!\n); fork(); printf(\tI am process%d.\n,getpid());

} 1B. #include<stdio.h> #include<unistd.h> int main (void)){ int pid; printf(Hello world!\n); pid=fork() if(pid=0) printf( I am the child process.\n); else printf( I am the parent process.\n); } 1C. #include<stdio.h> #include<unistd.h> int main (void) fork(); fork(); fork(); printf(Hello World from process%d!\n,getpid()); } 1D. Guarantees the child process will print its message before the parent process. #include<stdio.h> #include<sys/wait.h> int main (void){ int pid; int status; printf(Hello World!\n); pid=fork(); if(pid==-1){ perror(bad fork); exit(1); } if(pid==0) printf( I am the child process.\n); else{ wait(&staurs); printf( I am the parent process.\n); } }

2. $ cat fork2.c #include<stdio.h> #include<unistd.h> int main (void) pid_tp; printf(Original program,pid=%d\n,getpid()); p=fork(); if(p==0){ printf( In child process,pid=%d,ppid=%d\n,getpid(), getppid()); }else{ printf(In parent, pid=%d, fork returned=%d\n,getpid(),p); } } $gcc fork2.c-o fork2 $./fork2 Original program, pid=767 In child process, pid=768, ppud=767 In parent, pid=767,fork returned=768 3. $ cat exec1.c #include<unistd.h> #include<stdio.h> int main (void){ /*Define a null terminated array of the command to run followed by any parameters, in this case none*/char*age[]={/usr/bin/ls,0}; /*fork, and exec within child process*/ if (fork()==0) { printf( In child process:\n); execv(arg[0], arg); printf(I will never be called\n); } printf(Execution continues in parent process\n); } $gcc exec1.c-o exec1 $./exec1 In child process: Fork1.c exec1 fork2 exec1.c Fork2.c Execution continues in parent process

fork1

4. #include<stdio.h> #include<strings.h> #include<unistd.h> #include<sys/types.h> #include<sys/stat.h> #include<fcnt1.h> int main (void) { int fd_in, fd_out; char buf[1024]; memset(buf,0,1024); } sleep(10); } Output: $gcc fdtest1.c-o fdtest1 $./fdtest1 2875:1 2874:2 2875:3 2874:4 2875:5 2874:6 2874:7 Sunbox$cat/tmp/outfile 2875Hello,world! 2874Hello,world! 2875Hello,world! 2874Hello,world! 2875Hello,world! 2874Hello,world! 2874Hello,world! 5. #include<unistd.h> #include<stdio.h> int main(void){ int i; if (fork()){ /*Parent*/ sleep(2) _exit(0); } for(i=0;i<5;i++){

printf(My parent is %d\n,getppid()); sleep(1); } } $gcc die1.c-o die1 $./die1 My parent is 2920 My parent is 2920 Sunbox$ My parent is 1 My parent is 1 My parent is 1

6. $cat die2.c #include<unistd.h> #include<stdio.h> int main(void){ int i; if (!fork()){ /*Child exists immediately*/ _exit(0); } /*Parent waits around for a minute*/ sleep(60); } $gcc die2.c-o die2 $./die2& [1] 2934 $pa-ef|grep 2934 sean 2934 2885 0 21:43:05 pts/1 0:00./die2 sean 2935 2934 0 -? 0:00<defunct> $ps-ef|grep 2934 [1]+Exit 199 /die2 7. #include<unistd.h> #include<stdio.h> #include<sys/types.h> #include<sys/wait.h> void sighandler(int sig){ printf(In signal handler for signal%d/n,sig); /*wait0 is the key to acknowledging the SIGGHLD*/ wait(0); }

int main (void) { int i; /*Assign a signal handler to SIGCHLD*/ sigset(SIGCHLD, & sighandler); if(!fork()) { /*child*/ _exit(o); } sleep(60); } $gcc die3.c-o die3 $./die3& [1]3116 $ In signal handler for signal 18 ps-ef|grep 3116 sean 3116 2885 0 22:37:26pts/1 0:00./die3

8. main() {printf(Files in Directory are:n); excel(.bin.ls, ls, -1,0); } 9. main(){ int return_value; printf(Forking processn); fork(); printf(The process id is %d and return value is %dn, getpid(),return_value); excel(/bin/ls/, ls, -1,0); printf ( This line is not printedn); }

Output: Forking process The process id is 6753 and return value is 0 The process id is 6754 and return value is 0 Two lists of files in current directory

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy