[ home / board list / faq / random / create / bans / search / manage / irc ] [ ]

/prog/ - Programming

Programming board

Catalog

8chan Bitcoin address: 1NpQaXqmCBji6gfX8UgaQEmEstvVY7U32C
The next generation of Infinity is here (discussion) (contribute)
A message from @CodeMonkeyZ, 2ch lead developer: "How Hiroyuki Nishimura will sell 4chan data"
Name
Email
Subject
Comment *
File
* = required field[▶ Show post options & limits]
Confused? See the FAQ.
Embed
(replaces files and can be used instead)
Options
Password (For file and post deletion.)

Allowed file types:jpg, jpeg, gif, png, webm, mp4, pdf
Max filesize is 8 MB.
Max image dimensions are 10000 x 10000.
You may upload 1 per post.


File: 1422374073619.gif (237.12 KB, 255x162, 85:54, 1421935881546.gif)

cacce0 No.1131

Shell scripting thread
Batch and bash are welcome. Post questions, tips, and what you have made.

543879 No.1281

>>1131
Some things allow you to embed a single line of bash into a right click function or what the fuck ever, you can embed a huge ass script though if you use bash -c "script here"
Its a hack but it let's you by in some constrained places

83b56f No.1297

>>1281
Wouldn't "/path/to/executable" work fine as well? No need to limit yourself to bash that way.

7a0a99 No.2487

I was working on a script to copy all my Ebooks and convert them to my preferable extension.

Now I crave a script that removes the last character from the filenames in a directory.


6db79a No.2488

>>2487

Assuming you're on a relatively mainstream Gahnoo+Leenooks distro:


find . -type f -exec rename 's/.$//' '{}' +

Or, alternatively, if there are no subdirectories:


rename 's/.$//' *

For future reference, rename can change file extensions too:


rename 's/\.old/.new/' *.old


7a0a99 No.2489

>>2488

Bless you for the effort, but they don't seem to work.

"SpecialVariables

"

Is one of the file names, deleting all nonalphanumeric whitespaces might do the trick.


6db79a No.2490

>>2489

>newlines at the end of the files

In that case, try this (just run it in the offending directory):


#!/usr/bin/env python2.7

import os
import sys

for file in os.listdir("."):
try:
# Skip dot files
if file[:1] == '.':
continue

# Skip things that are not files
with open(file):
os.rename(file, file[:-1])

except IOError as err:
# Whine and complain
sys.stderr.write("%s\n" % str(err))

except OSError:
# Whine and complain louder
sys.stderr.write("failed to rename file: `%s'\n" % file)


7a0a99 No.2491

>>2490

Thank you very much!


2055f6 No.2514

check_ses()
{
sudo -u ${USER} tmux has-session -t $TMUX_CONSOLE
}

How do I get this to return the results instead of printing it to stdout?


6db79a No.2516

>>2514

>How do I get this to return the results instead of printing it to stdout?

You can't.

You can store the output in a variable though:


OUTPUT=$(check_ses)

if [ "$?" -eq 0 ]; then
# Command was successful
else
# Command failed
fi

What this does is causes the shell to fork - the child shell executes the shell function,

then the parent reads the output of the child shell and stores it in a variable, OUTPUT.

I'm not even trolling.


84537e No.2548

is there a difference and does it matter if i do

result=$(echo "$foo")

vs

result="$(echo $foo)"

?


6db79a No.2552

>>2548

Double quotes tell the shell that everything in between is a single argument.

If you don't have double quotes, then the shell splits up the expanded variable.

For example, if "$foo" is "-n foo bar bazz", then this:


result=$(echo "$foo")

Would give you "-n foo bar bazz", while this:


result="$(echo $foo)"

Gives you "foo bar bazz".


84537e No.2555

>>2552

nice example, thanks.

and i suppose i can do

result="$(echo "$foo")"

because the quotes inside the brackets are evaluated separately from those around them?


6db79a No.2557

>>2555

>because the quotes inside the brackets are evaluated separately from those around them?

No. What you're giving the shell is three separate tokens:

>"$(echo "

>$foo

and

>")"

Because there is no whitespace in between, it concatenates them to make a single unit:


$(echo $foo)

Then the whole thing is evaluated to get your result.

If you just want to copy the variable though:


result=$foo

Will do the trick.


6bafe1 No.2595

>>2557

>No. What you're giving the shell is three separate tokens

Wrong.

One of the main benefits of using

$(...)
instead of
`...`
is that the contents of the former will get evaluated separately from the surrounding code; i.e.
"$(echo "$foo")"
is a single togen
"$(...)"
, where the echo function is also given a single token
"$foo"
as an argument.


84537e No.2609

>>2595

indeed. newlines in foo get preserved correctly in

result="$(echo "$foo")"

so the quotes are evaluated correctly. unintuitive but useful.




[Return][Go to top][Catalog][Post a Reply]
Delete Post [ ]
[]
[ home / board list / faq / random / create / bans / search / manage / irc ] [ ]