What are the different types of shells available in UNIX?
There is a lot of shell available in UNIX.
Ex:- Bourne shell, Bash shell, CShell, Korn shell etc.
$cat /etc/shells <--from linux
How would you replace the n character in a file with some xyz?
cat cc | sed s/n/xyz/g
Also can be done with :g/n/s//xyz/g
How would you print just the 25th line in a file (smallest possible script please)?
awk NR==25
head -25 | nl | tail -1
How do u open a read only file in UNIX?
Using vi editor. Read only files can be opened without any issues
Anything that opens a file, can open a read only file: cat, more, less, awk, pg, et all. The chmod command you put does not make a file read only, it makes it read/write for the user/owner.
How to print nth column of a pattern/file without using awk,cut commands?
i think by modifying the IFS, we can achieve this as follows, i hope this helps. #take for example there is an entry in /etc/password with this info santarvedi:x:500:500:santarvedi:/home/santarvedi:/...
perl -ane print "$F[0]"; < test.txt
You can use this I hope , replace the zero with the column number you want to print.Normally people use cut and awk command for extracting column.
Want to delete the record if I find a word (dd)in the fixed length(5-7).
Want to delete the record if I find a word (dd)in the length(5-7). Lets think that (5-7) length is ff2. this is a fixed width file. ex f1 ff1 ff2 ff3 sds dd fd sd ss ew dd dd se o/p should be like ff1 ff2 ff3 sd ss ew
sed /dd/d $file
sed s/dd //g file
How does windows NT supports multitasking?
Windows NT supports multitasking with the help of preemptive multitasking which is based on preemptive operating system. The operating system takes control of the processor from a task in two ways: W...
preemptive multitask
How to extract the second row of a text-file?
We can use either
head -2 file.dat | tail -1 or
cat file.dat | sed -n 2p > output.dat
sed -n 2p
Write a shell script that accept 7 numbers in loop
Write a shell script that accept 7 numbers in loop,then make a sum of this numbers. Then count the numbers that are greater than 250 ?
Code
#!/bin/bash total=0 c=0 for a in {1..3}; do echo -n "Write a number : " read i if [ $i -ge 250 ] ; then let "c=c+1" fi let "total = i + total" done echo "total = $total" echo " > 250 = $c "
How would you get the character positions 10-20 from a text file?
Answered by: Suman Saha
Answered On : Jun 27th, 2005cat filename.txt | cut -c 10-20
sed -n 25p file_name
cut -c 10-20 filename
Shell script to add file extension
Write a small shell script that adds an extension ".New" to all the files in a directory.
ls -lrt | awk {priint "mv "$9" "%9".New"} | sh
Code
for i in *; do mv $i $i.New; done
How do you read arguments in a shell program - $1, $2 ?
Positional parameters are reads either by displaying there value or can be assigned to another variables.
echo $1 $2 $9 ${10} ${20}
shift operators can be used to move around the positional parameters.
example: test.sh 1 2 3 4 5
a=$1
while [ -z "$a" ]
do
echo $a
shift
done
echo $1
echo $2
$# : Stands for counts of the number of the positional parameters passed to a script.
example test.sh 1 2 3 4 5 6 7 , there are 7 paramaters as passed so the count of the $# is 7
By using $# parameter, the program checks whether the right number of arguments have been entered.
How will you list only the empty lines in a file (using grep)?
grep "^$" filename
grep -c "^$" file name
here -c option count the no.of empty line..the anchors denotes the empty line from your file name
How to find how many users have logged in and logged out in last five minutes using shell scripts?
How many users have logged in and logged out in last five or 10 minutes
Current_time=`date +%H%M`
who -u | awk $Current_time - $4 <= 5 { print }
last -5|grep -i "logged in"|wc -l
last -5|grep -v "logged out"|wc -l
How do you invoke and send mail from UNIX ?
Code
mailx -s "<subject>" <email_id list> < <input_file>
The following is a more detailed example for using mailx in shell script. This should help.
Code
SUBJECT="Email Subject Line" TO=xyz@company.com CC=`tr ' ' ', ' < mailCClist.txt` #BCC="xyz2@comp.com" echo "Send the E-mail message..." /usr/bin/mailx -s "${SUBJECT}" ${TO} <<-END ~< ! uuencode $DCdir/$FileName "Result.txt" ~c ${CC} ~b ${BCC} Hi, Blah, Blah.. body of the email. Thanks Satya ~. END echo "Mail Sent!!"
Hello,i am trying to write a UNIX shell script program to send mail to cc and to list.I am using the '-c' option for mailx UNIX command,but am getting an error illegal -c option. Could any one please guide me to use the mailx command in UNIX shell scripting to send the mail to the cc and to list along...
Code
uuencode attachment1 attachment1 | mailx -c x@yz.com,a@bc.com -s "subject" m@no.com
You should be able to use something like the following example to get the CC option working with mailx. Preferably create a list of users needed to be put in the CC option (as mailCClist.txt in the ex...
How will you write a shell script to connect to SQL database?
in vi editor
vi connect.sh
#!/bin/bash
sqlplus -s sys as sysdba <
spool on;
spool ashu;
connect scott/tiger
select * from emp;
spool off;
exit;
EOF
cat ashu
sqlplus -s username/password@schema name
:wq
How to read the contents of fie inside jar without extracting in shell script?
You can read the contents of file inside jar without extracting in shell script by using:
tar -tvf file.tar
jar tvf jarName.jar
It gives exit status with echo command
Ex:- echo $?
It gives Zero when command is successfully executed otherwise it will gives garbage value.
$? returns the status of the last command executed . if the last command is successfull then it will 0 , otherwise it will retrun non-zero. ex: date ; echo $? or try this dte ; echo $?