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 9172149
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T16:16:38+00:00 2026-06-17T16:16:38+00:00

I’m attempting to construct and execute a csh script from python. The code I

  • 0

I’m attempting to construct and execute a csh script from python.

The code I have produces what looks like a correct script, and os.system("my_script.csh") returns ‘0’, but the script doesn’t perform the task within it unless I go into it manually using vim and re-save it (changing nothing in the script manually – i don’t even enter ‘insert’ mode). What is it that re-saving in vim does that isn’t being done in my code, and is it possible to do it?

Here’s the relevant part of my code:

grabmeName = '%sgrabme%s.csh'%(dirNames['grabmes'],uniqID)
if not os.path.exists(grabmeName):
    open(grabmeName,'w').close()
    os.chmod(grabmeName,0777)
    with open(grabmeName,'a') as f:
        f.write("#!/bin/csh -f\n")
        f.write("echo 'hello'")
    os.system(grabmeName)
  • 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-06-17T16:16:40+00:00Added an answer on June 17, 2026 at 4:16 pm

    The main problem is that every line in the shell needs to end with a \n in order to be executed, even the last line. You can just add \n to the end of the "echo 'hello'" string. This is arguably a bug in csh, since bash and friends don’t have this problem, but if you want to use csh, you’ll have to accommodate it.

    When you save a text file in vim, it adds a trailing newline to the file if there wasn’t one to begin with. You can verify this by saving a copy of the file beforehand and running diff to see what vim changes:

    $ cat blah-grabme-12.csh 
    #!/bin/csh -f
    echo 'hello'$ cp blah-grabme-12.csh blah-grabme-12.csh.orig
    $ vim blah-grabme-12.csh
    ┌────────────────────────────────────────────────────────────────────────────────┐
    │#!/bin/csh -f                                                                   │
    │echo 'hello'                                                                    │
    │~                                                                               │
    │~                                                                               │
    │~                                                                               │
    │~                                                                               │
    │~                                                                               │
    │~                                                                               │
    │~                                                                               │
    │~                                                                               │
    │~                                                                               │
    │~                                                                               │
    │~                                                                               │
    │~                                                                               │
    │~                                                                               │
    │~                                                                               │
    │~                                                                               │
    │~                                                                               │
    │~                                                                               │
    │~                                                                               │
    │~                                                                               │
    │~                                                                               │
    │~                                                                               │
    │:wq❚                                                                            │
    └────────────────────────────────────────────────────────────────────────────────┘
    $ diff -u blah-grabme-12.csh.orig blah-grabme-12.csh.orig
    --- blah-grabme-12.csh.orig
    +++ blah-grabme-12.csh
    @@ -1,2 +1,2 @@
     #!/bin/csh -f
    -echo 'hello'
    \ No newline at end of file
    +echo 'hello'
    

    You can turn off this behaviour of vim in your ~/.vimrc if you’d like. See :help 'eol' in vim help.

    Another potential problem is that system(filename) will only work if filename is a non-bare path—i.e., has a / in it—or if . is in the system $PATH.

    Additionally, by using os.open() instead of open(), you can set the file permissions at file creation time. Here it might not make a big difference, but in many contexts, creating the file and then changing its permissions results in a security vulnerability. This stackoverflow question shows how to do that.


    Putting it all together, you’d get something like this:

    import os
    import os.path
    
    grabmeName = 'blah-grabme-12.csh'
    
    with os.fdopen(os.open(grabmeName, os.O_WRONLY | os.O_CREAT, 0700), 'w') as f:
        f.write("#!/bin/csh -f\n")
        f.write("echo 'hello'\n")
    os.system(os.path.abspath(grabmeName))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an autohotkey script which looks up a word in a bilingual dictionary
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a small JavaScript validation script that validates inputs based on Regex. I
I have this code to decode numeric html entities to the UTF8 equivalent character.
I would like to run a str_replace or preg_replace which looks for certain words
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I have a text area in my form which accepts all possible characters from
I have a view passing on information from a database: def serve_article(request, id): served_article
I have a bunch of posts stored in text files formatted in yaml/textile (from

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.