简体   繁体   English

curl的bash别名函数

[英]bash alias function for curl

I want to setup a bash alias for curl so I only need to pass the file and server url.我想为 curl 设置一个 bash 别名,所以我只需要传递文件和服务器 url。

I have tried variations of我尝试过

alias upload='function _upload(){ curl -F 'file=@"$1"' "$2"; };_upload'

without success没有成功

I also need to some basic checking when i execute the alias ie if i only do我还需要在执行别名时进行一些基本检查,即如果我只执行

upload file.txt

it should return "destination server missing" as I haven't entered the server url.它应该返回“目标服务器丢失”,因为我没有输入服务器 url。 Same should be done if the filename is missing.如果缺少文件名,也应该这样做。

Don't use aliases for anything complicated.不要对任何复杂的事情使用别名。 Stick to functions.坚持功能。

upload() {
    [[ "$1" ]] || { echo "Error: Missing file" >&2; return 1; }
    [[ "$2" ]] || { echo "Error: Missing url"  >&2; return 1; }
    curl -F "file=@$1" "$2"
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM