8 Replies - 531 Views - Last Post: 04 March 2012 - 11:57 PM

#1 diek  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 53
  • Joined: 06-November 11

Script - Execute Command and Displaying Success/Return to User

Posted 04 March 2012 - 09:43 PM

I am writing a basic script. I am just learning and I have been reading various tuts on the internet. I just cannot figure this out. I have been using openSUSE, staying away from windows to help the learning curve.

Is using touch the only way to create a file? I know that if I have test I can add it to echo and create the file. Are there other options?

I cannot figure out how to report to the screen that the file or folder was successfully created and return the file/folder name. The echo `ls*` is just overkill, is there something that will be more specific?

Thank you,

diek

This post has been edited by diek: 04 March 2012 - 09:43 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Script - Execute Command and Displaying Success/Return to User

#2 no2pencil  Icon User is offline

  • Original Digital Gansta
  • member icon

Reputation: 4462
  • View blog
  • Posts: 24,905
  • Joined: 10-May 07

Re: Script - Execute Command and Displaying Success/Return to User

Posted 04 March 2012 - 09:46 PM

The following will test if a file exists using dash f. If the file does exist, it will be deleted, if it does not, it will be created using touch.

#!/bin/sh

file=test.file
if [ ! -f ${file} ]; then
  echo ${file} does not exist...
  touch ${file}
else
  echo ${file} does exist...
  rm -f ${file}
fi


Was This Post Helpful? 1
  • +
  • -

#3 diek  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 53
  • Joined: 06-November 11

Re: Script - Execute Command and Displaying Success/Return to User

Posted 04 March 2012 - 09:55 PM

That makes perfect sense, can the same be done for folders?
Was This Post Helpful? 0
  • +
  • -

#4 no2pencil  Icon User is offline

  • Original Digital Gansta
  • member icon

Reputation: 4462
  • View blog
  • Posts: 24,905
  • Joined: 10-May 07

Re: Script - Execute Command and Displaying Success/Return to User

Posted 04 March 2012 - 09:59 PM

Fairly certain directories (not folders) is the dash d option. Also you can't touch a directory, you have to use mkdir. Lastly you use rm -r to remove a directory.
Was This Post Helpful? 0
  • +
  • -

#5 diek  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 53
  • Joined: 06-November 11

Re: Script - Execute Command and Displaying Success/Return to User

Posted 04 March 2012 - 10:11 PM

Okay, when linux executes commands, can the successful execution be captured in an echo statement? The file and folder were two of the examples, permissions, moving files, are a few more examples. So I have to do add a number of other commands and return success in each case.

And I do appreciate the help, thank you.

d
Was This Post Helpful? 0
  • +
  • -

#6 no2pencil  Icon User is offline

  • Original Digital Gansta
  • member icon

Reputation: 4462
  • View blog
  • Posts: 24,905
  • Joined: 10-May 07

Re: Script - Execute Command and Displaying Success/Return to User

Posted 04 March 2012 - 10:15 PM

You can check $? for the return result of the previous command.

rm -f ${file}
if [ $? -ne 0 ]; then
  echo The file failed to be removed
fi


Was This Post Helpful? 0
  • +
  • -

#7 diek  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 53
  • Joined: 06-November 11

Re: Script - Execute Command and Displaying Success/Return to User

Posted 04 March 2012 - 10:50 PM

I am doing something wrong here, but I am just not seeing it.
new_folder=W0235269
rmdir ${new_folder}
mkdir ${new_folder}


Do I need the curly braces?

This post has been edited by diek: 04 March 2012 - 10:56 PM

Was This Post Helpful? 0
  • +
  • -

#8 no2pencil  Icon User is offline

  • Original Digital Gansta
  • member icon

Reputation: 4462
  • View blog
  • Posts: 24,905
  • Joined: 10-May 07

Re: Script - Execute Command and Displaying Success/Return to User

Posted 04 March 2012 - 10:55 PM

View Postno2pencil, on 04 March 2012 - 11:59 PM, said:

Lastly you use rm -r to remove a directory.

Was This Post Helpful? 0
  • +
  • -

#9 diek  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 53
  • Joined: 06-November 11

Re: Script - Execute Command and Displaying Success/Return to User

Posted 04 March 2012 - 11:57 PM

opensuse whined like a ---- when I used rm, so I had to use rmdir. So folders and files portion complete, again I appreciate the guidance. its late and I gotta get some rack, hardware class tomorrow.

d
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1