Sponsored Content
Full Discussion: Block ";" in input string
Top Forums Programming Block ";" in input string Post 302077259 by Corona688 on Wednesday 21st of June 2006 01:04:06 AM
Old 06-21-2006
It's a bad sign when your programming strategy shows up on The Daily WTF. But then, half of the important things I learned, I learned on TDWTF. Smilie

How about escaping things instead?
Code:
// bashescape.c
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
int safe_system(const char *strin)
{
  int m,pos=0;
  char bufout[512];

  for(m=0; (pos<511)&&(strin[m] != '\0'); m++)
  {
    char c=strin[m];
    if(!(isalnum(c) || isspace(c)))
    {
      bufout[pos++]='\\';
      if(pos >= 510) break;
    }

    bufout[pos++]=c;
  }

  fprintf(stderr,"system(\"%s\")\n",bufout);
  return(system(bufout));
}

int main(int argc, char *argv[])
{
  return(safe_system(argv[1]));
}

Code:
# cc bashescape.c -o bashescape
# ./bashescape "echo hello ; world"
system("echo hello \; world")
hello ; world
#

 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

check input = "empty" and "numeric"

Hi how to check input is "empty" and "numeric" in ksh? e.g: ./myscript.ksh k output show: invalid number input ./myscript.ksh output show: no input ./myscript.ksh 10 output show: input is numeric (6 Replies)
Discussion started by: geoffry
6 Replies

2. Shell Programming and Scripting

input string="3MMTQSZ348GGMZRQWMJM4SD6M";output string="3MMTQ-SZ348-GGMZR-QWMJM-4SD6

input string="3MMTQSZ348GGMZRQWMJM4SD6M" output string="3MMTQ-SZ348-GGMZR-QWMJM-4SD6M" using linux shell script (4 Replies)
Discussion started by: pankajd
4 Replies

3. Shell Programming and Scripting

awk command to replace ";" with "|" and ""|" at diferent places in line of file

Hi, I have line in input file as below: 3G_CENTRAL;INDONESIA_(M)_TELKOMSEL;SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL My expected output for line in the file must be : "1-Radon1-cMOC_deg"|"LDIndex"|"3G_CENTRAL|INDONESIA_(M)_TELKOMSEL"|LAST|"SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL" Can someone... (7 Replies)
Discussion started by: shis100
7 Replies

4. Shell Programming and Scripting

Using sed to find text between a "string " and character ","

Hello everyone Sorry I have to add another sed question. I am searching a log file and need only the first 2 occurances of text which comes after (note the space) "string " and before a ",". I have tried sed -n 's/.*string \(*\),.*/\1/p' filewith some, but limited success. This gives out all... (10 Replies)
Discussion started by: haggismn
10 Replies

5. Shell Programming and Scripting

how to use "cut" or "awk" or "sed" to remove a string

logs: "/home/abc/public_html/index.php" "/home/abc/public_html/index.php" "/home/xyz/public_html/index.php" "/home/xyz/public_html/index.php" "/home/xyz/public_html/index.php" how to use "cut" or "awk" or "sed" to get the following result: abc abc xyz xyz xyz (8 Replies)
Discussion started by: timmywong
8 Replies

6. Shell Programming and Scripting

tcsh - understanding difference between "echo string" and "echo string > /dev/stdout"

I came across and unexpected behavior with redirections in tcsh. I know, csh is not best for redirections, but I'd like to understand what is happening here. I have following script (called out_to_streams.csh): #!/bin/tcsh -f echo Redirected to STDOUT > /dev/stdout echo Redirected to... (2 Replies)
Discussion started by: marcink
2 Replies

7. Shell Programming and Scripting

grep with "[" and "]" and "dot" within the search string

Hello. Following recommendations for one of my threads, this is working perfectly : #!/bin/bash CNT=$( grep -c -e "some text 1" -e "some text 2" -e "some text 3" "/tmp/log_file.txt" ) Now I need a grep success for some thing like : #!/bin/bash CNT=$( grep -c -e "some text_1... (4 Replies)
Discussion started by: jcdole
4 Replies

8. UNIX for Dummies Questions & Answers

Using "mailx" command to read "to" and "cc" email addreses from input file

How to use "mailx" command to do e-mail reading the input file containing email address, where column 1 has name and column 2 containing “To” e-mail address and column 3 contains “cc” e-mail address to include with same email. Sample input file, email.txt Below is an sample code where... (2 Replies)
Discussion started by: asjaiswal
2 Replies

9. Shell Programming and Scripting

How to avoid "Too many arguments" error, when passing a long String literal as input to a command?

Hi, I am using awk here. Inside an awk script, I have a variable which contains a very long XML data in string format (500kb). I want to pass this data (as argument) to curl command using system function. But getting Too many arguments error due to length of string data(payloadBlock). I... (4 Replies)
Discussion started by: cool.aquarian
4 Replies

10. Shell Programming and Scripting

Delete all log files older than 10 day and whose first string of the first line is "MSH" or "<?xml"

Dear Ladies & Gents, I have a requirement to delete all the log files in /var/log/test directory that are older than 10 days and their first line begin with "MSH" or "<?xml" or "FHS". I've put together the following BASH script, but it's erroring out: for filename in $(find /var/log/test... (2 Replies)
Discussion started by: Hiroshi
2 Replies
explain_execvp(3)					     Library Functions Manual						 explain_execvp(3)

NAME
explain_execvp - explain execvp(3) errors SYNOPSIS
#include <libexplain/execvp.h> const char *explain_execvp(const char *pathname, char *const *argv); const char *explain_errno_execvp(int errnum, const char *pathname, char *const *argv); void explain_message_execvp(char *message, int message_size, const char *pathname, char *const *argv); void explain_message_errno_execvp(char *message, int message_size, int errnum, const char *pathname, char *const *argv); DESCRIPTION
These functions may be used to obtain explanations for errors returned by the execvp(3) system call. explain_execvp const char *explain_execvp(const char *pathname, char *const *argv); The explain_execvp function is used to obtain an explanation of an error returned by the execvp(3) system call. The least the message will contain is the value of strerror(errno), but usually it will do much better, and indicate the underlying cause in more detail. The errno global variable will be used to obtain the error value to be decoded. This function is intended to be used in a fashion similar to the following example: if (execvp(pathname, argv) < 0) { fprintf(stderr, "%s ", explain_execvp(pathname, argv)); exit(EXIT_FAILURE); } The above code example is available pre-packaged as the explain_execvp_or_die(3) function. pathname The original pathname, exactly as passed to the execvp(3) system call. argv The original argv, exactly as passed to the execvp(3) system call. Returns: The message explaining the error. This message buffer is shared by all libexplain functions which do not supply a buffer in their argument list. This will be overwritten by the next call to any libexplain function which shares this buffer, including other threads. Note: This function is not thread safe, because it shares a return buffer across all threads, and many other functions in this library. explain_errno_execvp const char *explain_errno_execvp(int errnum, const char *pathname, char *const *argv); The explain_errno_execvp function is used to obtain an explanation of an error returned by the execvp(3) system call. The least the mes- sage will contain is the value of strerror(errnum), but usually it will do much better, and indicate the underlying cause in more detail. This function is intended to be used in a fashion similar to the following example: if (execvp(pathname, argv) < 0) { int err = errno; fprintf(stderr, "%s ", explain_errno_execvp(err, pathname, argv)); exit(EXIT_FAILURE); } The above code example is available pre-packaged as the explain_execvp_or_die(3) function. errnum The error value to be decoded, usually obtained from the errno global variable just before this function is called. This is neces- sary if you need to call any code between the system call to be explained and this function, because many libc functions will alter the value of errno. pathname The original pathname, exactly as passed to the execvp(3) system call. argv The original argv, exactly as passed to the execvp(3) system call. Returns: The message explaining the error. This message buffer is shared by all libexplain functions which do not supply a buffer in their argument list. This will be overwritten by the next call to any libexplain function which shares this buffer, including other threads. Note: This function is not thread safe, because it shares a return buffer across all threads, and many other functions in this library. explain_message_execvp void explain_message_execvp(char *message, int message_size, const char *pathname, char *const *argv); The explain_message_execvp function may be used to obtain an explanation of an error returned by the execvp(3) system call. The least the message will contain is the value of strerror(errno), but usually it will do much better, and indicate the underlying cause in more detail. The errno global variable will be used to obtain the error value to be decoded. This function is intended to be used in a fashion similar to the following example: if (execvp(pathname, argv) < 0) { char message[3000]; explain_message_execvp(message, sizeof(message), pathname, argv); fprintf(stderr, "%s ", message); exit(EXIT_FAILURE); } The above code example is available pre-packaged as the explain_execvp_or_die(3) function. message The location in which to store the returned message. If a suitable message return buffer is supplied, this function is thread safe. message_size The size in bytes of the location in which to store the returned message. pathname The original pathname, exactly as passed to the execvp(3) system call. argv The original argv, exactly as passed to the execvp(3) system call. explain_message_errno_execvp void explain_message_errno_execvp(char *message, int message_size, int errnum, const char *pathname, char *const *argv); The explain_message_errno_execvp function may be used to obtain an explanation of an error returned by the execvp(3) system call. The least the message will contain is the value of strerror(errnum), but usually it will do much better, and indicate the underlying cause in more detail. This function is intended to be used in a fashion similar to the following example: if (execvp(pathname, argv) < 0) { int err = errno; char message[3000]; explain_message_errno_execvp(message, sizeof(message), err, pathname, argv); fprintf(stderr, "%s ", message); exit(EXIT_FAILURE); } The above code example is available pre-packaged as the explain_execvp_or_die(3) function. message The location in which to store the returned message. If a suitable message return buffer is supplied, this function is thread safe. message_size The size in bytes of the location in which to store the returned message. errnum The error value to be decoded, usually obtained from the errno global variable just before this function is called. This is neces- sary if you need to call any code between the system call to be explained and this function, because many libc functions will alter the value of errno. pathname The original pathname, exactly as passed to the execvp(3) system call. argv The original argv, exactly as passed to the execvp(3) system call. SEE ALSO
execvp(3) execute a file explain_execvp_or_die(3) execute a file and report errors COPYRIGHT
libexplain version 0.52 Copyright (C) 2009 Peter Miller explain_execvp(3)
All times are GMT -4. The time now is 02:57 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy