sh

How can I remove the extension of a filename in a shell script?

一曲冷凌霜 提交于 2019-12-17 03:23:58
问题 What's wrong with the following code? name='$filename | cut -f1 -d'.'' As is, I get the literal string $filename | cut -f1 -d'.' , but if I remove the quotes I don't get anything. Meanwhile typing "test.exe" | cut -f1 -d'.' in a shell gives me the output I want, test . I already know $filename has been assigned the right value. What I want to do is assign to a variable the filename without the extension. 回答1: You should be using the command substitution syntax $(command) when you want to

Why is testing “$?” to see if a command succeeded or not, an anti-pattern?

☆樱花仙子☆ 提交于 2019-12-17 02:38:18
问题 I see here that testing whether $? is zero (success) or something else (failure) is an anti-pattern, but I have not been able to find this anywhere else. Sticking to the definition of anti-pattern of the Wikipedia: "An anti-pattern (or anti-pattern) is a common response to a recurring problem that is usually ineffective and risks being highly counterproductive." Why would this be an anti-pattern? 回答1: This is an antipattern because it introduces complexity that wouldn't exist if you didn't

What does $@ mean in a shell script?

随声附和 提交于 2019-12-17 02:33:32
问题 What does a dollar sign followed by an at-sign ( @ ) mean in a shell script? For example: umbrella_corp_options $@ 回答1: $@ is all of the parameters passed to the script. For instance, if you call ./someScript.sh foo bar then $@ will be equal to foo bar . If you do: ./someScript.sh foo bar and then inside someScript.sh reference: umbrella_corp_options "$@" this will be passed to umbrella_corp_options with each individual parameter enclosed in double quotes, allowing to take parameters with

How to execute mongo commands through shell scripts?

僤鯓⒐⒋嵵緔 提交于 2019-12-17 02:05:07
问题 I want to execute mongo commands in shell script, e.g. in a script test.sh : #!/bin/sh mongo myDbName db.mycollection.findOne() show collections When I execute this script via ./test.sh , then the connection to MongoDB is established, but the following commands are not executed. How to execute other commands through shell script test.sh ? 回答1: You can also evaluate a command using the --eval flag, if it is just a single command. mongo --eval "printjson(db.serverStatus())" Please note: if you

How to execute mongo commands through shell scripts?

徘徊边缘 提交于 2019-12-17 02:04:30
问题 I want to execute mongo commands in shell script, e.g. in a script test.sh : #!/bin/sh mongo myDbName db.mycollection.findOne() show collections When I execute this script via ./test.sh , then the connection to MongoDB is established, but the following commands are not executed. How to execute other commands through shell script test.sh ? 回答1: You can also evaluate a command using the --eval flag, if it is just a single command. mongo --eval "printjson(db.serverStatus())" Please note: if you

Why does my bash code fail when I run it with sh?

南笙酒味 提交于 2019-12-16 22:52:27
问题 I have a line of code that works fine in my terminal: for i in *.mp4; do echo ffmpeg -i "$i" "${i/.mp4/.mp3}"; done Then I put the exact same line of code in a script myscript.sh : #!/bin/sh for i in *.mp4; do echo ffmpeg -i "$i" "${i/.mp4/.mp3}"; done However, now I get an error when running it: $ sh myscript.sh myscript.sh: 2: myscript.sh: Bad substitution Based on other questions I tried changing the shebang to #!/bin/bash , but I get the exact same error. Why can't I run this script? 回答1:

backtick is not working to run mysql queries in shell script

冷暖自知 提交于 2019-12-16 18:02:06
问题 Hi I am trying to run MySQL queries from shell script. mysql -u root -p'1234' -e "CREATE TABLE $DB.aa_vv_cc ( id int(10) unsigned NOT NULL AUTO_INCREMENT, city varchar(100) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL, lat varchar(255) DEFAULT NULL, `long` varchar(255) DEFAULT NULL, status int(11) NOT NULL DEFAULT '1', created_at timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, updated_at timestamp NOT NULL DEFAULT '0000-00-00 00:00:00', PRIMARY KEY ('id') );" I am getting error on "long"

How to display newline in ssh

巧了我就是萌 提交于 2019-12-14 01:07:11
问题 I'm trying to do the following: #!/bin/sh ssh user@server "echo \"Test \n for newline\"" This displays: test \n for newline How do I get the shell to interpret \n as an actual newline? 回答1: Try using the -e option, e.g., echo -e "Test \n for newline" . If your echo doesn't have a -e option, then I'd use printf . It's widely available and it does not have nearly as many variations in it's implementations. 回答2: For greater portability, use printf instead of echo . #!/bin/sh ssh user@server

Hex to Dec conversion with printf in sh fail for more than 16 digits

﹥>﹥吖頭↗ 提交于 2019-12-13 23:46:10
问题 I only have shell available no bash, Perl, python etc. Using printf small numbers work: root@DD-WRT:/jffs# printf "%d\n", 0x15a 346 But large numbers fail. root@DD-WRT:/jffs# printf "%d\n", 0x15abc12345afda325 sh: invalid number '0x15abc12345afda325' 0 Also is it possible to perform hexadecimal arithmetic for example Module using shell ? 回答1: What shell is this? On Linux I see: $ bash -c 'echo $((0x15abc12345afda325))' 6538120775109288741 wrong $ dash -c 'echo $((0x15abc12345afda325))'

Extract data from log [duplicate]

你。 提交于 2019-12-13 22:08:57
问题 This question already has an answer here : bash only email if occurrence since last alert (1 answer) Closed 4 years ago . I have logs in format ##<01-Mar-2015 03:48:18 o'clock GMT> <info> ##<01-Mar-2015 03:48:20 o'clock GMT> <info> ##<01-Mar-2015 03:48:30 o'clock GMT> <info> ##<01-Mar-2015 03:48:39 o'clock GMT> <info> I got to write shell script to extract data of last 5 minutes from the last recorded data in the log file and then search a string in it.I am new to shell script , I used grep