i made this tasks. and i need some explenation or just remake my code.

Hello i hope this post is ok! and i hope that i get the point of rules :slight_smile:
i made this tasks by my self but few of them arent working.. and i dont know why!?
u think u could help me? to give me some reasons why dont they work.. and remake my code that will work?

hope to get answer soon!

greetz

  1. Write a shell program which renames the current directory with the given file extension to another extension. The playoffs are given on the command line.

Example usage:

$ Rename *.txt to *.doc

will be renamed:

aaa.txt in aaa.doc
Juhutxt in Juhudoc
...

 i have done something like this..

#!/bin/bash

#program preimenuje koncnice datotek: naprimer .dat v .txt


for i in *$1; do 

    mv "$i" "${i%$1}$2";    ## "$1" je prvi "$2" pa drugi argument

done

exit

 
  • To solve, you can also help with sed or awk commands.

*********************************************************

  1. Write a shell program to check once every five minutes if the system appears a process and attempt to kill him. Name of the process we give the command line.

Example usage:

$ Kill firefox

computer will be checked every five minutes, if any process running called Firefox, and will try to kill him. Hint: Be sure to use the kill command.

 i have done something like this..

#!/bin/bash

while true
	do
ps -e | grep $1 ##ps -e "see every process on system using standard syntax:"--
	a=$?
if [ $a -eq 0 ];then
	kill `pidof $1`   ## kill proces with pidof, wich was added like arg1 
else
echo "this proces does not exsist"
	fi
	
	sleep 300
	
done

exit 1

fi
 

*********************************************************

  1. Write a shell program, which checks the file / etc / passwd and displays the number of processes for each user.

Example output: Processes

45 root
peter 2
Exercise 8

  • Look at solving the structure of the file / etc / passwd
 i have done something like this..

#!/bin/bash

for k in `cat /etc/passwd|cut -d: -f1` 
do
 echo "$k" `ps -U $k | wc -l`
 
done

exit

*********************************************************

  1. 5.Write a shell program that records the daily availability of computers. As the argument we give him a file containing a list of computers, the program should check every five minutes if they are available. In a file (log) to record, if any of the computers are not reachable, the name and the time was not available.

Example usage:

$ Check list_of_computers.txt

prints in the book:

io.fri.uni-lj.si ni dosegljiv ob 15:31
io.fri.uni-lj.si ni dosegljiv ob 15:36
io.fri.uni-lj.si ni dosegljiv ob 15:41
verbena.fe.uni-lj.si ni dosegljiv ob 15:41
...

 i have done something like this..

#!/bin/bash	

while true do

  for rac in `cat $1` do	
  
    ping -c 3 $rac || echo "Ta racunalnik $i ob `date +%T` ni dosegljiv" >>dnevnik_nedosegljivih.txt (kamor se vpi�e njegovo ime in as, ko ni bil dosegljiv)
	
   done
   
 sleep 300 #(5min*60sec=300 sec)
 
done

exit 0

*********************************************************

  1. Write a shell program, which scans all files in the specified directory and its poddirektorijih and displays the current file, (the date of last change).

Example output:

The latest $ / usr / etc
/ Usr / etc / httpd / httpd Oct 25 12:16

 i have done something like this..

#!/bin/bash

find $1 -not -type d -printf "%p %Aa %Ad %AH:%AM \n"  | tail -1

exit

*********************************************************

Faculty of Computer and Information Science
Slovenia, Ljubljana
Prof. Peer
Spletna u?ilnica FRI

  1. Track availability of computers
1 Like

tywm for that..
but i was wondering if u can make something like that?

 i have done something like this..

#!/bin/bash	

while true do

  for rac in `cat $1` do	
  
    ping -c 3 $rac || echo "Ta racunalnik $i ob `date +%T` ni dosegljiv" >>dnevnik_nedosegljivih.txt (kamor se vpi�e njegovo ime in as, ko ni bil dosegljiv)
	
   done
   
 sleep 300 #(5min*60sec=300 sec)
 
done

exit 0
  1. User processes

---------- Post updated at 19:26 ---------- Previous update was at 19:20 ----------

Yep, but don't forget to comment the comment, see red mark:

and don't forget the semicolon in the for line, also see red mark.

---------- Post updated at 20:08 ---------- Previous update was at 19:26 ----------

  1. Can you accept usage like rename .txt .doc :confused: If yes, check this:
#!/bin/sh

for i in *$1; do
mv "$i" "${i%.*}$2"
done

Test run:

$ ls
gport.py	label.py	monk.py		rename.sh	test.py
$ ./rename.sh .py .pl
$ ls
gport.pl	label.pl	monk.pl		rename.sh	test.pl
$ 
1 Like
#!/bin/sh

for i in *$1; do
mv "$i" "${i%.*}$2"
done

Test run:

this one makes this

mv: `new file' and `new file' are the same file
mv: `vaja1' and `vaja1' are the same file

so this just deletes extensions..?

How did you exactly call it?
Are you sure you have also supplied the second arg :confused:
I also run this script with only one argument and it deleted the extensions.

i name it preimenuj.sh..

and i dont know about 2nd arg :S
u have any idea?

:rolleyes:

Of course :cool: Do following:

$ touch document-01.doc
$ touch document-02.doc
$ touch document-03.doc
$ 
$ ./preimenuj.sh .doc .txt
$ 
$ ls docu*
document-01.txt	document-02.txt	document-03.txt
$ 

1st arg 2nd arg

1 Like

what about this one?

The bash write a program to check the password to verify the validity of a given password according to the given conditions! The password should be specified on the program. The current password must meet the following conditions:

1) The minimum length to eight characters;

2) At least two characters must be numeric characters, and

3) must contain at least two of the following non-alphanumeric characters: @, #, $ or%.

Check this: Check password strength :b:

---------- Post updated at 23:17 ---------- Previous update was at 23:09 ----------

---------- Post updated 06-01-10 at 03:21 ---------- Previous update was 05-31-10 at 23:17 ----------

#!/bin/sh

find $1 -type f -exec stat -f "%m%Sm %N" '{}' 2>/dev/null + | \
sort -nr | head -1 | \
sed 's/[0-9]*//;s/:[0-9 ]*\// \//;s/\([^/]*\)\(.*\)/\2 \1/'

Test run:

$ ./Findlatest.sh ~/code
/home/coder/code/sh/Findlatest.sh Jun  1 03:43 
$ ./Findlatest.sh /usr/local/etc
/usr/local/etc/gimp/2.2/unitrc May 13 20:21 
$

I'm not sure if this will work for you, since I tried it with a non-gnu version of stat.