LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (http://www.linuxquestions.org/questions/linux-general-1/)
-   -   Write a shell script capable of doing the following: (http://www.linuxquestions.org/questions/linux-general-1/write-a-shell-script-capable-of-doing-the-following-4175459929/)

ianwahome 04-28-2013 08:27 PM

Write a shell script capable of doing the following:
 
1. Create a directory "preFinal" in your home directory. In case that such directory exist, rename the existing one into "preFinal.000" and then create empty directory preFinal. While renaming existing preFinal into preFinal.000 you may discover that preFinal.000 also exists, so it needs to be renamed into preFinal.001 and so on.

2. Create empty files "a", "b", "c", etc in directory preFinal in the amount that match the number of directories preFinal*

Example:

1. Assume there are no directories preFinal* in your $HOME (very first run of the script). It will create directory preFinal and will create empty file "a" in it.

2. Second run of the same script will result in renaming preFinal into preFinal.000 and creating empty files "a" and "b" in preFinal. At the end of this run you will have empty file "a" in preFinal.000 and empty files "a" and "b" in preFinal

3. The 3rd run of the scrip will result in empty files "a","b","c" in preFinal, files "a","b" in preFinal.000 and "a" in preFinal.001

4. and so on

dugan 04-28-2013 09:06 PM

Obligatory: we are not here to do your homework for you.

And neither are the other forums you pasted this into.

ianwahome 04-28-2013 09:22 PM

this is not homework, it is a question from a previous test and i want to know if i got it right thats all but if you dont want to help thats fine, i'll just keep on researching

lleb 04-28-2013 11:30 PM

Quote:

Originally Posted by ianwahome (Post 4940826)
this is not homework, it is a question from a previous test and i want to know if i got it right thats all but if you dont want to help thats fine, i'll just keep on researching

then post what you did and we will be more then happy to help.

kooru 04-29-2013 02:55 AM

Hi and welcome to LQ!
It sounds as homework, anyway to start:

Code:

man mkdir
man mv
man touch


TenTenths 04-29-2013 05:51 AM

Quote:

Originally Posted by ianwahome (Post 4940826)
this is not homework, it is a question from a previous test and i want to know if i got it right thats all but if you dont want to help thats fine, i'll just keep on researching

LOL, totally homework!

If it wasn't then you'd just run your answer on a regular linux machine and see if it gave you the expected results!

So.... put your money where your mouth is, post what you've tried:

onebuck 04-29-2013 08:15 AM

Moderator Response
 
@Everyone: We now know by the members statement the request is not for homework. So no more corrections by fellow members.

@ianwahome, please post your script so members can help you .

ianwahome 05-01-2013 09:32 PM

#!bin/bash

title="preFinal"

if [ -d $title ]; then
i=0
for i in *; do
temp=$(echo "$i" | sed 's/^.*\.//'
if [ $temp == "title" ]; then
temp=-1
fi
temp=`echo $temp|sed 's/^0*//'`
((temp++))
temp=$(printf "%03d" $temp)
#temp="00$temp"
echo $temp
mv $i $title.$temp
((i++))
done

mkdir $title
let="61"

while [ $i -ge 0 ];
do
des=$(printf "\x$let")
touch $title/$des
((i--))
((let++))
done
else
mkdir $title
touch $title/a
fi

chrism01 05-02-2013 12:35 AM

1. please use code tags https://www.linuxquestions.org/quest...do=bbcode#code to post your code and indent it.

2. try amending the top of the script thus
Code:

#!bin/bash
set -xv

This enables the parser to show you what's going on; this will help you enormously to debug.

Useful links
http://rute.2038bug.com/index.html.gz
http://tldp.org/LDP/Bash-Beginners-G...tml/index.html
http://www.tldp.org/LDP/abs/html/

lleb 05-02-2013 11:14 AM

Quote:

Originally Posted by ianwahome (Post 4943066)
#!bin/bash

title="preFinal"

if [ -d $title ]; then
i=0
for i in *; do+

temp=$(echo "$i" | sed 's/^.*\.//' #### you are missing the closing ) and fi

if [ $temp == "title" ]; then
temp=-1
fi
temp=`echo $temp|sed 's/^0*//'`
((temp++))
temp=$(printf "%03d" $temp)
#temp="00$temp"
echo $temp
mv $i $title.$temp
((i++))
done

mkdir $title
let="61"

while [ $i -ge 0 ];
do
des=$(printf "\x$let")
touch $title/$des
((i--))
((let++))
done
else
mkdir $title
touch $title/a
fi

mind you im no script guru but i think i see a few issues.


All times are GMT -5. The time now is 07:55 AM.