LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices

Reply
 
LinkBack Search this Thread
Old 04-28-2013, 08:27 PM   #1
ianwahome
LQ Newbie
 
Registered: Apr 2013
Posts: 3

Rep: Reputation: Disabled
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
 
Old 04-28-2013, 09:06 PM   #2
dugan
Senior Member
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 4,238

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

And neither are the other forums you pasted this into.
 
Old 04-28-2013, 09:22 PM   #3
ianwahome
LQ Newbie
 
Registered: Apr 2013
Posts: 3

Original Poster
Rep: Reputation: Disabled
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
 
Old 04-28-2013, 11:30 PM   #4
lleb
Senior Member
 
Registered: Dec 2005
Location: Florida
Distribution: CentOS/Fedora
Posts: 2,289

Rep: Reputation: 394Reputation: 394Reputation: 394Reputation: 394
Quote:
Originally Posted by ianwahome View Post
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.
 
Old 04-29-2013, 02:55 AM   #5
kooru
Senior Member
 
Registered: Sep 2012
Location: Italy
Distribution: Slackware, NetBSD
Posts: 1,101
Blog Entries: 3

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

Code:
man mkdir
man mv
man touch
 
1 members found this post helpful.
Old 04-29-2013, 05:51 AM   #6
TenTenths
Senior Member
 
Registered: Aug 2011
Location: Dublin
Distribution: Centos 5 / 6
Posts: 1,244

Rep: Reputation: 394Reputation: 394Reputation: 394Reputation: 394
Quote:
Originally Posted by ianwahome View Post
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:
 
Old 04-29-2013, 08:15 AM   #7
onebuck
Moderator
 
Registered: Jan 2005
Location: Midwest USA, Central Illinois
Distribution: SlackwareŽ
Posts: 10,883
Blog Entries: 1

Rep: Reputation: 1307Reputation: 1307Reputation: 1307Reputation: 1307Reputation: 1307Reputation: 1307Reputation: 1307Reputation: 1307Reputation: 1307Reputation: 1307
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 .
 
Old 05-01-2013, 09:32 PM   #8
ianwahome
LQ Newbie
 
Registered: Apr 2013
Posts: 3

Original Poster
Rep: Reputation: Disabled
#!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
 
Old 05-02-2013, 12:35 AM   #9
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.5
Posts: 16,086

Rep: Reputation: 1995Reputation: 1995Reputation: 1995Reputation: 1995Reputation: 1995Reputation: 1995Reputation: 1995Reputation: 1995Reputation: 1995Reputation: 1995Reputation: 1995
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/

Last edited by chrism01; 05-02-2013 at 12:36 AM.
 
Old 05-02-2013, 11:14 AM   #10
lleb
Senior Member
 
Registered: Dec 2005
Location: Florida
Distribution: CentOS/Fedora
Posts: 2,289

Rep: Reputation: 394Reputation: 394Reputation: 394Reputation: 394
Quote:
Originally Posted by ianwahome View Post
#!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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
SHELL SCRIPT Write at the right of the shell window solo9300 Linux - General 3 09-29-2009 04:56 PM
How to ssh from a shell script ? For ppl who can write shell scripts. thefountainhead100 Programming 14 10-22-2008 06:24 AM
Need help to write Shell Script vikrambhimbar Linux - Software 1 05-08-2006 06:10 AM
[SHELL SCRIPT] Write at the right of the shell window Creak Linux - General 2 04-02-2004 03:00 PM


All times are GMT -5. The time now is 02:56 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration