Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6868409
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T03:25:52+00:00 2026-05-27T03:25:52+00:00

I want to write code like this: command="some command" safeRunCommand $command safeRunCommand() { cmnd=$1

  • 0

I want to write code like this:

command="some command"

safeRunCommand $command

safeRunCommand() {
   cmnd=$1

   $($cmnd)

   if [ $? != 0 ]; then
      printf "Error when executing command: '$command'"
      exit $ERROR_CODE
   fi
}

But this code does not work the way I want. Where did I make the mistake?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T03:25:52+00:00Added an answer on May 27, 2026 at 3:25 am

    Below is the fixed code:

    #!/bin/ksh
    safeRunCommand() {
      typeset cmnd="$*"
      typeset ret_code
    
      echo cmnd=$cmnd
      eval $cmnd
      ret_code=$?
      if [ $ret_code != 0 ]; then
        printf "Error: [%d] when executing command: '$cmnd'" $ret_code
        exit $ret_code
      fi
    }
    
    command="ls -l | grep p"
    safeRunCommand "$command"
    

    Now if you look into this code, the few things that I changed are:

    • use of typeset is not necessary, but it is a good practice. It makes cmnd and ret_code local to safeRunCommand
    • use of ret_code is not necessary, but it is a good practice to store the return code in some variable (and store it ASAP), so that you can use it later like I did in printf "Error: [%d] when executing command: '$command'" $ret_code
    • pass the command with quotes surrounding the command like safeRunCommand "$command". If you don’t then cmnd will get only the value ls and not ls -l. And it is even more important if your command contains pipes.
    • you can use typeset cmnd="$*" instead of typeset cmnd="$1" if you want to keep the spaces. You can try with both depending upon how complex is your command argument.
    • ‘eval’ is used to evaluate so that a command containing pipes can work fine

    Note: Do remember some commands give 1 as the return code even though there isn’t any error like grep. If grep found something it will return 0, else 1.

    I had tested with KornShell and Bash. And it worked fine. Let me know if you face issues running this.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I often find I want to write code something like this in C#, but
The code that I want to write is like this: void MethodOnThreadA() { for
I'm overriding WndProc, so I want to write code like if (m.Msg == WM_COMMAND)
How do I write code to connect through Windows VPN client? I want this
Is there a better way to write this code? I want to show a
I want to write simple C++ code for adding two integers (in a command
I am a AS3 novice learning PureMVC and want to write code following best
I want to write an NSOutputStream to a server with apple's sample code: NSURL
I want to write a simple task which will update and commit source code
I want to write a .xml file using the following code into the App_Data/posts.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.